1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the MesHighlightBundle package. |
||
5 | * |
||
6 | * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Mes\Misc\HighlightBundle; |
||
13 | |||
14 | /** |
||
15 | * Class Highlighter. |
||
16 | */ |
||
17 | class Highlighter implements HighlighterInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var \Highlight\Highlighter |
||
21 | */ |
||
22 | private $h; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $root; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $leftDelimiter; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $rightDelimiter; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $supportedLanguages = array(); |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $showLines = true; |
||
48 | |||
49 | /** |
||
50 | * Highlighter constructor. |
||
51 | * |
||
52 | * @param array $languages |
||
53 | * @param $root |
||
54 | * @param $leftDelimiter |
||
55 | * @param $rightDelimiter |
||
56 | */ |
||
57 | 1 | public function __construct(array $languages, $root, $leftDelimiter, $rightDelimiter) |
|
58 | { |
||
59 | 1 | $this->leftDelimiter = $leftDelimiter; |
|
60 | 1 | $this->rightDelimiter = $rightDelimiter; |
|
61 | 1 | $this->root = $root; |
|
62 | |||
63 | 1 | $this->h = new \Highlight\Highlighter(); |
|
64 | |||
65 | // Set the languages you want to detect automatically. |
||
66 | 1 | $this->addSupportedLanguages($languages); |
|
67 | 1 | } |
|
68 | |||
69 | /** |
||
70 | * @param $content |
||
71 | * @param null $language |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | 1 | public function searchPatternAndHighlight($content, $language = null) |
|
76 | { |
||
77 | 1 | $pattern = '#(?:'.$this->leftDelimiter.')([\S|\s]+)(?:'.$this->rightDelimiter.')#iU'; |
|
78 | |||
79 | return preg_replace_callback($pattern, function ($matches) use ($language) { |
||
80 | 1 | return $this->highlight($this->resolveResource(trim($matches[1])), $language); |
|
81 | 1 | }, $content); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param $content |
||
86 | * @param null $language |
||
87 | * |
||
88 | * @return string|void |
||
89 | */ |
||
90 | 1 | public function highlight($content, $language = null) |
|
91 | { |
||
92 | // Highlight some code. |
||
93 | 1 | $r = null === $language ? $this->h->highlightAuto($content) : $this->h->highlight($language, $content); |
|
94 | |||
95 | 1 | if (!($value = $r->value)) { |
|
96 | return; |
||
97 | } |
||
98 | |||
99 | 1 | if ($this->showLines) { |
|
100 | 1 | $lines = preg_split("/\r\n|\r|\n/", $value); |
|
101 | |||
102 | 1 | array_walk($lines, function (&$line, $index) { |
|
103 | $line = <<<HTML |
||
104 | 1 | <div class="line"><span class="linenum">$index</span>$line</div> |
|
105 | HTML; |
||
106 | 1 | }); |
|
107 | |||
108 | 1 | $value = implode('', $lines); |
|
109 | } |
||
110 | |||
111 | return <<<HTML |
||
112 | 1 | <pre class="hljs $r->language">$value</pre> |
|
113 | HTML; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Set root location for "code files". |
||
118 | * |
||
119 | * @param $rootPath |
||
120 | */ |
||
121 | 1 | public function setRootPath($rootPath) |
|
122 | { |
||
123 | 1 | if ('/' !== substr($rootPath, -1)) { |
|
124 | 1 | $rootPath .= '/'; |
|
125 | } |
||
126 | |||
127 | 1 | $this->root = $rootPath; |
|
128 | 1 | } |
|
129 | |||
130 | /** |
||
131 | * Add supported languages for highlighting. |
||
132 | * |
||
133 | * @param array $supportedLanguages |
||
134 | */ |
||
135 | 1 | public function addSupportedLanguages(array $supportedLanguages) |
|
136 | { |
||
137 | 1 | $this->supportedLanguages = array_merge($this->supportedLanguages, $supportedLanguages); |
|
138 | |||
139 | 1 | $this->h->setAutodetectLanguages($this->supportedLanguages); |
|
140 | 1 | } |
|
141 | |||
142 | /** |
||
143 | * @param $resource |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 1 | protected function resolveResource($resource) |
|
148 | { |
||
149 | 1 | if (is_file($this->root.$resource)) { |
|
150 | 1 | $resource = file_get_contents($this->root.$resource); |
|
151 | } |
||
152 | |||
153 | 1 | return trim($resource); |
|
154 | } |
||
155 | } |
||
156 |