-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCodeNodeRenderer.php
148 lines (122 loc) · 4.76 KB
/
CodeNodeRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
/*
* This file is part of the Docs Builder package.
* (c) Ryan Weaver <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyDocsBuilder\Renderers;
use Doctrine\RST\Nodes\CodeNode;
use Doctrine\RST\Renderers\NodeRenderer;
use Doctrine\RST\Templates\TemplateRenderer;
use Highlight\Highlighter;
class CodeNodeRenderer implements NodeRenderer
{
private static $isHighlighterConfigured = false;
private const LANGUAGES_MAPPING = [
'env' => 'bash',
'html+jinja' => 'twig',
'html+twig' => 'twig',
'jinja' => 'twig',
'html+php' => 'html',
'xml+php' => 'xml',
'php-annotations' => 'php',
'php-attributes' => 'php',
'terminal' => 'bash',
'rst' => 'markdown',
'php-standalone' => 'php',
'php-symfony' => 'php',
'varnish4' => 'c',
'varnish3' => 'c',
'vcl' => 'c',
];
/** @var CodeNode */
private $codeNode;
/** @var TemplateRenderer */
private $templateRenderer;
public function __construct(CodeNode $codeNode, TemplateRenderer $templateRenderer)
{
$this->codeNode = $codeNode;
$this->templateRenderer = $templateRenderer;
}
public function render(): string
{
$code = trim($this->codeNode->getValue());
if ($this->codeNode->isRaw()) {
return $code;
}
$language = $this->codeNode->getLanguage() ?? 'php';
$languageMapping = self::LANGUAGES_MAPPING[$language] ?? $language;
$languages = array_unique([$language, $languageMapping]);
if ('text' === $language) {
$highlightedCode = $code;
} else {
$this->configureHighlighter();
$highLighter = new Highlighter();
$highlightedCode = $highLighter->highlight($languageMapping, $code)->value;
// this allows to highlight the $ in PHP variable names
$highlightedCode = str_replace('<span class="hljs-variable">$', '<span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>', $highlightedCode);
}
if ('terminal' === $language) {
$highlightedCode = preg_replace('/^\$ /m', '<span class="hljs-prompt">$ </span>', $highlightedCode);
$highlightedCode = preg_replace('/^C:\\\> /m', '<span class="hljs-prompt">C:\> </span>', $highlightedCode);
}
if ('diff' === $language) {
// remove the '+' and '-' signs so they can be added with css
$highlightedCode = str_replace([
'<span class="hljs-deletion">-',
'<span class="hljs-addition">+',
], [
'<span class="hljs-deletion"> ',
'<span class="hljs-addition"> ',
], $highlightedCode);
}
$numOfLines = \count(preg_split('/\r\n|\r|\n/', $highlightedCode));
$lineNumbers = implode("\n", range(1, $numOfLines));
return $this->templateRenderer->render(
'code.html.twig',
[
'languages' => $languages,
'line_numbers' => $lineNumbers,
'code' => $highlightedCode,
'loc' => $numOfLines,
// the length of the codeblock in a semantic way (to tweak styling)
// e.g. LOC = 5, length = 'sm'; LOC = 18, length = 'md'
'length' => [1 => 'sm', 2 => 'md', 3 => 'lg', 4 => 'xl'][strlen((string) $numOfLines)],
]
);
}
public static function isLanguageSupported(string $lang): bool
{
$highlighter = new Highlighter();
$supportedLanguages = array_merge(
array_keys(self::LANGUAGES_MAPPING),
$highlighter->listRegisteredLanguages(true),
// not highlighted, but valid
['text']
);
return \in_array($lang, $supportedLanguages, true);
}
private function getLines(string $code): array
{
$lines = preg_split('/\r\n|\r|\n/', $code);
$reversedLines = array_reverse($lines);
// trim empty lines at the end of the code
foreach ($reversedLines as $key => $line) {
if ('' !== trim($line)) {
break;
}
unset($reversedLines[$key]);
}
return array_reverse($reversedLines);
}
private function configureHighlighter()
{
if (false === self::$isHighlighterConfigured) {
Highlighter::registerLanguage('php', __DIR__.'/../Templates/highlight.php/php.json', true);
Highlighter::registerLanguage('twig', __DIR__.'/../Templates/highlight.php/twig.json', true);
}
self::$isHighlighterConfigured = true;
}
}