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
|
|
|
return $this->renderAsMarkdown( $content ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( $this->syntaxHighlightLanguage !== "" ) { |
70
|
|
|
$syntax_highlight = "<syntaxhighlight lang=\"". $this->syntaxHighlightLanguage ."\""; |
71
|
|
|
$syntax_highlight .= " start=\"". $this->syntaxHighlightStartingLineNumber ."\""; |
72
|
|
|
|
73
|
|
|
if ( $this->syntaxHighlightEnableLineNumbers === true ) { |
74
|
|
|
$syntax_highlight .= " line"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ( $this->syntaxHighlightHighlightedLines !== "" ) { |
78
|
|
|
$syntax_highlight .= " highlight=\"". $this->syntaxHighlightHighlightedLines ."\""; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( $this->syntaxHighlightInlineSource === true ) { |
82
|
|
|
$syntax_highlight .= " inline"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$syntax_highlight .= ">$content</syntaxhighlight>"; |
86
|
|
|
return $parser->recursiveTagParse( $syntax_highlight, null ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $content; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getFileContent() { |
93
|
|
|
try { |
94
|
|
|
return $this->fileFetcher->fetchFile( $this->getFileUrl() ); |
95
|
|
|
} |
96
|
|
|
catch ( FileFetchingException $ex ) { |
97
|
|
|
return ''; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getFileUrl() { |
102
|
|
|
return sprintf( |
103
|
|
|
'%s/%s/%s/%s', |
104
|
|
|
$this->gitHubUrl, |
105
|
|
|
$this->repoName, |
106
|
|
|
$this->branchName, |
107
|
|
|
$this->fileName |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function isMarkdownFile() { |
112
|
|
|
return $this->fileHasExtension( 'md' ) || $this->fileHasExtension( 'markdown' ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function fileHasExtension( $extension ) { |
116
|
|
|
$fullExtension = '.' . $extension; |
117
|
|
|
return substr( $this->fileName, -strlen( $fullExtension ) ) === $fullExtension; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function renderAsMarkdown( $content ) { |
121
|
|
|
return Markdown::defaultTransform( $content ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|