|
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 $fileFetcher; |
|
19
|
|
|
private $gitHubUrl; |
|
20
|
|
|
|
|
21
|
|
|
private $fileName; |
|
22
|
|
|
private $repoName; |
|
23
|
|
|
private $branchName; |
|
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
|
|
|
/** |
|
34
|
|
|
* @param FileFetcher $fileFetcher |
|
35
|
|
|
* @param string $gitHubUrl |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( FileFetcher $fileFetcher, $gitHubUrl ) { |
|
38
|
|
|
$this->fileFetcher = $fileFetcher; |
|
39
|
|
|
$this->gitHubUrl = $gitHubUrl; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function handle( Parser $parser, ProcessingResult $result ) { |
|
43
|
|
|
$this->setFields( $result ); |
|
44
|
|
|
|
|
45
|
|
|
return $this->getRenderedContent( $parser ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function setFields( ProcessingResult $result ) { |
|
49
|
|
|
$params = $result->getParameters(); |
|
50
|
|
|
|
|
51
|
|
|
$this->fileName = $params['file']->getValue(); |
|
52
|
|
|
$this->repoName = $params['repo']->getValue(); |
|
53
|
|
|
$this->branchName = $params['branch']->getValue(); |
|
54
|
|
|
|
|
55
|
|
|
$this->syntaxHighlightLanguage = $params['lang']->getValue(); |
|
56
|
|
|
$this->syntaxHighlightEnableLineNumbers = $params['line']->getValue(); |
|
57
|
|
|
$this->syntaxHighlightStartingLineNumber = $params['start']->getValue(); |
|
58
|
|
|
$this->syntaxHighlightHighlightedLines = $params['highlight']->getValue(); |
|
59
|
|
|
$this->syntaxHighlightInlineSource = $params['inline']->getValue(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function getRenderedContent( Parser $parser ) { |
|
63
|
|
|
$content = $this->getFileContent(); |
|
64
|
|
|
|
|
65
|
|
|
if ( $this->isMarkdownFile() ) { |
|
66
|
|
|
$content = $this->renderAsMarkdown( $content ); |
|
67
|
|
|
} |
|
68
|
|
|
else if ( $this->syntaxHighlightLanguage !== "" ) { |
|
69
|
|
|
$syntax_highlight = "<syntaxhighlight lang=\"". $this->syntaxHighlightLanguage ."\""; |
|
70
|
|
|
$syntax_highlight .= " start=\"". $this->syntaxHighlightStartingLineNumber ."\""; |
|
71
|
|
|
|
|
72
|
|
|
if ( $this->syntaxHighlightEnableLineNumbers === true ) { |
|
73
|
|
|
$syntax_highlight .= " line"; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ( $this->syntaxHighlightHighlightedLines !== "" ) { |
|
77
|
|
|
$syntax_highlight .= " highlight=\"". $this->syntaxHighlightHighlightedLines ."\""; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ( $this->syntaxHighlightInlineSource === true ) { |
|
81
|
|
|
$syntax_highlight .= " inline"; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$syntax_highlight .= ">$content</syntaxhighlight>"; |
|
85
|
|
|
$content = $parser->recursiveTagParse( $syntax_highlight, null ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $content; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getFileContent() { |
|
92
|
|
|
try { |
|
93
|
|
|
return $this->fileFetcher->fetchFile( $this->getFileUrl() ); |
|
94
|
|
|
} |
|
95
|
|
|
catch ( FileFetchingException $ex ) { |
|
96
|
|
|
return ''; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getFileUrl() { |
|
101
|
|
|
return sprintf( |
|
102
|
|
|
'%s/%s/%s/%s', |
|
103
|
|
|
$this->gitHubUrl, |
|
104
|
|
|
$this->repoName, |
|
105
|
|
|
$this->branchName, |
|
106
|
|
|
$this->fileName |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function isMarkdownFile() { |
|
111
|
|
|
return $this->fileHasExtension( 'md' ) || $this->fileHasExtension( 'markdown' ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function fileHasExtension( $extension ) { |
|
115
|
|
|
$fullExtension = '.' . $extension; |
|
116
|
|
|
return substr( $this->fileName, -strlen( $fullExtension ) ) === $fullExtension; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function renderAsMarkdown( $content ) { |
|
120
|
|
|
return Markdown::defaultTransform( $content ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|