Skip to content

Commit

Permalink
Merge pull request #64 from scrivo/feature/9.18-support
Browse files Browse the repository at this point in the history
highlight.js 9.18.x support
  • Loading branch information
allejo authored Jan 29, 2020
2 parents abe5a2f + 0cb2c45 commit 0e78600
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 34 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Language and style definitions (copied from the highlight.js project)
Jason Tate <[email protected]>
Jay Strybis <[email protected]>
Jeff Escalante <[email protected]>
Jeffrey Arnold <[email protected]>
Jen Evers-Corvina <[email protected]>
Joe Cheng <[email protected]>
Joe Eli McIlvain <[email protected]>
Expand Down
10 changes: 5 additions & 5 deletions Highlight/HighlightResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@
*/
abstract class HighlightResult
{
/** @var int */
/** @var int the relevance score */
public $relevance;

/** @var string */
/** @var string the highlighted HTML code */
public $value;

/** @var string */
/** @var string the language name */
public $language;

/** @var bool */
/** @var bool indicates whether any illegal matches were found */
public $illegal;

/** @var Mode|null */
/** @var Mode|null top of the current mode stack */
public $top;

/** @var \Exception|null */
Expand Down
38 changes: 19 additions & 19 deletions Highlight/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Highlighter
private $lastMatch;

/** @var string The current code we are highlighting */
private $value;
private $codeToHighlight;

/** @var array<string, Language> A mapping of a language ID to a Language definition */
private static $classMap = array();
Expand Down Expand Up @@ -422,7 +422,7 @@ private function doBeginMatch($match)
private function doEndMatch($match)
{
$lexeme = $match[0];
$matchPlusRemainder = substr($this->value, $match->index);
$matchPlusRemainder = substr($this->codeToHighlight, $match->index);
$endMode = $this->endOfMode($this->top, $matchPlusRemainder);

if (!$endMode) {
Expand Down Expand Up @@ -488,7 +488,7 @@ private function processLexeme($textBeforeMatch, $match = null)
// Ref: https://github.com/highlightjs/highlight.js/issues/2140
if ($this->lastMatch->type === "begin" && $match->type === "end" && $this->lastMatch->index === $match->index && $lexeme === "") {
// spit the "skipped" character that our regex choked on back into the output sequence
$this->modeBuffer .= substr($this->value, $match->index, 1);
$this->modeBuffer .= substr($this->codeToHighlight, $match->index, 1);

return 1;
}
Expand Down Expand Up @@ -661,8 +661,8 @@ private function autoDetection($name)
*
* @todo In v10.x, change the return type from \stdClass to HighlightResult
*
* @param string $name
* @param string $value
* @param string $languageName
* @param string $code
* @param bool $ignoreIllegals
* @param Mode|null $continuation
*
Expand All @@ -672,13 +672,13 @@ private function autoDetection($name)
*
* @return HighlightResult|\stdClass
*/
public function highlight($name, $value, $ignoreIllegals = true, $continuation = null)
public function highlight($languageName, $code, $ignoreIllegals = true, $continuation = null)
{
$this->value = $value;
$this->language = $this->getLanguage($name);
$this->codeToHighlight = $code;
$this->language = $this->getLanguage($languageName);

if ($this->language === null) {
throw new \DomainException("Unknown language: \"$name\"");
throw new \DomainException("Unknown language: \"$languageName\"");
}

$this->language->compile($this->safeMode);
Expand Down Expand Up @@ -711,17 +711,17 @@ public function highlight($name, $value, $ignoreIllegals = true, $continuation =

while ($this->top) {
$this->top->terminators->lastIndex = $index;
$match = $this->top->terminators->exec($value);
$match = $this->top->terminators->exec($this->codeToHighlight);

if (!$match) {
break;
}

$count = $this->processLexeme(substr($value, $index, $match->index - $index), $match);
$count = $this->processLexeme(substr($this->codeToHighlight, $index, $match->index - $index), $match);
$index = $match->index + $count;
}

$this->processLexeme(substr($value, $index));
$this->processLexeme(substr($this->codeToHighlight, $index));

for ($current = $this->top; isset($current->parent); $current = $current->parent) {
if ($current->className) {
Expand All @@ -740,13 +740,13 @@ public function highlight($name, $value, $ignoreIllegals = true, $continuation =
if (strpos($e->getMessage(), "Illegal") !== false) {
$res->illegal = true;
$res->relevance = 0;
$res->value = $this->escape($value);
$res->value = $this->escape($this->codeToHighlight);

return $res;
} elseif ($this->safeMode) {
$res->relevance = 0;
$res->value = $this->escape($value);
$res->language = $name;
$res->value = $this->escape($this->codeToHighlight);
$res->language = $languageName;
$res->top = $this->top;
$res->errorRaised = $e;

Expand All @@ -761,7 +761,7 @@ public function highlight($name, $value, $ignoreIllegals = true, $continuation =
* Highlight the given code by highlighting the given code with each
* registered language and then finding the match with highest accuracy.
*
* @param string $text
* @param string $code
* @param string[]|null $languageSubset When set to null, this method will attempt to highlight $text with each
* language. Set this to an array of languages of your choice to limit the
* amount of languages to try.
Expand All @@ -771,12 +771,12 @@ public function highlight($name, $value, $ignoreIllegals = true, $continuation =
*
* @return HighlightResult|\stdClass
*/
public function highlightAuto($text, $languageSubset = null)
public function highlightAuto($code, $languageSubset = null)
{
/** @var HighlightResult $result */
$result = new \stdClass();
$result->relevance = 0;
$result->value = $this->escape($text);
$result->value = $this->escape($code);
$result->language = "";
$secondBest = clone $result;

Expand All @@ -795,7 +795,7 @@ public function highlightAuto($text, $languageSubset = null)
continue;
}

$current = $this->highlight($name, $text, false);
$current = $this->highlight($name, $code, false);

if ($current->relevance > $secondBest->relevance) {
$secondBest = $current;
Expand Down
2 changes: 1 addition & 1 deletion Highlight/languages/arduino.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Highlight/languages/cpp.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"aliases":["c","cc","h","c++","h++","hpp","hh","hxx","cxx"],"keywords":{"keyword":"int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace unsigned long volatile static protected bool template mutable if public friend do goto auto void enum else break extern using asm case typeid wchar_tshort reinterpret_cast|10 default double register explicit signed typename try this switch continue inline delete alignas alignof constexpr consteval constinit decltype concept co_await co_return co_yield requires noexcept static_assert thread_local restrict final override atomic_bool atomic_char atomic_schar atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong atomic_ullong new throw return and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq","built_in":"std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream auto_ptr deque list queue stack vector map set bitset multiset multimap unordered_set unordered_map unordered_multiset unordered_multimap array shared_ptr abort terminate abs acos asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary","literal":"true false nullptr NULL"},"illegal":"</","contains":[{"className":"keyword","begin":"\\b[a-z\\d_]*_t\\b"},{"className":"comment","begin":"//","end":"$","contains":[{"begin":"\\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\\b"},{"className":"doctag","begin":"(?:TODO|FIXME|NOTE|BUG|XXX):","relevance":0}]},{"className":"comment","begin":"/\\*","end":"\\*/","contains":[{"$ref":"#contains.1.contains.0"},{"className":"doctag","begin":"(?:TODO|FIXME|NOTE|BUG|XXX):","relevance":0}]},{"className":"number","variants":[{"begin":"\\b(0b[01']+)"},{"begin":"(-?)\\b([\\d']+(\\.[\\d']*)?|\\.[\\d']+)(u|U|l|L|ul|UL|f|F|b|B)"},{"begin":"(-?)(\\b0[xX][a-fA-F0-9']+|(\\b[\\d']+(\\.[\\d']*)?|\\.[\\d']+)([eE][-+]?[\\d']+)?)"}],"relevance":0},{"className":"string","variants":[{"begin":"(u8?|U|L)?\"","end":"\"","illegal":"\\n","contains":[{"begin":"\\\\[\\s\\S]","relevance":0}]},{"begin":"(u8?|U|L)?'(\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)|.)","end":"'","illegal":"."},{"begin":"(?:u8?|U|L)?R\"([^()\\\\ ]{0,16})\\((?:.|\\n)*?\\)\\1\""}]},{"className":"meta","begin":"#\\s*[a-z]+\\b","end":"$","keywords":{"meta-keyword":"if else elif endif define undef warning error line pragma _Pragma ifdef ifndef include"},"contains":[{"begin":"\\\\\\n","relevance":0},{"className":"meta-string","variants":{"$ref":"#contains.4.variants"}},{"className":"meta-string","begin":"<.*?>","end":"$","illegal":"\\n"},{"$ref":"#contains.1"},{"$ref":"#contains.2"}]},{"begin":"\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<","end":">","keywords":{"$ref":"#keywords"},"contains":["self",{"$ref":"#contains.0"}]},{"begin":"[a-zA-Z]\\w*::","keywords":{"$ref":"#keywords"}},{"variants":[{"begin":"=","end":";"},{"begin":"\\(","end":"\\)"},{"beginKeywords":"new throw return else","end":";"}],"keywords":{"$ref":"#keywords"},"contains":[{"$ref":"#contains.0"},{"$ref":"#contains.1"},{"$ref":"#contains.2"},{"$ref":"#contains.3"},{"$ref":"#contains.4"},{"begin":"\\(","end":"\\)","keywords":{"$ref":"#keywords"},"contains":[{"$ref":"#contains.0"},{"$ref":"#contains.1"},{"$ref":"#contains.2"},{"$ref":"#contains.3"},{"$ref":"#contains.4"},"self"],"relevance":0}],"relevance":0},{"className":"function","begin":"([a-zA-Z]\\w*[\\*&\\s]+)+[a-zA-Z]\\w*\\s*\\(","returnBegin":true,"end":"[{;=]","excludeEnd":true,"keywords":{"$ref":"#keywords"},"illegal":"[^\\w\\s\\*&]","contains":[{"begin":"[a-zA-Z]\\w*\\s*\\(","returnBegin":true,"contains":[{"className":"title","begin":"[a-zA-Z]\\w*","relevance":0}],"relevance":0},{"className":"params","begin":"\\(","end":"\\)","keywords":{"$ref":"#keywords"},"relevance":0,"contains":[{"$ref":"#contains.1"},{"$ref":"#contains.2"},{"$ref":"#contains.4"},{"$ref":"#contains.3"},{"$ref":"#contains.0"},{"begin":"\\(","end":"\\)","keywords":{"$ref":"#keywords"},"relevance":0,"contains":["self",{"$ref":"#contains.1"},{"$ref":"#contains.2"},{"$ref":"#contains.4"},{"$ref":"#contains.3"},{"$ref":"#contains.0"}]}]},{"$ref":"#contains.1"},{"$ref":"#contains.2"},{"$ref":"#contains.5"}]},{"className":"class","beginKeywords":"class struct","end":"[{;:]","contains":[{"begin":"<","end":">","contains":["self"]},{"$ref":"#contains.9.contains.0.contains.0"}]}],"exports":{"preprocessor":{"$ref":"#contains.5"},"strings":{"$ref":"#contains.4"},"keywords":{"$ref":"#keywords"}}}
{"aliases":["c","cc","h","c++","h++","hpp","hh","hxx","cxx"],"keywords":{"keyword":"int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace unsigned long volatile static protected bool template mutable if public friend do goto auto void enum else break extern using asm case typeid wchar_tshort reinterpret_cast|10 default double register explicit signed typename try this switch continue inline delete alignas alignof constexpr consteval constinit decltype concept co_await co_return co_yield requires noexcept static_assert thread_local restrict final override atomic_bool atomic_char atomic_schar atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong atomic_ullong new throw return and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq","built_in":"std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream auto_ptr deque list queue stack vector map set bitset multiset multimap unordered_set unordered_map unordered_multiset unordered_multimap array shared_ptr abort terminate abs acos asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary","literal":"true false nullptr NULL"},"illegal":"</","contains":[{"variants":[{"begin":"=","end":";"},{"begin":"\\(","end":"\\)"},{"beginKeywords":"new throw return else","end":";"}],"keywords":{"$ref":"#keywords"},"contains":[{"className":"keyword","begin":"\\b[a-z\\d_]*_t\\b"},{"className":"comment","begin":"//","end":"$","contains":[{"begin":"\\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\\b"},{"className":"doctag","begin":"(?:TODO|FIXME|NOTE|BUG|XXX):","relevance":0}]},{"className":"comment","begin":"/\\*","end":"\\*/","contains":[{"$ref":"#contains.0.contains.1.contains.0"},{"className":"doctag","begin":"(?:TODO|FIXME|NOTE|BUG|XXX):","relevance":0}]},{"className":"number","variants":[{"begin":"\\b(0b[01']+)"},{"begin":"(-?)\\b([\\d']+(\\.[\\d']*)?|\\.[\\d']+)(u|U|l|L|ul|UL|f|F|b|B)"},{"begin":"(-?)(\\b0[xX][a-fA-F0-9']+|(\\b[\\d']+(\\.[\\d']*)?|\\.[\\d']+)([eE][-+]?[\\d']+)?)"}],"relevance":0},{"className":"string","variants":[{"begin":"(u8?|U|L)?\"","end":"\"","illegal":"\\n","contains":[{"begin":"\\\\[\\s\\S]","relevance":0}]},{"begin":"(u8?|U|L)?'(\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)|.)","end":"'","illegal":"."},{"begin":"(?:u8?|U|L)?R\"([^()\\\\ ]{0,16})\\((?:.|\\n)*?\\)\\1\""}]},{"begin":"\\(","end":"\\)","keywords":{"$ref":"#keywords"},"contains":[{"$ref":"#contains.0.contains.0"},{"$ref":"#contains.0.contains.1"},{"$ref":"#contains.0.contains.2"},{"$ref":"#contains.0.contains.3"},{"$ref":"#contains.0.contains.4"},"self"],"relevance":0}],"relevance":0},{"className":"function","begin":"((decltype\\(auto\\)|(?:[a-zA-Z_]\\w*::)?[a-zA-Z_]\\w*(?:<.*?>)?)[\\*&\\s]+)+(?:[a-zA-Z_]\\w*::)?[a-zA-Z]\\w*\\s*\\(","returnBegin":true,"end":"[{;=]","excludeEnd":true,"keywords":{"$ref":"#keywords"},"illegal":"[^\\w\\s\\*&:<>]","contains":[{"begin":"decltype\\(auto\\)","keywords":{"$ref":"#keywords"},"relevance":0},{"begin":"(?:[a-zA-Z_]\\w*::)?[a-zA-Z]\\w*\\s*\\(","returnBegin":true,"contains":[{"className":"title","begin":"(?:[a-zA-Z_]\\w*::)?[a-zA-Z]\\w*","relevance":0}],"relevance":0},{"className":"params","begin":"\\(","end":"\\)","keywords":{"$ref":"#keywords"},"relevance":0,"contains":[{"$ref":"#contains.0.contains.1"},{"$ref":"#contains.0.contains.2"},{"$ref":"#contains.0.contains.4"},{"$ref":"#contains.0.contains.3"},{"$ref":"#contains.0.contains.0"},{"begin":"\\(","end":"\\)","keywords":{"$ref":"#keywords"},"relevance":0,"contains":["self",{"$ref":"#contains.0.contains.1"},{"$ref":"#contains.0.contains.2"},{"$ref":"#contains.0.contains.4"},{"$ref":"#contains.0.contains.3"},{"$ref":"#contains.0.contains.0"}]}]},{"$ref":"#contains.0.contains.0"},{"$ref":"#contains.0.contains.1"},{"$ref":"#contains.0.contains.2"},{"className":"meta","begin":"#\\s*[a-z]+\\b","end":"$","keywords":{"meta-keyword":"if else elif endif define undef warning error line pragma _Pragma ifdef ifndef include"},"contains":[{"begin":"\\\\\\n","relevance":0},{"className":"meta-string","variants":{"$ref":"#contains.0.contains.4.variants"}},{"className":"meta-string","begin":"<.*?>","end":"$","illegal":"\\n"},{"$ref":"#contains.0.contains.1"},{"$ref":"#contains.0.contains.2"}]}]},{"$ref":"#contains.0.contains.0"},{"$ref":"#contains.0.contains.1"},{"$ref":"#contains.0.contains.2"},{"$ref":"#contains.0.contains.3"},{"$ref":"#contains.0.contains.4"},{"$ref":"#contains.1.contains.6"},{"begin":"\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<","end":">","keywords":{"$ref":"#keywords"},"contains":["self",{"$ref":"#contains.0.contains.0"}]},{"begin":"[a-zA-Z]\\w*::","keywords":{"$ref":"#keywords"}},{"className":"class","beginKeywords":"class struct","end":"[{;:]","contains":[{"begin":"<","end":">","contains":["self"]},{"className":"title","begin":"[a-zA-Z]\\w*","relevance":0}]}],"exports":{"preprocessor":{"$ref":"#contains.1.contains.6"},"strings":{"$ref":"#contains.0.contains.4"},"keywords":{"$ref":"#keywords"}}}
Loading

0 comments on commit 0e78600

Please sign in to comment.