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->syntaxHighlightLanguage === '' ) { |
62
|
|
|
return ( new ContentPurifier() ) |
63
|
|
|
->purify( $this->getRenderedNonSyntaxContent( $content, $fileName ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->getRenderedSyntaxContent( $content ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getRenderedNonSyntaxContent( string $content, string $fileName ): string { |
70
|
|
|
if ( $this->isMarkdownFile( $fileName ) ) { |
71
|
|
|
return $this->renderAsMarkdown( $content ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $content; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getRenderedSyntaxContent( string $content ): string { |
78
|
|
|
$syntax_highlight = "<syntaxhighlight lang=\"". $this->syntaxHighlightLanguage ."\""; |
79
|
|
|
$syntax_highlight .= " start=\"". $this->syntaxHighlightStartingLineNumber ."\""; |
80
|
|
|
|
81
|
|
|
if ( $this->syntaxHighlightEnableLineNumbers === true ) { |
82
|
|
|
$syntax_highlight .= " line"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ( $this->syntaxHighlightHighlightedLines !== "" ) { |
86
|
|
|
$syntax_highlight .= " highlight=\"". $this->syntaxHighlightHighlightedLines ."\""; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ( $this->syntaxHighlightInlineSource === true ) { |
90
|
|
|
$syntax_highlight .= " inline"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$syntax_highlight .= ">$content</syntaxhighlight>"; |
94
|
|
|
$parsed = $this->parser->recursiveTagParse( $syntax_highlight, null ); |
95
|
|
|
|
96
|
|
|
if ( is_string( $parsed ) ) { |
97
|
|
|
return $parsed; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return ''; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function isMarkdownFile( string $fileName ): bool { |
104
|
|
|
return $this->fileHasExtension( $fileName, 'md' ) |
105
|
|
|
|| $this->fileHasExtension( $fileName,'markdown' ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function fileHasExtension( string $fileName, string $extension ): bool { |
109
|
|
|
$fullExtension = '.' . $extension; |
110
|
|
|
return substr( $fileName, -strlen( $fullExtension ) ) === $fullExtension; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function renderAsMarkdown( string $content ): string { |
114
|
|
|
return Markdown::defaultTransform( $content ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|