|
8 | 8 | namespace yii\apidoc\helpers; |
9 | 9 |
|
10 | 10 | use cebe\markdown\GithubMarkdown; |
11 | | -use DomainException; |
12 | | -use Highlight\Highlighter; |
13 | 11 | use yii\apidoc\models\TypeDoc; |
14 | 12 | use yii\apidoc\renderers\BaseRenderer; |
15 | 13 | use yii\helpers\Html; |
|
25 | 23 | class ApiMarkdown extends GithubMarkdown |
26 | 24 | { |
27 | 25 | use ApiMarkdownTrait; |
| 26 | + use MarkdownHighlightTrait; |
28 | 27 |
|
29 | 28 | /** |
30 | 29 | * @var BaseRenderer |
@@ -85,69 +84,6 @@ protected function applyToc($content) |
85 | 84 | return $content; |
86 | 85 | } |
87 | 86 |
|
88 | | - /** |
89 | | - * @var Highlighter |
90 | | - */ |
91 | | - private static $highlighter; |
92 | | - |
93 | | - /** |
94 | | - * @inheritdoc |
95 | | - */ |
96 | | - protected function renderCode($block) |
97 | | - { |
98 | | - if (self::$highlighter === null) { |
99 | | - self::$highlighter = new Highlighter(); |
100 | | - self::$highlighter->setAutodetectLanguages([ |
101 | | - 'apache', 'nginx', |
102 | | - 'bash', 'dockerfile', 'http', |
103 | | - 'css', 'less', 'scss', |
104 | | - 'javascript', 'json', 'markdown', |
105 | | - 'php', 'sql', 'twig', 'xml', |
106 | | - ]); |
107 | | - } |
108 | | - try { |
109 | | - if (isset($block['language'])) { |
110 | | - $result = self::$highlighter->highlight($block['language'], $block['content'] . "\n"); |
111 | | - return "<pre><code class=\"hljs {$result->language} language-{$block['language']}\">{$result->value}</code></pre>\n"; |
112 | | - } else { |
113 | | - $result = self::$highlighter->highlightAuto($block['content'] . "\n"); |
114 | | - return "<pre><code class=\"hljs {$result->language}\">{$result->value}</code></pre>\n"; |
115 | | - } |
116 | | - } catch (DomainException $e) { |
117 | | - echo $e; |
118 | | - return parent::renderCode($block); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Highlights code |
124 | | - * |
125 | | - * @param string $code code to highlight |
126 | | - * @param string $language language of the code to highlight |
127 | | - * @return string HTML of highlighted code |
128 | | - * @deprecated since 2.0.5 this method is not used anymore, highlight.php is used for highlighting |
129 | | - */ |
130 | | - public static function highlight($code, $language) |
131 | | - { |
132 | | - if ($language !== 'php') { |
133 | | - return htmlspecialchars($code, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
134 | | - } |
135 | | - |
136 | | - if (strncmp($code, '<?php', 5) === 0) { |
137 | | - $text = @highlight_string(trim($code), true); |
138 | | - } else { |
139 | | - $text = highlight_string("<?php ".trim($code), true); |
140 | | - $text = str_replace('<?php', '', $text); |
141 | | - if (($pos = strpos($text, ' ')) !== false) { |
142 | | - $text = substr($text, 0, $pos) . substr($text, $pos + 6); |
143 | | - } |
144 | | - } |
145 | | - // remove <code><span style="color: #000000">\n and </span>tags added by php |
146 | | - $text = substr(trim($text), 36, -16); |
147 | | - |
148 | | - return $text; |
149 | | - } |
150 | | - |
151 | 87 | /** |
152 | 88 | * @inheritDoc |
153 | 89 | */ |
|
0 commit comments