|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GitHub; |
|
4
|
|
|
|
|
5
|
|
|
use ExtensionRegistry; |
|
6
|
|
|
use FileFetcher\FileFetcher; |
|
7
|
|
|
use FileFetcher\FileFetchingException; |
|
8
|
|
|
use Michelf\Markdown; |
|
9
|
|
|
use ParamProcessor\ProcessingResult; |
|
10
|
|
|
use Parser; |
|
11
|
|
|
use ParserHooks\HookHandler; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @licence GNU GPL v2+ |
|
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
16
|
|
|
*/ |
|
17
|
|
|
class GitHubParserHook implements HookHandler { |
|
18
|
|
|
|
|
19
|
|
|
private $fileFetcher; |
|
20
|
|
|
private $gitHubUrl; |
|
21
|
|
|
|
|
22
|
|
|
private $fileName; |
|
23
|
|
|
private $repoName; |
|
24
|
|
|
private $branchName; |
|
25
|
|
|
|
|
26
|
|
|
// Parameters for SyntaxHighlight extension (formerly SyntaxHighlight_GeSHi) |
|
27
|
|
|
private $lang; |
|
28
|
|
|
private $line; |
|
29
|
|
|
private $start; |
|
30
|
|
|
private $highlight; |
|
31
|
|
|
private $inline; |
|
32
|
|
|
|
|
33
|
|
|
private static $syntaxHighlightFields = array('lang', 'line', 'start', 'highlight', 'inline'); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param FileFetcher $fileFetcher |
|
37
|
|
|
* @param string $gitHubUrl |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( FileFetcher $fileFetcher, $gitHubUrl ) { |
|
40
|
|
|
$this->fileFetcher = $fileFetcher; |
|
41
|
|
|
$this->gitHubUrl = $gitHubUrl; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function handle( Parser $parser, ProcessingResult $result ) { |
|
45
|
|
|
$this->setFields( $result ); |
|
46
|
|
|
|
|
47
|
|
|
return $this->getRenderedContent($parser); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function setFields( ProcessingResult $result ) { |
|
51
|
|
|
$params = $result->getParameters(); |
|
52
|
|
|
|
|
53
|
|
|
$this->fileName = $params['file']->getValue(); |
|
54
|
|
|
$this->repoName = $params['repo']->getValue(); |
|
55
|
|
|
$this->branchName = $params['branch']->getValue(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ( self::$syntaxHighlightFields as $val ) { |
|
58
|
|
|
if ( isset( $params[$val] ) ) { |
|
59
|
|
|
$this->$val = $this->cleanField( $params[$val]->getValue() ); |
|
60
|
|
|
} |
|
61
|
|
|
else { |
|
62
|
|
|
$this->$val = null; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function cleanField( $val ) { |
|
68
|
|
|
if ( $val !== null ) { |
|
69
|
|
|
$val = trim( $val, "'\"" ); |
|
70
|
|
|
} |
|
71
|
|
|
return $val; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function getRenderedContent(Parser $parser) { |
|
75
|
|
|
$content = $this->getFileContent(); |
|
76
|
|
|
|
|
77
|
|
|
if ( $this->isMarkdownFile() ) { |
|
78
|
|
|
$content = $this->renderAsMarkdown( $content ); |
|
79
|
|
|
} |
|
80
|
|
|
else if ($this->lang !== "") { |
|
81
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'SyntaxHighlight' ) ) { |
|
82
|
|
|
// Use SyntaxHighlight specifically |
|
83
|
|
|
$tag = "syntaxhighlight"; |
|
84
|
|
|
} |
|
85
|
|
|
else { |
|
86
|
|
|
// Some other extensions also watch for this |
|
87
|
|
|
$tag = "source"; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$syntax_highlight = "<$tag lang=\"". $this->lang ."\""; |
|
91
|
|
|
$syntax_highlight .= " start=\"". $this->start ."\""; |
|
92
|
|
|
|
|
93
|
|
|
if ( $this->line !== null ) { |
|
94
|
|
|
$syntax_highlight .= " line"; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ( $this->highlight !== "" ) { |
|
98
|
|
|
$syntax_highlight .= " highlight=\"". $this->highlight ."\""; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ( $this->inline !== null ) { |
|
102
|
|
|
$syntax_highlight .= " inline"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$syntax_highlight .= ">$content</$tag>"; |
|
106
|
|
|
$content = $parser->recursiveTagParse( $syntax_highlight, null ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $content; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function getFileContent() { |
|
113
|
|
|
try { |
|
114
|
|
|
return $this->fileFetcher->fetchFile( $this->getFileUrl() ); |
|
115
|
|
|
} |
|
116
|
|
|
catch ( FileFetchingException $ex ) { |
|
117
|
|
|
return ''; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function getFileUrl() { |
|
122
|
|
|
return sprintf( |
|
123
|
|
|
'%s/%s/%s/%s', |
|
124
|
|
|
$this->gitHubUrl, |
|
125
|
|
|
$this->repoName, |
|
126
|
|
|
$this->branchName, |
|
127
|
|
|
$this->fileName |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
private function isMarkdownFile() { |
|
132
|
|
|
return $this->fileHasExtension( 'md' ) || $this->fileHasExtension( 'markdown' ); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private function fileHasExtension( $extension ) { |
|
136
|
|
|
$fullExtension = '.' . $extension; |
|
137
|
|
|
return substr( $this->fileName, -strlen( $fullExtension ) ) === $fullExtension; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function renderAsMarkdown( $content ) { |
|
141
|
|
|
return Markdown::defaultTransform( $content ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|