Skip to content

Commit 1c512a7

Browse files
committed
extracted markdown code highlighting to a trait
1 parent 4649632 commit 1c512a7

File tree

3 files changed

+85
-65
lines changed

3 files changed

+85
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Yii Framework 2 apidoc extension Change Log
66

77
- Enh #38: Fixed display of default values given as octal or hex notation (hiqsol)
88
- Enh: Display TOC only if there is more than one headline (cebe)
9+
- Enh: Extracted markdown code highlighting to a trait `MarkdownHighlightTrait` (cebe)
910

1011

1112
2.1.0 November 22, 2016

helpers/ApiMarkdown.php

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
namespace yii\apidoc\helpers;
99

1010
use cebe\markdown\GithubMarkdown;
11-
use DomainException;
12-
use Highlight\Highlighter;
1311
use yii\apidoc\models\TypeDoc;
1412
use yii\apidoc\renderers\BaseRenderer;
1513
use yii\helpers\Html;
@@ -25,6 +23,7 @@
2523
class ApiMarkdown extends GithubMarkdown
2624
{
2725
use ApiMarkdownTrait;
26+
use MarkdownHighlightTrait;
2827

2928
/**
3029
* @var BaseRenderer
@@ -85,69 +84,6 @@ protected function applyToc($content)
8584
return $content;
8685
}
8786

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('&lt;?php', '', $text);
141-
if (($pos = strpos($text, '&nbsp;')) !== 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-
15187
/**
15288
* @inheritDoc
15389
*/

helpers/MarkdownHighlightTrait.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* @link http://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license http://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yii\apidoc\helpers;
9+
10+
use DomainException;
11+
use Highlight\Highlighter;
12+
13+
/**
14+
* MarkdownHighlightTrait provides code highlighting functionality for Markdown Parsers.
15+
*
16+
* @since 2.1.1
17+
* @author Carsten Brandt <mail@cebe.cc>
18+
*/
19+
trait MarkdownHighlightTrait
20+
{
21+
/**
22+
* @var Highlighter
23+
*/
24+
private static $highlighter;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function renderCode($block)
30+
{
31+
if (self::$highlighter === null) {
32+
self::$highlighter = new Highlighter();
33+
self::$highlighter->setAutodetectLanguages([
34+
'apache', 'nginx',
35+
'bash', 'dockerfile', 'http',
36+
'css', 'less', 'scss',
37+
'javascript', 'json', 'markdown',
38+
'php', 'sql', 'twig', 'xml',
39+
]);
40+
}
41+
try {
42+
if (isset($block['language'])) {
43+
$result = self::$highlighter->highlight($block['language'], $block['content'] . "\n");
44+
return "<pre><code class=\"hljs {$result->language} language-{$block['language']}\">{$result->value}</code></pre>\n";
45+
} else {
46+
$result = self::$highlighter->highlightAuto($block['content'] . "\n");
47+
return "<pre><code class=\"hljs {$result->language}\">{$result->value}</code></pre>\n";
48+
}
49+
} catch (DomainException $e) {
50+
echo $e;
51+
return parent::renderCode($block);
52+
}
53+
}
54+
55+
/**
56+
* Highlights code
57+
*
58+
* @param string $code code to highlight
59+
* @param string $language language of the code to highlight
60+
* @return string HTML of highlighted code
61+
* @deprecated since 2.0.5 this method is not used anymore, highlight.php is used for highlighting
62+
*/
63+
public static function highlight($code, $language)
64+
{
65+
if ($language !== 'php') {
66+
return htmlspecialchars($code, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
67+
}
68+
69+
if (strncmp($code, '<?php', 5) === 0) {
70+
$text = @highlight_string(trim($code), true);
71+
} else {
72+
$text = highlight_string("<?php ".trim($code), true);
73+
$text = str_replace('&lt;?php', '', $text);
74+
if (($pos = strpos($text, '&nbsp;')) !== false) {
75+
$text = substr($text, 0, $pos) . substr($text, $pos + 6);
76+
}
77+
}
78+
// remove <code><span style="color: #000000">\n and </span>tags added by php
79+
$text = substr(trim($text), 36, -16);
80+
81+
return $text;
82+
}
83+
}

0 commit comments

Comments
 (0)