Highlighter.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. class Highlighter
  34. {
  35. const SPAN_END_TAG = "</span>";
  36. /** @var bool */
  37. private $safeMode = true;
  38. /** @var array */
  39. private $options;
  40. /** @var string */
  41. private $modeBuffer = "";
  42. /** @var string */
  43. private $result = "";
  44. /** @var Language|null */
  45. private $top = null;
  46. /** @var Language|null */
  47. private $language = null;
  48. /** @var int */
  49. private $relevance = 0;
  50. /** @var bool */
  51. private $ignoreIllegals = false;
  52. /** @var array */
  53. private $continuations = array();
  54. /** @var RegExMatch */
  55. private $lastMatch;
  56. /** @var string */
  57. private $value;
  58. private static $classMap = array();
  59. private static $languages = null;
  60. private static $aliases = null;
  61. private $autodetectSet = array(
  62. "xml", "json", "javascript", "css", "php", "http",
  63. );
  64. public function __construct()
  65. {
  66. $this->lastMatch = new RegExMatch(array());
  67. $this->lastMatch->type = "";
  68. $this->lastMatch->rule = null;
  69. $this->options = array(
  70. 'classPrefix' => 'hljs-',
  71. 'tabReplace' => null,
  72. 'useBR' => false,
  73. 'languages' => null,
  74. );
  75. self::registerLanguages();
  76. }
  77. private static function registerLanguages()
  78. {
  79. // Languages that take precedence in the classMap array.
  80. $languagePath = __DIR__ . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR;
  81. foreach (array("xml", "django", "javascript", "matlab", "cpp") as $languageId) {
  82. $filePath = $languagePath . $languageId . ".json";
  83. if (is_readable($filePath)) {
  84. self::registerLanguage($languageId, $filePath);
  85. }
  86. }
  87. $d = @dir($languagePath);
  88. if ($d) {
  89. while (($entry = $d->read()) !== false) {
  90. if (substr($entry, -5) === ".json") {
  91. $languageId = substr($entry, 0, -5);
  92. $filePath = $languagePath . $entry;
  93. if (is_readable($filePath)) {
  94. self::registerLanguage($languageId, $filePath);
  95. }
  96. }
  97. }
  98. $d->close();
  99. }
  100. self::$languages = array_keys(self::$classMap);
  101. }
  102. /**
  103. * Register a language definition with the Highlighter's internal language
  104. * storage. Languages are stored in a static variable, so they'll be available
  105. * across all instances. You only need to register a language once.
  106. *
  107. * @param string $languageId The unique name of a language
  108. * @param string $filePath The file path to the language definition
  109. * @param bool $overwrite Overwrite language if it already exists
  110. *
  111. * @return Language The object containing the definition for a language's markup
  112. */
  113. public static function registerLanguage($languageId, $filePath, $overwrite = false)
  114. {
  115. if (!isset(self::$classMap[$languageId]) || $overwrite) {
  116. $lang = new Language($languageId, $filePath);
  117. self::$classMap[$languageId] = $lang;
  118. if ($lang->aliases) {
  119. foreach ($lang->aliases as $alias) {
  120. self::$aliases[$alias] = $languageId;
  121. }
  122. }
  123. }
  124. return self::$classMap[$languageId];
  125. }
  126. /**
  127. * @param $re
  128. * @param $lexeme
  129. *
  130. * @throws \Exception
  131. *
  132. * @return bool
  133. */
  134. private function testRe($re, $lexeme)
  135. {
  136. if (!$re) {
  137. return false;
  138. }
  139. $test = preg_match((string) $re, $lexeme, $match, PREG_OFFSET_CAPTURE);
  140. if ($test === false) {
  141. throw new \Exception("Invalid regexp: " . var_export($re, true));
  142. }
  143. return count($match) && ($match[0][1] == 0);
  144. }
  145. private function escapeRe($value)
  146. {
  147. return sprintf('/%s/m', preg_quote($value));
  148. }
  149. /**
  150. * @param $mode
  151. * @param $lexeme
  152. *
  153. * @throws \Exception
  154. *
  155. * @return Mode|null
  156. */
  157. private function endOfMode($mode, $lexeme)
  158. {
  159. if ($this->testRe($mode->endRe, $lexeme)) {
  160. while ($mode->endsParent && $mode->parent) {
  161. $mode = $mode->parent;
  162. }
  163. return $mode;
  164. }
  165. if ($mode->endsWithParent) {
  166. return $this->endOfMode($mode->parent, $lexeme);
  167. }
  168. return null;
  169. }
  170. private function keywordMatch($mode, $match)
  171. {
  172. $kwd = $this->language->case_insensitive ? mb_strtolower($match[0], "UTF-8") : $match[0];
  173. return isset($mode->keywords[$kwd]) ? $mode->keywords[$kwd] : null;
  174. }
  175. private function buildSpan($className, $insideSpan, $leaveOpen = false, $noPrefix = false)
  176. {
  177. if (!$leaveOpen && $insideSpan === '') {
  178. return '';
  179. }
  180. if (!$className) {
  181. return $insideSpan;
  182. }
  183. $classPrefix = $noPrefix ? "" : $this->options['classPrefix'];
  184. $openSpan = "<span class=\"" . $classPrefix;
  185. $closeSpan = $leaveOpen ? "" : self::SPAN_END_TAG;
  186. $openSpan .= $className . "\">";
  187. return $openSpan . $insideSpan . $closeSpan;
  188. }
  189. private function escape($value)
  190. {
  191. return htmlspecialchars($value, ENT_NOQUOTES);
  192. }
  193. private function processKeywords()
  194. {
  195. if (!$this->top->keywords) {
  196. return $this->escape($this->modeBuffer);
  197. }
  198. $result = "";
  199. $lastIndex = 0;
  200. $this->top->lexemesRe->lastIndex = 0;
  201. $match = $this->top->lexemesRe->exec($this->modeBuffer);
  202. while ($match) {
  203. $result .= $this->escape(substr($this->modeBuffer, $lastIndex, $match->index - $lastIndex));
  204. $keyword_match = $this->keywordMatch($this->top, $match);
  205. if ($keyword_match) {
  206. $this->relevance += $keyword_match[1];
  207. $result .= $this->buildSpan($keyword_match[0], $this->escape($match[0]));
  208. } else {
  209. $result .= $this->escape($match[0]);
  210. }
  211. $lastIndex = $this->top->lexemesRe->lastIndex;
  212. $match = $this->top->lexemesRe->exec($this->modeBuffer);
  213. }
  214. return $result . $this->escape(substr($this->modeBuffer, $lastIndex));
  215. }
  216. private function processSubLanguage()
  217. {
  218. try {
  219. $hl = new Highlighter();
  220. $hl->setAutodetectLanguages($this->autodetectSet);
  221. $explicit = is_string($this->top->subLanguage);
  222. if ($explicit && !in_array($this->top->subLanguage, self::$languages)) {
  223. return $this->escape($this->modeBuffer);
  224. }
  225. if ($explicit) {
  226. $res = $hl->highlight(
  227. $this->top->subLanguage,
  228. $this->modeBuffer,
  229. true,
  230. isset($this->continuations[$this->top->subLanguage]) ? $this->continuations[$this->top->subLanguage] : null
  231. );
  232. } else {
  233. $res = $hl->highlightAuto(
  234. $this->modeBuffer,
  235. count($this->top->subLanguage) ? $this->top->subLanguage : null
  236. );
  237. }
  238. // Counting embedded language score towards the host language may be disabled
  239. // with zeroing the containing mode relevance. Use case in point is Markdown that
  240. // allows XML everywhere and makes every XML snippet to have a much larger Markdown
  241. // score.
  242. if ($this->top->relevance > 0) {
  243. $this->relevance += $res->relevance;
  244. }
  245. if ($explicit) {
  246. $this->continuations[$this->top->subLanguage] = $res->top;
  247. }
  248. return $this->buildSpan($res->language, $res->value, false, true);
  249. } catch (\Exception $e) {
  250. error_log("TODO, is this a relevant catch?");
  251. error_log($e);
  252. return $this->escape($this->modeBuffer);
  253. }
  254. }
  255. private function processBuffer()
  256. {
  257. if (is_object($this->top) && $this->top->subLanguage) {
  258. $this->result .= $this->processSubLanguage();
  259. } else {
  260. $this->result .= $this->processKeywords();
  261. }
  262. $this->modeBuffer = '';
  263. }
  264. private function startNewMode($mode)
  265. {
  266. $this->result .= $mode->className ? $this->buildSpan($mode->className, "", true) : "";
  267. $t = clone $mode;
  268. $t->parent = $this->top;
  269. $this->top = $t;
  270. }
  271. private function doBeginMatch($match)
  272. {
  273. $lexeme = $match[0];
  274. $newMode = $match->rule;
  275. if ($newMode && $newMode->endSameAsBegin) {
  276. $newMode->endRe = $this->escapeRe($lexeme);
  277. }
  278. if ($newMode->skip) {
  279. $this->modeBuffer .= $lexeme;
  280. } else {
  281. if ($newMode->excludeBegin) {
  282. $this->modeBuffer .= $lexeme;
  283. }
  284. $this->processBuffer();
  285. if (!$newMode->returnBegin && !$newMode->excludeBegin) {
  286. $this->modeBuffer = $lexeme;
  287. }
  288. }
  289. $this->startNewMode($newMode);
  290. return $newMode->returnBegin ? 0 : strlen($lexeme);
  291. }
  292. private function doEndMatch($match)
  293. {
  294. $lexeme = $match[0];
  295. $matchPlusRemainder = substr($this->value, $match->index);
  296. $endMode = $this->endOfMode($this->top, $matchPlusRemainder);
  297. if (!$endMode) {
  298. return null;
  299. }
  300. $origin = $this->top;
  301. if ($origin->skip) {
  302. $this->modeBuffer .= $lexeme;
  303. } else {
  304. if (!($origin->returnEnd || $origin->excludeEnd)) {
  305. $this->modeBuffer .= $lexeme;
  306. }
  307. $this->processBuffer();
  308. if ($origin->excludeEnd) {
  309. $this->modeBuffer = $lexeme;
  310. }
  311. }
  312. do {
  313. if ($this->top->className) {
  314. $this->result .= self::SPAN_END_TAG;
  315. }
  316. if (!$this->top->skip && !$this->top->subLanguage) {
  317. $this->relevance += $this->top->relevance;
  318. }
  319. $this->top = $this->top->parent;
  320. } while ($this->top !== $endMode->parent);
  321. if ($endMode->starts) {
  322. if ($endMode->endSameAsBegin) {
  323. $endMode->starts->endRe = $endMode->endRe;
  324. }
  325. $this->startNewMode($endMode->starts);
  326. }
  327. return $origin->returnEnd ? 0 : strlen($lexeme);
  328. }
  329. /**
  330. * @param string $textBeforeMatch
  331. * @param RegExMatch|null $match
  332. *
  333. * @return int
  334. */
  335. private function processLexeme($textBeforeMatch, $match = null)
  336. {
  337. $lexeme = $match ? $match[0] : null;
  338. // add non-matched text to the current mode buffer
  339. $this->modeBuffer .= $textBeforeMatch;
  340. if ($lexeme === null) {
  341. $this->processBuffer();
  342. return 0;
  343. }
  344. // we've found a 0 width match and we're stuck, so we need to advance
  345. // this happens when we have badly behaved rules that have optional matchers to the degree that
  346. // sometimes they can end up matching nothing at all
  347. // Ref: https://github.com/highlightjs/highlight.js/issues/2140
  348. if ($this->lastMatch->type === "begin" && $match->type === "end" && $this->lastMatch->index === $match->index && $lexeme === "") {
  349. // spit the "skipped" character that our regex choked on back into the output sequence
  350. $this->modeBuffer .= substr($this->value, $match->index, 1);
  351. return 1;
  352. }
  353. $this->lastMatch = $match;
  354. if ($match->type === "begin") {
  355. return $this->doBeginMatch($match);
  356. } elseif ($match->type === "illegal" && !$this->ignoreIllegals) {
  357. // illegal match, we do not continue processing
  358. $_modeRaw = isset($this->top->className) ? $this->top->className : "<unnamed>";
  359. throw new \UnexpectedValueException("Illegal lexeme \"$lexeme\" for mode \"$_modeRaw\"");
  360. } elseif ($match->type === "end") {
  361. $processed = $this->doEndMatch($match);
  362. if ($processed !== null) {
  363. return $processed;
  364. }
  365. }
  366. // Why might be find ourselves here? Only one occasion now. An end match that was
  367. // triggered but could not be completed. When might this happen? When an `endSameasBegin`
  368. // rule sets the end rule to a specific match. Since the overall mode termination rule that's
  369. // being used to scan the text isn't recompiled that means that any match that LOOKS like
  370. // the end (but is not, because it is not an exact match to the beginning) will
  371. // end up here. A definite end match, but when `doEndMatch` tries to "reapply"
  372. // the end rule and fails to match, we wind up here, and just silently ignore the end.
  373. //
  374. // This causes no real harm other than stopping a few times too many.
  375. $this->modeBuffer .= $lexeme;
  376. return strlen($lexeme);
  377. }
  378. /**
  379. * Replace tabs for something more usable.
  380. *
  381. * @param string $code
  382. *
  383. * @return string
  384. */
  385. private function replaceTabs($code)
  386. {
  387. if ($this->options['tabReplace'] !== null) {
  388. return str_replace("\t", $this->options['tabReplace'], $code);
  389. }
  390. return $code;
  391. }
  392. /**
  393. * Set the set of languages used for autodetection. When using
  394. * autodetection the code to highlight will be probed for every language
  395. * in this set. Limiting this set to only the languages you want to use
  396. * will greatly improve highlighting speed.
  397. *
  398. * @param array $set An array of language games to use for autodetection. This defaults
  399. * to a typical set Web development languages.
  400. */
  401. public function setAutodetectLanguages(array $set)
  402. {
  403. $this->autodetectSet = array_unique($set);
  404. self::registerLanguages();
  405. }
  406. /**
  407. * Get the tab replacement string.
  408. *
  409. * @return string The tab replacement string
  410. */
  411. public function getTabReplace()
  412. {
  413. return $this->options['tabReplace'];
  414. }
  415. /**
  416. * Set the tab replacement string. This defaults to NULL: no tabs
  417. * will be replaced.
  418. *
  419. * @param string $tabReplace The tab replacement string
  420. */
  421. public function setTabReplace($tabReplace)
  422. {
  423. $this->options['tabReplace'] = $tabReplace;
  424. }
  425. /**
  426. * Get the class prefix string.
  427. *
  428. * @return string
  429. * The class prefix string
  430. */
  431. public function getClassPrefix()
  432. {
  433. return $this->options['classPrefix'];
  434. }
  435. /**
  436. * Set the class prefix string.
  437. *
  438. * @param string $classPrefix The class prefix string
  439. */
  440. public function setClassPrefix($classPrefix)
  441. {
  442. $this->options['classPrefix'] = $classPrefix;
  443. }
  444. /**
  445. * @since 9.17.1.0
  446. */
  447. public function enableSafeMode()
  448. {
  449. $this->safeMode = true;
  450. }
  451. /**
  452. * @since 9.17.1.0
  453. */
  454. public function disableSafeMode()
  455. {
  456. $this->safeMode = false;
  457. }
  458. /**
  459. * @param string $name
  460. *
  461. * @throws \DomainException if the requested language was not in this
  462. * Highlighter's language set
  463. *
  464. * @return Language
  465. */
  466. private function getLanguage($name)
  467. {
  468. if (isset(self::$classMap[$name])) {
  469. return self::$classMap[$name];
  470. } elseif (isset(self::$aliases[$name]) && isset(self::$classMap[self::$aliases[$name]])) {
  471. return self::$classMap[self::$aliases[$name]];
  472. }
  473. throw new \DomainException("Unknown language: $name");
  474. }
  475. /**
  476. * Determine whether or not a language definition supports auto detection.
  477. *
  478. * @param string $name Language name
  479. *
  480. * @return bool
  481. */
  482. private function autoDetection($name)
  483. {
  484. return !$this->getLanguage($name)->disableAutodetect;
  485. }
  486. /**
  487. * Core highlighting function. Accepts a language name, or an alias, and a
  488. * string with the code to highlight. Returns an object with the following
  489. * properties:
  490. * - relevance (int)
  491. * - value (an HTML string with highlighting markup).
  492. *
  493. * @todo In v10.x, change the return type from \stdClass to HighlightResult
  494. *
  495. * @param string $name
  496. * @param string $value
  497. * @param bool $ignoreIllegals
  498. * @param null $continuation
  499. *
  500. * @throws \DomainException if the requested language was not in this
  501. * Highlighter's language set
  502. * @throws \Exception if an invalid regex was given in a language file
  503. *
  504. * @return HighlightResult|\stdClass
  505. */
  506. public function highlight($name, $value, $ignoreIllegals = true, $continuation = null)
  507. {
  508. $this->value = $value;
  509. $this->language = $this->getLanguage($name);
  510. $this->language->compile($this->safeMode);
  511. $this->language->caseInsensitive = 0;
  512. $this->top = $continuation ? $continuation : $this->language;
  513. $this->continuations = array();
  514. $this->result = "";
  515. for ($current = $this->top; $current !== $this->language; $current = $current->parent) {
  516. if ($current->className) {
  517. $this->result = $this->buildSpan($current->className, '', true) . $this->result;
  518. }
  519. }
  520. $this->modeBuffer = "";
  521. $this->relevance = 0;
  522. $this->ignoreIllegals = $ignoreIllegals;
  523. /** @var HighlightResult $res */
  524. $res = new \stdClass();
  525. $res->relevance = 0;
  526. $res->value = "";
  527. $res->language = "";
  528. $res->top = null;
  529. $res->errorRaised = null;
  530. try {
  531. $match = null;
  532. $count = 0;
  533. $index = 0;
  534. while ($this->top) {
  535. $this->top->terminators->lastIndex = $index;
  536. $match = $this->top->terminators->exec($value);
  537. if (!$match) {
  538. break;
  539. }
  540. $count = $this->processLexeme(substr($value, $index, $match->index - $index), $match);
  541. $index = $match->index + $count;
  542. }
  543. $this->processLexeme(substr($value, $index));
  544. for ($current = $this->top; isset($current->parent); $current = $current->parent) {
  545. if ($current->className) {
  546. $this->result .= self::SPAN_END_TAG;
  547. }
  548. }
  549. $res->relevance = $this->relevance;
  550. $res->value = $this->replaceTabs($this->result);
  551. $res->illegal = false;
  552. $res->language = $this->language->name;
  553. $res->top = $this->top;
  554. return $res;
  555. } catch (\Exception $e) {
  556. if (strpos($e->getMessage(), "Illegal") !== false) {
  557. $res->illegal = true;
  558. $res->relevance = 0;
  559. $res->value = $this->escape($value);
  560. return $res;
  561. } elseif ($this->safeMode) {
  562. $res->relevance = 0;
  563. $res->value = $this->escape($value);
  564. $res->language = $name;
  565. $res->top = $this->top;
  566. $res->errorRaised = $e;
  567. return $res;
  568. }
  569. throw $e;
  570. }
  571. }
  572. /**
  573. * Highlight the given code by highlighting the given code with each
  574. * registered language and then finding the match with highest accuracy.
  575. *
  576. * @param string $text
  577. * @param string[]|null $languageSubset When set to null, this method will
  578. * attempt to highlight $code with each language (170+). Set this to
  579. * an array of languages of your choice to limit the amount of languages
  580. * to try.
  581. *
  582. * @throws \Exception if an invalid regex was given in a language file
  583. * @throws \DomainException if the attempted language to check does not exist
  584. *
  585. * @return HighlightResult|\stdClass
  586. */
  587. public function highlightAuto($text, $languageSubset = null)
  588. {
  589. $res = new \stdClass();
  590. $res->relevance = 0;
  591. $res->value = $this->escape($text);
  592. $res->language = "";
  593. $scnd = clone $res;
  594. $tmp = $languageSubset ? $languageSubset : $this->autodetectSet;
  595. foreach ($tmp as $l) {
  596. // don't fail if we run into a non-existent language
  597. try {
  598. // skip any languages that don't support auto detection
  599. if (!$this->autoDetection($l)) {
  600. continue;
  601. }
  602. $current = $this->highlight($l, $text, false);
  603. } catch (\DomainException $e) {
  604. continue;
  605. }
  606. if ($current->relevance > $scnd->relevance) {
  607. $scnd = $current;
  608. }
  609. if ($current->relevance > $res->relevance) {
  610. $scnd = $res;
  611. $res = $current;
  612. }
  613. }
  614. if ($scnd->language) {
  615. $res->secondBest = $scnd;
  616. }
  617. return $res;
  618. }
  619. /**
  620. * Return a list of all supported languages. Using this list in
  621. * setAutodetectLanguages will turn on autodetection for all supported
  622. * languages.
  623. *
  624. * @param bool $include_aliases specify whether language aliases
  625. * should be included as well
  626. *
  627. * @return string[] An array of language names
  628. */
  629. public function listLanguages($include_aliases = false)
  630. {
  631. if ($include_aliases === true) {
  632. return array_merge(self::$languages, array_keys(self::$aliases));
  633. }
  634. return self::$languages;
  635. }
  636. /**
  637. * Returns list of all available aliases for given language name.
  638. *
  639. * @param string $language name or alias of language to look-up
  640. *
  641. * @throws \DomainException if the requested language was not in this
  642. * Highlighter's language set
  643. *
  644. * @return string[] An array of all aliases associated with the requested
  645. * language name language. Passed-in name is included as
  646. * well.
  647. */
  648. public function getAliasesForLanguage($language)
  649. {
  650. $language = self::getLanguage($language);
  651. if ($language->aliases === null) {
  652. return array($language->name);
  653. }
  654. return array_merge(array($language->name), $language->aliases);
  655. }
  656. }