Trait yii\apidoc\helpers\MarkdownHighlightTrait
Implemented by | yii\apidoc\helpers\ApiMarkdown |
---|---|
Available since extension's version | 2.0.7 |
Source Code | https://github.com/yiisoft/yii2-apidoc/blob/master/helpers/MarkdownHighlightTrait.php |
MarkdownHighlightTrait provides code highlighting functionality for Markdown Parsers.
Public Methods
Method | Description | Defined By |
---|---|---|
highlight() | Highlights code | yii\apidoc\helpers\MarkdownHighlightTrait |
Protected Methods
Method | Description | Defined By |
---|---|---|
renderCode() | yii\apidoc\helpers\MarkdownHighlightTrait |
Method Details
Deprecated
since 2.0.5 this method is not used anymore, highlight.php is used for highlighting
Highlights code
public static string highlight ( $code, $language ) | ||
$code | string |
Code to highlight |
$language | string |
Language of the code to highlight |
return | string |
HTML of highlighted code |
---|
public static function highlight($code, $language)
{
if ($language !== 'php') {
return htmlspecialchars($code, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
if (strncmp($code, '<?php', 5) === 0) {
$text = @highlight_string(trim($code), true);
} else {
$text = highlight_string("<?php ".trim($code), true);
$text = str_replace('<?php', '', $text);
if (($pos = strpos($text, ' ')) !== false) {
$text = substr($text, 0, $pos) . substr($text, $pos + 6);
}
}
// remove <code><span style="color: #000000">\n and </span>tags added by php
$text = substr(trim($text), 36, -16);
return $text;
}
protected void renderCode ( $block ) | ||
$block |
protected function renderCode($block)
{
if (self::$highlighter === null) {
self::$highlighter = new Highlighter();
self::$highlighter->setAutodetectLanguages([
'apache', 'nginx',
'bash', 'dockerfile', 'http',
'css', 'less', 'scss',
'javascript', 'json', 'markdown',
'php', 'sql', 'twig', 'xml',
]);
}
try {
if (isset($block['language'])) {
$result = self::$highlighter->highlight($block['language'], $block['content'] . "\n");
return "<pre><code class=\"hljs {$result->language} language-{$block['language']}\">{$result->value}</code></pre>\n";
} else {
$result = self::$highlighter->highlightAuto($block['content'] . "\n");
return "<pre><code class=\"hljs {$result->language}\">{$result->value}</code></pre>\n";
}
} catch (DomainException $e) {
echo $e;
return parent::renderCode($block);
}
}