Test Failed
Push — extract ( e8a5db )
by Jeroen De
05:13 queued 18s
created

GitHubParserHook::getFileContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace GitHub;
4
5
use FileFetcher\FileFetcher;
6
use FileFetcher\FileFetchingException;
7
use Michelf\Markdown;
8
use ParamProcessor\ProcessingResult;
9
use Parser;
10
use ParserHooks\HookHandler;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class GitHubParserHook implements HookHandler {
17
18
	private $gitHubFetcher;
19
20
	/**
21
	 * @var Parser
22
	 */
23
	private $parser;
24
25
	// Parameters for SyntaxHighlight extension (formerly SyntaxHighlight_GeSHi)
26
	// https://www.mediawiki.org/wiki/Extension:SyntaxHighlight
27
	private $syntaxHighlightLanguage;
28
	private $syntaxHighlightEnableLineNumbers;
29
	private $syntaxHighlightStartingLineNumber;
30
	private $syntaxHighlightHighlightedLines;
31
	private $syntaxHighlightInlineSource;
32
33
	public function __construct( GitHubFetcher $gitHubFetcher ) {
34
		$this->gitHubFetcher = $gitHubFetcher;
35
	}
36
37
	public function handle( Parser $parser, ProcessingResult $result ): string {
38
		$this->parser = $parser;
39
40
		$params = $result->getParameters();
41
		$this->setFields( $params );
42
43
		$content = $this->gitHubFetcher->getFileContent(
44
			$params['repo']->getValue(),
45
			$params['branch']->getValue(),
46
			$params['file']->getValue()
47
		);
48
49
		return $this->getRenderedContent( $content, $params['file']->getValue() );
50
	}
51
52
	private function setFields( array $params ) {
53
		$this->syntaxHighlightLanguage = $params['lang']->getValue();
54
		$this->syntaxHighlightEnableLineNumbers = $params['line']->getValue();
55
		$this->syntaxHighlightStartingLineNumber = $params['start']->getValue();
56
		$this->syntaxHighlightHighlightedLines = $params['highlight']->getValue();
57
		$this->syntaxHighlightInlineSource = $params['inline']->getValue();
58
	}
59
60
	private function getRenderedContent( string $content, string $fileName ): string {
61
		if ( $this->isMarkdownFile( $fileName ) ) {
62
			return $this->renderAsMarkdown( $content );
63
		}
64
65
		if ( $this->syntaxHighlightLanguage !== "" ) {
66
			$syntax_highlight = "<syntaxhighlight lang=\"". $this->syntaxHighlightLanguage ."\"";
67
			$syntax_highlight .= " start=\"". $this->syntaxHighlightStartingLineNumber ."\"";
68
69
			if ( $this->syntaxHighlightEnableLineNumbers === true ) {
70
				$syntax_highlight .= " line";
71
			}
72
73
			if ( $this->syntaxHighlightHighlightedLines !== "" ) {
74
				$syntax_highlight .= " highlight=\"". $this->syntaxHighlightHighlightedLines ."\"";
75
			}
76
77
			if ( $this->syntaxHighlightInlineSource === true ) {
78
				$syntax_highlight .= " inline";
79
			}
80
81
			$syntax_highlight .= ">$content</syntaxhighlight>";
82
			return $this->parser->recursiveTagParse( $syntax_highlight, null );
83
		}
84
85
		return $content;
86
	}
87
88
	private function isMarkdownFile( string $fileName ): bool {
89
		return $this->fileHasExtension( $fileName, 'md' )
90
			   || $this->fileHasExtension( $fileName,'markdown' );
91
	}
92
93
	private function fileHasExtension( string $fileName, string $extension ): bool {
94
		$fullExtension = '.' . $extension;
95
		return substr( $fileName, -strlen( $fullExtension ) ) === $fullExtension;
96
	}
97
98
	private function renderAsMarkdown( string $content ): string {
99
		return Markdown::defaultTransform( $content );
100
	}
101
102
}
103