1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GitHub; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @licence GNU GPL v2+ |
7
|
|
|
* @author Jeroen De Dauw < [email protected] > |
8
|
|
|
*/ |
9
|
|
|
class SyntaxRenderer { |
10
|
|
|
|
11
|
|
|
private $recursiveTagParseFunction; |
12
|
|
|
|
13
|
|
|
// Parameters for SyntaxHighlight extension (formerly SyntaxHighlight_GeSHi) |
14
|
|
|
// https://www.mediawiki.org/wiki/Extension:SyntaxHighlight |
15
|
|
|
private $language; |
16
|
|
|
private $enableLineNumbers; |
17
|
|
|
private $startingLineNumber; |
18
|
|
|
private $highlightedLines; |
19
|
|
|
private $inlineSource; |
20
|
|
|
|
21
|
|
|
public function __construct( callable $recursiveTagParseFunction, string $language, |
22
|
|
|
bool $enableLineNumbers, int $startingLineNumber, string $highlightedLines, bool $inlineSource ) { |
23
|
|
|
|
24
|
|
|
$this->recursiveTagParseFunction = $recursiveTagParseFunction; |
25
|
|
|
$this->language = $language; |
26
|
|
|
$this->enableLineNumbers = $enableLineNumbers; |
27
|
|
|
$this->startingLineNumber = $startingLineNumber; |
28
|
|
|
$this->highlightedLines = $highlightedLines; |
29
|
|
|
$this->inlineSource = $inlineSource; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getRenderedContent( string $content ): string { |
33
|
|
|
$parsed = ($this->recursiveTagParseFunction)( $this->buildSyntaxTag( $content ) ); |
34
|
|
|
|
35
|
|
|
if ( is_string( $parsed ) ) { |
36
|
|
|
return $parsed; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return ''; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function buildSyntaxTag( $content ): string { |
43
|
|
|
return \Html::rawElement( |
44
|
|
|
'syntaxhighlight', |
45
|
|
|
$this->getTagAttributes(), |
46
|
|
|
$content |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function getTagAttributes(): array { |
51
|
|
|
$attributes = [ |
52
|
|
|
'lang' => $this->language, |
53
|
|
|
'start' => $this->startingLineNumber, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
if ( $this->enableLineNumbers ) { |
57
|
|
|
$attributes['line'] = true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ( $this->highlightedLines !== '' ) { |
61
|
|
|
$attributes['highlight'] = $this->highlightedLines; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ( $this->inlineSource ) { |
65
|
|
|
$attributes['inline'] = true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $attributes; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|