Highlighter.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. /* Copyright (c)
  3. * - 2006-2013, Ivan Sagalaev (maniac@softwaremaniacs.org), highlight.js
  4. * (original author)
  5. * - 2013-2019, Geert Bergman (geert@scrivo.nl), highlight.php
  6. * - 2014 Daniel Lynge, highlight.php (contributor)
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  17. * contributors may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Highlight;
  33. /**
  34. * @api
  35. *
  36. * @since 7.5.0.0
  37. */
  38. class Highlighter
  39. {
  40. /**
  41. * @since 9.12.0.4
  42. */
  43. const SPAN_END_TAG = "</span>";
  44. /** @var bool */
  45. private $safeMode = true;
  46. // @TODO In v10.x, this value should be static to match highlight.js behavior
  47. /** @var array<string, mixed> */
  48. private $options;
  49. /** @var string */
  50. private $modeBuffer = "";
  51. /** @var string */
  52. private $result = "";
  53. /** @var Mode|null */
  54. private $top = null;
  55. /** @var Language|null */
  56. private $language = null;
  57. /** @var int */
  58. private $relevance = 0;
  59. /** @var bool */
  60. private $ignoreIllegals = false;
  61. /** @var array<string, Mode> */
  62. private $continuations = array();
  63. /** @var RegExMatch */
  64. private $lastMatch;
  65. /** @var string The current code we are highlighting */
  66. private $codeToHighlight;
  67. /** @var array<string, Language> A mapping of a language ID to a Language definition */
  68. private static $classMap = array();
  69. /** @var string[] A list of registered language IDs */
  70. private static $languages = array();
  71. /** @var array<string, string> A mapping from alias (key) to main language ID (value) */
  72. private static $aliases = array();
  73. public function __construct()
  74. {
  75. $this->lastMatch = new RegExMatch(array());
  76. $this->lastMatch->type = "";
  77. $this->lastMatch->rule = null;
  78. // @TODO In v10.x, remove the default value for the `languages` value to follow highlight.js behavior
  79. $this->options = array(
  80. 'classPrefix' => 'hljs-',
  81. 'tabReplace' => null,
  82. 'useBR' => false,
  83. 'languages' => array(
  84. "xml", "json", "javascript", "css", "php", "http",
  85. ),
  86. );
  87. self::registerLanguages();
  88. }
  89. /**
  90. * Loop through all of the languages in our `languages` folder and automatically register them all.
  91. *
  92. * @since 8.3.0.0
  93. *
  94. * @return void
  95. */
  96. private static function registerLanguages()
  97. {
  98. // Languages that take precedence in the classMap array.
  99. $languagePath = __DIR__ . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR;
  100. foreach (array("xml", "django", "javascript", "matlab", "cpp") as $languageId) {
  101. $filePath = $languagePath . $languageId . ".json";
  102. if (is_readable($filePath)) {
  103. self::registerLanguage($languageId, $filePath);
  104. }
  105. }
  106. $d = @dir($languagePath);
  107. if ($d) {
  108. while (($entry = $d->read()) !== false) {
  109. if (substr($entry, -5) === ".json") {
  110. $languageId = substr($entry, 0, -5);
  111. $filePath = $languagePath . $entry;
  112. if (is_readable($filePath)) {
  113. self::registerLanguage($languageId, $filePath);
  114. }
  115. }
  116. }
  117. $d->close();
  118. }
  119. self::$languages = array_keys(self::$classMap);
  120. }
  121. /**
  122. * Register a language definition with the Highlighter's internal language
  123. * storage. Languages are stored in a static variable, so they'll be available
  124. * across all instances. You only need to register a language once.
  125. *
  126. * @param string $languageId The unique name of a language
  127. * @param string $filePath The file path to the language definition
  128. * @param bool $overwrite Overwrite language if it already exists
  129. *
  130. * @return Language The object containing the definition for a language's markup
  131. */
  132. public static function registerLanguage($languageId, $filePath, $overwrite = false)
  133. {
  134. if (!isset(self::$classMap[$languageId]) || $overwrite) {
  135. $lang = new Language($languageId, $filePath);
  136. self::$classMap[$languageId] = $lang;
  137. if ($lang->aliases) {
  138. foreach ($lang->aliases as $alias) {
  139. self::$aliases[$alias] = $languageId;
  140. }
  141. }
  142. }
  143. return self::$classMap[$languageId];
  144. }
  145. /**
  146. * @param RegEx|null $re
  147. * @param string $lexeme
  148. *
  149. * @return bool
  150. */
  151. private function testRe($re, $lexeme)
  152. {
  153. if (!$re) {
  154. return false;
  155. }
  156. $lastIndex = $re->lastIndex;
  157. $result = $re->exec($lexeme);
  158. $re->lastIndex = $lastIndex;
  159. return $result && $result->index === 0;
  160. }
  161. /**
  162. * @param string $value
  163. *
  164. * @return RegEx
  165. */
  166. private function escapeRe($value)
  167. {
  168. return new RegEx(sprintf('/%s/um', preg_quote($value)));
  169. }
  170. /**
  171. * @param Mode $mode
  172. * @param string $lexeme
  173. *
  174. * @return Mode|null
  175. */
  176. private function endOfMode($mode, $lexeme)
  177. {
  178. if ($this->testRe($mode->endRe, $lexeme)) {
  179. while ($mode->endsParent && $mode->parent) {
  180. $mode = $mode->parent;
  181. }
  182. return $mode;
  183. }
  184. if ($mode->endsWithParent) {
  185. return $this->endOfMode($mode->parent, $lexeme);
  186. }
  187. return null;
  188. }
  189. /**
  190. * @param Mode $mode
  191. * @param RegExMatch $match
  192. *
  193. * @return mixed|null
  194. */
  195. private function keywordMatch($mode, $match)
  196. {
  197. $kwd = $this->language->case_insensitive ? mb_strtolower($match[0], "UTF-8") : $match[0];
  198. return isset($mode->keywords[$kwd]) ? $mode->keywords[$kwd] : null;
  199. }
  200. /**
  201. * @param string $className
  202. * @param string $insideSpan
  203. * @param bool $leaveOpen
  204. * @param bool $noPrefix
  205. *
  206. * @return string
  207. */
  208. private function buildSpan($className, $insideSpan, $leaveOpen = false, $noPrefix = false)
  209. {
  210. if (!$leaveOpen && $insideSpan === '') {
  211. return '';
  212. }
  213. if (!$className) {
  214. return $insideSpan;
  215. }
  216. $classPrefix = $noPrefix ? "" : $this->options['classPrefix'];
  217. $openSpan = "<span class=\"" . $classPrefix;
  218. $closeSpan = $leaveOpen ? "" : self::SPAN_END_TAG;
  219. $openSpan .= $className . "\">";
  220. return $openSpan . $insideSpan . $closeSpan;
  221. }
  222. /**
  223. * @param string $value
  224. *
  225. * @return string
  226. */
  227. private function escape($value)
  228. {
  229. return htmlspecialchars($value, ENT_NOQUOTES);
  230. }
  231. /**
  232. * @return string
  233. */
  234. private function processKeywords()
  235. {
  236. if (!$this->top->keywords) {
  237. return $this->escape($this->modeBuffer);
  238. }
  239. $result = "";
  240. $lastIndex = 0;
  241. $this->top->lexemesRe->lastIndex = 0;
  242. $match = $this->top->lexemesRe->exec($this->modeBuffer);
  243. while ($match) {
  244. $result .= $this->escape(substr($this->modeBuffer, $lastIndex, $match->index - $lastIndex));
  245. $keyword_match = $this->keywordMatch($this->top, $match);
  246. if ($keyword_match) {
  247. $this->relevance += $keyword_match[1];
  248. $result .= $this->buildSpan($keyword_match[0], $this->escape($match[0]));
  249. } else {
  250. $result .= $this->escape($match[0]);
  251. }
  252. $lastIndex = $this->top->lexemesRe->lastIndex;
  253. $match = $this->top->lexemesRe->exec($this->modeBuffer);
  254. }
  255. return $result . $this->escape(substr($this->modeBuffer, $lastIndex));
  256. }
  257. /**
  258. * @return string
  259. */
  260. private function processSubLanguage()
  261. {
  262. try {
  263. $hl = new Highlighter();
  264. // @TODO in v10.x, this should no longer be necessary once `$options` is made static
  265. $hl->setAutodetectLanguages($this->options['languages']);
  266. $hl->setClassPrefix($this->options['classPrefix']);
  267. $hl->setTabReplace($this->options['tabReplace']);
  268. if (!$this->safeMode) {
  269. $hl->disableSafeMode();
  270. }
  271. $explicit = is_string($this->top->subLanguage);
  272. if ($explicit && !in_array($this->top->subLanguage, self::$languages)) {
  273. return $this->escape($this->modeBuffer);
  274. }
  275. if ($explicit) {
  276. $res = $hl->highlight(
  277. $this->top->subLanguage,
  278. $this->modeBuffer,
  279. true,
  280. isset($this->continuations[$this->top->subLanguage]) ? $this->continuations[$this->top->subLanguage] : null
  281. );
  282. } else {
  283. $res = $hl->highlightAuto(
  284. $this->modeBuffer,
  285. count($this->top->subLanguage) ? $this->top->subLanguage : null
  286. );
  287. }
  288. // Counting embedded language score towards the host language may be disabled
  289. // with zeroing the containing mode relevance. Use case in point is Markdown that
  290. // allows XML everywhere and makes every XML snippet to have a much larger Markdown
  291. // score.
  292. if ($this->top->relevance > 0) {
  293. $this->relevance += $res->relevance;
  294. }
  295. if ($explicit) {
  296. $this->continuations[$this->top->subLanguage] = $res->top;
  297. }
  298. return $this->buildSpan($res->language, $res->value, false, true);
  299. } catch (\Exception $e) {
  300. return $this->escape($this->modeBuffer);
  301. }
  302. }
  303. /**
  304. * @return void
  305. */
  306. private function processBuffer()
  307. {
  308. if (is_object($this->top) && $this->top->subLanguage) {
  309. $this->result .= $this->processSubLanguage();
  310. } else {
  311. $this->result .= $this->processKeywords();
  312. }
  313. $this->modeBuffer = '';
  314. }
  315. /**
  316. * @param Mode $mode
  317. *
  318. * @return void
  319. */
  320. private function startNewMode($mode)
  321. {
  322. $this->result .= $mode->className ? $this->buildSpan($mode->className, "", true) : "";
  323. $t = clone $mode;
  324. $t->parent = $this->top;
  325. $this->top = $t;
  326. }
  327. /**
  328. * @param RegExMatch $match
  329. *
  330. * @return int
  331. */
  332. private function doBeginMatch($match)
  333. {
  334. $lexeme = $match[0];
  335. $newMode = $match->rule;
  336. if ($newMode && $newMode->endSameAsBegin) {
  337. $newMode->endRe = $this->escapeRe($lexeme);
  338. }
  339. if ($newMode->skip) {
  340. $this->modeBuffer .= $lexeme;
  341. } else {
  342. if ($newMode->excludeBegin) {
  343. $this->modeBuffer .= $lexeme;
  344. }
  345. $this->processBuffer();
  346. if (!$newMode->returnBegin && !$newMode->excludeBegin) {
  347. $this->modeBuffer = $lexeme;
  348. }
  349. }
  350. $this->startNewMode($newMode);
  351. return $newMode->returnBegin ? 0 : strlen($lexeme);
  352. }
  353. /**
  354. * @param RegExMatch $match
  355. *
  356. * @return int|null
  357. */
  358. private function doEndMatch($match)
  359. {
  360. $lexeme = $match[0];
  361. $matchPlusRemainder = substr($this->codeToHighlight, $match->index);
  362. $endMode = $this->endOfMode($this->top, $matchPlusRemainder);
  363. if (!$endMode) {
  364. return null;
  365. }
  366. $origin = $this->top;
  367. if ($origin->skip) {
  368. $this->modeBuffer .= $lexeme;
  369. } else {
  370. if (!($origin->returnEnd || $origin->excludeEnd)) {
  371. $this->modeBuffer .= $lexeme;
  372. }
  373. $this->processBuffer();
  374. if ($origin->excludeEnd) {
  375. $this->modeBuffer = $lexeme;
  376. }
  377. }
  378. do {
  379. if ($this->top->className) {
  380. $this->result .= self::SPAN_END_TAG;
  381. }
  382. if (!$this->top->skip && !$this->top->subLanguage) {
  383. $this->relevance += $this->top->relevance;
  384. }
  385. $this->top = $this->top->parent;
  386. } while ($this->top !== $endMode->parent);
  387. if ($endMode->starts) {
  388. if ($endMode->endSameAsBegin) {
  389. $endMode->starts->endRe = $endMode->endRe;
  390. }
  391. $this->startNewMode($endMode->starts);
  392. }
  393. return $origin->returnEnd ? 0 : strlen($lexeme);
  394. }
  395. /**
  396. * @param string $textBeforeMatch
  397. * @param RegExMatch|null $match
  398. *
  399. * @return int
  400. */
  401. private function processLexeme($textBeforeMatch, $match = null)
  402. {
  403. $lexeme = $match ? $match[0] : null;
  404. // add non-matched text to the current mode buffer
  405. $this->modeBuffer .= $textBeforeMatch;
  406. if ($lexeme === null) {
  407. $this->processBuffer();
  408. return 0;
  409. }
  410. // we've found a 0 width match and we're stuck, so we need to advance
  411. // this happens when we have badly behaved rules that have optional matchers to the degree that
  412. // sometimes they can end up matching nothing at all
  413. // Ref: https://github.com/highlightjs/highlight.js/issues/2140
  414. if ($this->lastMatch->type === "begin" && $match->type === "end" && $this->lastMatch->index === $match->index && $lexeme === "") {
  415. // spit the "skipped" character that our regex choked on back into the output sequence
  416. $this->modeBuffer .= substr($this->codeToHighlight, $match->index, 1);
  417. return 1;
  418. }
  419. $this->lastMatch = $match;
  420. if ($match->type === "begin") {
  421. return $this->doBeginMatch($match);
  422. } elseif ($match->type === "illegal" && !$this->ignoreIllegals) {
  423. // illegal match, we do not continue processing
  424. $_modeRaw = isset($this->top->className) ? $this->top->className : "<unnamed>";
  425. throw new \UnexpectedValueException("Illegal lexeme \"$lexeme\" for mode \"$_modeRaw\"");
  426. } elseif ($match->type === "end") {
  427. $processed = $this->doEndMatch($match);
  428. if ($processed !== null) {
  429. return $processed;
  430. }
  431. }
  432. // Why might be find ourselves here? Only one occasion now. An end match that was
  433. // triggered but could not be completed. When might this happen? When an `endSameasBegin`
  434. // rule sets the end rule to a specific match. Since the overall mode termination rule that's
  435. // being used to scan the text isn't recompiled that means that any match that LOOKS like
  436. // the end (but is not, because it is not an exact match to the beginning) will
  437. // end up here. A definite end match, but when `doEndMatch` tries to "reapply"
  438. // the end rule and fails to match, we wind up here, and just silently ignore the end.
  439. //
  440. // This causes no real harm other than stopping a few times too many.
  441. $this->modeBuffer .= $lexeme;
  442. return strlen($lexeme);
  443. }
  444. /**
  445. * Replace tabs for something more usable.
  446. *
  447. * @param string $code
  448. *
  449. * @return string
  450. */
  451. private function replaceTabs($code)
  452. {
  453. if ($this->options['tabReplace'] !== null) {
  454. return str_replace("\t", $this->options['tabReplace'], $code);
  455. }
  456. return $code;
  457. }
  458. /**
  459. * Set the set of languages used for autodetection. When using
  460. * autodetection the code to highlight will be probed for every language
  461. * in this set. Limiting this set to only the languages you want to use
  462. * will greatly improve highlighting speed.
  463. *
  464. * @param string[] $set An array of language games to use for autodetection. This defaults
  465. * to a typical set Web development languages.
  466. *
  467. * @return void
  468. */
  469. public function setAutodetectLanguages(array $set)
  470. {
  471. $this->options['languages'] = array_unique($set);
  472. self::registerLanguages();
  473. }
  474. /**
  475. * Get the tab replacement string.
  476. *
  477. * @return string The tab replacement string
  478. */
  479. public function getTabReplace()
  480. {
  481. return $this->options['tabReplace'];
  482. }
  483. /**
  484. * Set the tab replacement string. This defaults to NULL: no tabs
  485. * will be replaced.
  486. *
  487. * @param string $tabReplace The tab replacement string
  488. *
  489. * @return void
  490. */
  491. public function setTabReplace($tabReplace)
  492. {
  493. $this->options['tabReplace'] = $tabReplace;
  494. }
  495. /**
  496. * Get the class prefix string.
  497. *
  498. * @return string The class prefix string
  499. */
  500. public function getClassPrefix()
  501. {
  502. return $this->options['classPrefix'];
  503. }
  504. /**
  505. * Set the class prefix string.
  506. *
  507. * @param string $classPrefix The class prefix string
  508. *
  509. * @return void
  510. */
  511. public function setClassPrefix($classPrefix)
  512. {
  513. $this->options['classPrefix'] = $classPrefix;
  514. }
  515. /**
  516. * @since 9.17.1.0
  517. *
  518. * @return void
  519. */
  520. public function enableSafeMode()
  521. {
  522. $this->safeMode = true;
  523. }
  524. /**
  525. * @since 9.17.1.0
  526. *
  527. * @return void
  528. */
  529. public function disableSafeMode()
  530. {
  531. $this->safeMode = false;
  532. }
  533. /**
  534. * @param string $name
  535. *
  536. * @return Language|null
  537. */
  538. private function getLanguage($name)
  539. {
  540. if (isset(self::$classMap[$name])) {
  541. return self::$classMap[$name];
  542. } elseif (isset(self::$aliases[$name]) && isset(self::$classMap[self::$aliases[$name]])) {
  543. return self::$classMap[self::$aliases[$name]];
  544. }
  545. return null;
  546. }
  547. /**
  548. * Determine whether or not a language definition supports auto detection.
  549. *
  550. * @param string $name Language name
  551. *
  552. * @return bool
  553. */
  554. private function autoDetection($name)
  555. {
  556. $lang = $this->getLanguage($name);
  557. return $lang && !$lang->disableAutodetect;
  558. }
  559. /**
  560. * Core highlighting function. Accepts a language name, or an alias, and a
  561. * string with the code to highlight. Returns an object with the following
  562. * properties:
  563. * - relevance (int)
  564. * - value (an HTML string with highlighting markup).
  565. *
  566. * @todo In v10.x, change the return type from \stdClass to HighlightResult
  567. *
  568. * @param string $languageName
  569. * @param string $code
  570. * @param bool $ignoreIllegals
  571. * @param Mode|null $continuation
  572. *
  573. * @throws \DomainException if the requested language was not in this
  574. * Highlighter's language set
  575. * @throws \Exception if an invalid regex was given in a language file
  576. *
  577. * @return HighlightResult|\stdClass
  578. */
  579. public function highlight($languageName, $code, $ignoreIllegals = true, $continuation = null)
  580. {
  581. $this->codeToHighlight = $code;
  582. $this->language = $this->getLanguage($languageName);
  583. if ($this->language === null) {
  584. throw new \DomainException("Unknown language: \"$languageName\"");
  585. }
  586. $this->language->compile($this->safeMode);
  587. $this->top = $continuation ? $continuation : $this->language;
  588. $this->continuations = array();
  589. $this->result = "";
  590. for ($current = $this->top; $current !== $this->language; $current = $current->parent) {
  591. if ($current->className) {
  592. $this->result = $this->buildSpan($current->className, '', true) . $this->result;
  593. }
  594. }
  595. $this->modeBuffer = "";
  596. $this->relevance = 0;
  597. $this->ignoreIllegals = $ignoreIllegals;
  598. /** @var HighlightResult $res */
  599. $res = new \stdClass();
  600. $res->relevance = 0;
  601. $res->value = "";
  602. $res->language = "";
  603. $res->top = null;
  604. $res->errorRaised = null;
  605. try {
  606. $match = null;
  607. $count = 0;
  608. $index = 0;
  609. while ($this->top) {
  610. $this->top->terminators->lastIndex = $index;
  611. $match = $this->top->terminators->exec($this->codeToHighlight);
  612. if (!$match) {
  613. break;
  614. }
  615. $count = $this->processLexeme(substr($this->codeToHighlight, $index, $match->index - $index), $match);
  616. $index = $match->index + $count;
  617. }
  618. $this->processLexeme(substr($this->codeToHighlight, $index));
  619. for ($current = $this->top; isset($current->parent); $current = $current->parent) {
  620. if ($current->className) {
  621. $this->result .= self::SPAN_END_TAG;
  622. }
  623. }
  624. $res->relevance = $this->relevance;
  625. $res->value = $this->replaceTabs($this->result);
  626. $res->illegal = false;
  627. $res->language = $this->language->name;
  628. $res->top = $this->top;
  629. return $res;
  630. } catch (\Exception $e) {
  631. if (strpos($e->getMessage(), "Illegal") !== false) {
  632. $res->illegal = true;
  633. $res->relevance = 0;
  634. $res->value = $this->escape($this->codeToHighlight);
  635. return $res;
  636. } elseif ($this->safeMode) {
  637. $res->relevance = 0;
  638. $res->value = $this->escape($this->codeToHighlight);
  639. $res->language = $languageName;
  640. $res->top = $this->top;
  641. $res->errorRaised = $e;
  642. return $res;
  643. }
  644. throw $e;
  645. }
  646. }
  647. /**
  648. * Highlight the given code by highlighting the given code with each
  649. * registered language and then finding the match with highest accuracy.
  650. *
  651. * @param string $code
  652. * @param string[]|null $languageSubset When set to null, this method will attempt to highlight $text with each
  653. * language. Set this to an array of languages of your choice to limit the
  654. * amount of languages to try.
  655. *
  656. * @throws \Exception if an invalid regex was given in a language file
  657. * @throws \DomainException if the attempted language to check does not exist
  658. *
  659. * @return HighlightResult|\stdClass
  660. */
  661. public function highlightAuto($code, $languageSubset = null)
  662. {
  663. /** @var HighlightResult $result */
  664. $result = new \stdClass();
  665. $result->relevance = 0;
  666. $result->value = $this->escape($code);
  667. $result->language = "";
  668. $secondBest = clone $result;
  669. if ($languageSubset === null) {
  670. $optionsLanguages = $this->options['languages'];
  671. if (is_array($optionsLanguages) && count($optionsLanguages) > 0) {
  672. $languageSubset = $optionsLanguages;
  673. } else {
  674. $languageSubset = self::$languages;
  675. }
  676. }
  677. foreach ($languageSubset as $name) {
  678. if ($this->getLanguage($name) === null || !$this->autoDetection($name)) {
  679. continue;
  680. }
  681. $current = $this->highlight($name, $code, false);
  682. if ($current->relevance > $secondBest->relevance) {
  683. $secondBest = $current;
  684. }
  685. if ($current->relevance > $result->relevance) {
  686. $secondBest = $result;
  687. $result = $current;
  688. }
  689. }
  690. if ($secondBest->language) {
  691. $result->secondBest = $secondBest;
  692. }
  693. return $result;
  694. }
  695. /**
  696. * Return a list of all supported languages. Using this list in
  697. * setAutodetectLanguages will turn on autodetection for all supported
  698. * languages.
  699. *
  700. * @param bool $include_aliases specify whether language aliases
  701. * should be included as well
  702. *
  703. * @since 9.12.0.3 The `$include_aliases` parameter was added
  704. * @since 8.3.0.0
  705. *
  706. * @return string[] An array of language names
  707. */
  708. public function listLanguages($include_aliases = false)
  709. {
  710. if ($include_aliases === true) {
  711. return array_merge(self::$languages, array_keys(self::$aliases));
  712. }
  713. return self::$languages;
  714. }
  715. /**
  716. * Returns list of all available aliases for given language name.
  717. *
  718. * @param string $name name or alias of language to look-up
  719. *
  720. * @throws \DomainException if the requested language was not in this
  721. * Highlighter's language set
  722. *
  723. * @since 9.12.0.3
  724. *
  725. * @return string[] An array of all aliases associated with the requested
  726. * language name language. Passed-in name is included as
  727. * well.
  728. */
  729. public function getAliasesForLanguage($name)
  730. {
  731. $language = self::getLanguage($name);
  732. if ($language === null) {
  733. throw new \DomainException("Unknown language: $language");
  734. }
  735. if ($language->aliases === null) {
  736. return array($language->name);
  737. }
  738. return array_merge(array($language->name), $language->aliases);
  739. }
  740. }