1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\CommonMarkHighlighter; |
4
|
|
|
|
5
|
|
|
use DomainException; |
6
|
|
|
use Highlight\Highlighter; |
7
|
|
|
use function HighlightUtilities\splitCodeIntoArray; |
8
|
|
|
|
9
|
|
|
class CodeBlockHighlighter |
10
|
|
|
{ |
11
|
|
|
/** @var \Highlight\Highlighter */ |
12
|
|
|
protected $highlighter; |
13
|
|
|
|
14
|
|
|
public function __construct(array $autodetectLanguages = []) |
15
|
|
|
{ |
16
|
|
|
$this->highlighter = new Highlighter(); |
17
|
|
|
|
18
|
|
|
$this->highlighter->setAutodetectLanguages($autodetectLanguages); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function highlight(string $codeBlock, ?string $infoLine = null) |
22
|
|
|
{ |
23
|
|
|
$codeBlockWithoutTags = strip_tags($codeBlock); |
24
|
|
|
$contents = htmlspecialchars_decode($codeBlockWithoutTags); |
25
|
|
|
|
26
|
|
|
$definition = $this->parseLangAndLines($infoLine); |
27
|
|
|
$language = $definition['lang']; |
28
|
|
|
|
29
|
|
|
try { |
30
|
|
|
$result = $language |
31
|
|
|
? $this->highlighter->highlight($language, $contents) |
32
|
|
|
: $this->highlighter->highlightAuto($contents); |
33
|
|
|
|
34
|
|
|
$code = $result->value; |
35
|
|
|
|
36
|
|
|
if (count($definition['lines']) > 0) { |
37
|
|
|
$loc = splitCodeIntoArray($code); |
38
|
|
|
|
39
|
|
|
foreach ($loc as $i => $line) { |
40
|
|
|
$loc[$i] = vsprintf('<span class="loc%s">%s</span>', [ |
41
|
|
|
isset($definition['lines'][$i + 1]) ? ' highlighted' : '', |
42
|
|
|
$line, |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$code = implode("\n", $loc); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return vsprintf('<code class="%s hljs %s" data-lang="%s">%s</code>', [ |
50
|
|
|
'language-'.($language ? $language : $result->language), |
51
|
|
|
$result->language, |
52
|
|
|
$language ? $language : $result->language, |
53
|
|
|
$code, |
54
|
|
|
]); |
55
|
|
|
} catch (DomainException $e) { |
56
|
|
|
return $codeBlock; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function parseLangAndLines(?string $language) |
61
|
|
|
{ |
62
|
|
|
$parsed = [ |
63
|
|
|
'lang' => $language, |
64
|
|
|
'lines' => [], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
if ($language === null) { |
68
|
|
|
return $parsed; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$bracePos = strpos($language, '{'); |
72
|
|
|
|
73
|
|
|
if ($bracePos === false) { |
74
|
|
|
return $parsed; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$parsed['lang'] = substr($language, 0, $bracePos); |
78
|
|
|
$lineDef = substr($language, $bracePos + 1, -1); |
79
|
|
|
$lineNums = explode(',', $lineDef); |
80
|
|
|
|
81
|
|
|
foreach ($lineNums as $lineNum) { |
82
|
|
|
if (strpos($lineNum, '-') === false) { |
83
|
|
|
$parsed['lines'][intval($lineNum)] = true; |
84
|
|
|
|
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$extremes = explode('-', $lineNum); |
89
|
|
|
|
90
|
|
|
if (count($extremes) !== 2) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$start = intval($extremes[0]); |
95
|
|
|
$end = intval($extremes[1]); |
96
|
|
|
|
97
|
|
|
for ($i = $start; $i <= $end; $i++) { |
98
|
|
|
$parsed['lines'][$i] = true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $parsed; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|