highlight.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. Syntax highlighting with language autodetection.
  3. https://highlightjs.org/
  4. */
  5. (function(factory) {
  6. // Find the global object for export to both the browser and web workers.
  7. var globalObject = typeof window === 'object' && window ||
  8. typeof self === 'object' && self;
  9. // Setup highlight.js for different environments. First is Node.js or
  10. // CommonJS.
  11. if(typeof exports !== 'undefined') {
  12. factory(exports);
  13. } else if(globalObject) {
  14. // Export hljs globally even when using AMD for cases when this script
  15. // is loaded with others that may still expect a global hljs.
  16. globalObject.hljs = factory({});
  17. // Finally register the global hljs with AMD.
  18. if(typeof define === 'function' && define.amd) {
  19. define([], function() {
  20. return globalObject.hljs;
  21. });
  22. }
  23. }
  24. }(function(hljs) {
  25. // Convenience variables for build-in objects
  26. var ArrayProto = [],
  27. objectKeys = Object.keys;
  28. // Global internal variables used within the highlight.js library.
  29. var languages = {},
  30. aliases = {};
  31. // Regular expressions used throughout the highlight.js library.
  32. var noHighlightRe = /^(no-?highlight|plain|text)$/i,
  33. languagePrefixRe = /\blang(?:uage)?-([\w-]+)\b/i,
  34. fixMarkupRe = /((^(<[^>]+>|\t|)+|(?:\n)))/gm;
  35. var spanEndTag = '</span>';
  36. // Global options used when within external APIs. This is modified when
  37. // calling the `hljs.configure` function.
  38. var options = {
  39. classPrefix: 'hljs-',
  40. tabReplace: null,
  41. useBR: false,
  42. languages: undefined
  43. };
  44. /* Utility functions */
  45. function escape(value) {
  46. return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  47. }
  48. function tag(node) {
  49. return node.nodeName.toLowerCase();
  50. }
  51. function testRe(re, lexeme) {
  52. var match = re && re.exec(lexeme);
  53. return match && match.index === 0;
  54. }
  55. function isNotHighlighted(language) {
  56. return noHighlightRe.test(language);
  57. }
  58. function blockLanguage(block) {
  59. var i, match, length, _class;
  60. var classes = block.className + ' ';
  61. classes += block.parentNode ? block.parentNode.className : '';
  62. // language-* takes precedence over non-prefixed class names.
  63. match = languagePrefixRe.exec(classes);
  64. if (match) {
  65. return getLanguage(match[1]) ? match[1] : 'no-highlight';
  66. }
  67. classes = classes.split(/\s+/);
  68. for (i = 0, length = classes.length; i < length; i++) {
  69. _class = classes[i]
  70. if (isNotHighlighted(_class) || getLanguage(_class)) {
  71. return _class;
  72. }
  73. }
  74. }
  75. function inherit(parent) { // inherit(parent, override_obj, override_obj, ...)
  76. var key;
  77. var result = {};
  78. var objects = Array.prototype.slice.call(arguments, 1);
  79. for (key in parent)
  80. result[key] = parent[key];
  81. objects.forEach(function(obj) {
  82. for (key in obj)
  83. result[key] = obj[key];
  84. });
  85. return result;
  86. }
  87. /* Stream merging */
  88. function nodeStream(node) {
  89. var result = [];
  90. (function _nodeStream(node, offset) {
  91. for (var child = node.firstChild; child; child = child.nextSibling) {
  92. if (child.nodeType === 3)
  93. offset += child.nodeValue.length;
  94. else if (child.nodeType === 1) {
  95. result.push({
  96. event: 'start',
  97. offset: offset,
  98. node: child
  99. });
  100. offset = _nodeStream(child, offset);
  101. // Prevent void elements from having an end tag that would actually
  102. // double them in the output. There are more void elements in HTML
  103. // but we list only those realistically expected in code display.
  104. if (!tag(child).match(/br|hr|img|input/)) {
  105. result.push({
  106. event: 'stop',
  107. offset: offset,
  108. node: child
  109. });
  110. }
  111. }
  112. }
  113. return offset;
  114. })(node, 0);
  115. return result;
  116. }
  117. function mergeStreams(original, highlighted, value) {
  118. var processed = 0;
  119. var result = '';
  120. var nodeStack = [];
  121. function selectStream() {
  122. if (!original.length || !highlighted.length) {
  123. return original.length ? original : highlighted;
  124. }
  125. if (original[0].offset !== highlighted[0].offset) {
  126. return (original[0].offset < highlighted[0].offset) ? original : highlighted;
  127. }
  128. /*
  129. To avoid starting the stream just before it should stop the order is
  130. ensured that original always starts first and closes last:
  131. if (event1 == 'start' && event2 == 'start')
  132. return original;
  133. if (event1 == 'start' && event2 == 'stop')
  134. return highlighted;
  135. if (event1 == 'stop' && event2 == 'start')
  136. return original;
  137. if (event1 == 'stop' && event2 == 'stop')
  138. return highlighted;
  139. ... which is collapsed to:
  140. */
  141. return highlighted[0].event === 'start' ? original : highlighted;
  142. }
  143. function open(node) {
  144. function attr_str(a) {return ' ' + a.nodeName + '="' + escape(a.value).replace('"', '&quot;') + '"';}
  145. result += '<' + tag(node) + ArrayProto.map.call(node.attributes, attr_str).join('') + '>';
  146. }
  147. function close(node) {
  148. result += '</' + tag(node) + '>';
  149. }
  150. function render(event) {
  151. (event.event === 'start' ? open : close)(event.node);
  152. }
  153. while (original.length || highlighted.length) {
  154. var stream = selectStream();
  155. result += escape(value.substring(processed, stream[0].offset));
  156. processed = stream[0].offset;
  157. if (stream === original) {
  158. /*
  159. On any opening or closing tag of the original markup we first close
  160. the entire highlighted node stack, then render the original tag along
  161. with all the following original tags at the same offset and then
  162. reopen all the tags on the highlighted stack.
  163. */
  164. nodeStack.reverse().forEach(close);
  165. do {
  166. render(stream.splice(0, 1)[0]);
  167. stream = selectStream();
  168. } while (stream === original && stream.length && stream[0].offset === processed);
  169. nodeStack.reverse().forEach(open);
  170. } else {
  171. if (stream[0].event === 'start') {
  172. nodeStack.push(stream[0].node);
  173. } else {
  174. nodeStack.pop();
  175. }
  176. render(stream.splice(0, 1)[0]);
  177. }
  178. }
  179. return result + escape(value.substr(processed));
  180. }
  181. /* Initialization */
  182. function expand_mode(mode) {
  183. if (mode.variants && !mode.cached_variants) {
  184. mode.cached_variants = mode.variants.map(function(variant) {
  185. return inherit(mode, {variants: null}, variant);
  186. });
  187. }
  188. return mode.cached_variants || (mode.endsWithParent && [inherit(mode)]) || [mode];
  189. }
  190. function compileLanguage(language) {
  191. function reStr(re) {
  192. return (re && re.source) || re;
  193. }
  194. function langRe(value, global) {
  195. return new RegExp(
  196. reStr(value),
  197. 'm' + (language.case_insensitive ? 'i' : '') + (global ? 'g' : '')
  198. );
  199. }
  200. function compileMode(mode, parent) {
  201. if (mode.compiled)
  202. return;
  203. mode.compiled = true;
  204. mode.keywords = mode.keywords || mode.beginKeywords;
  205. if (mode.keywords) {
  206. var compiled_keywords = {};
  207. var flatten = function(className, str) {
  208. if (language.case_insensitive) {
  209. str = str.toLowerCase();
  210. }
  211. str.split(' ').forEach(function(kw) {
  212. var pair = kw.split('|');
  213. compiled_keywords[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1];
  214. });
  215. };
  216. if (typeof mode.keywords === 'string') { // string
  217. flatten('keyword', mode.keywords);
  218. } else {
  219. objectKeys(mode.keywords).forEach(function (className) {
  220. flatten(className, mode.keywords[className]);
  221. });
  222. }
  223. mode.keywords = compiled_keywords;
  224. }
  225. mode.lexemesRe = langRe(mode.lexemes || /\w+/, true);
  226. if (parent) {
  227. if (mode.beginKeywords) {
  228. mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')\\b';
  229. }
  230. if (!mode.begin)
  231. mode.begin = /\B|\b/;
  232. mode.beginRe = langRe(mode.begin);
  233. if (mode.endSameAsBegin)
  234. mode.end = mode.begin;
  235. if (!mode.end && !mode.endsWithParent)
  236. mode.end = /\B|\b/;
  237. if (mode.end)
  238. mode.endRe = langRe(mode.end);
  239. mode.terminator_end = reStr(mode.end) || '';
  240. if (mode.endsWithParent && parent.terminator_end)
  241. mode.terminator_end += (mode.end ? '|' : '') + parent.terminator_end;
  242. }
  243. if (mode.illegal)
  244. mode.illegalRe = langRe(mode.illegal);
  245. if (mode.relevance == null)
  246. mode.relevance = 1;
  247. if (!mode.contains) {
  248. mode.contains = [];
  249. }
  250. mode.contains = Array.prototype.concat.apply([], mode.contains.map(function(c) {
  251. return expand_mode(c === 'self' ? mode : c)
  252. }));
  253. mode.contains.forEach(function(c) {compileMode(c, mode);});
  254. if (mode.starts) {
  255. compileMode(mode.starts, parent);
  256. }
  257. var terminators =
  258. mode.contains.map(function(c) {
  259. return c.beginKeywords ? '\\.?(' + c.begin + ')\\.?' : c.begin;
  260. })
  261. .concat([mode.terminator_end, mode.illegal])
  262. .map(reStr)
  263. .filter(Boolean);
  264. mode.terminators = terminators.length ? langRe(terminators.join('|'), true) : {exec: function(/*s*/) {return null;}};
  265. }
  266. compileMode(language);
  267. }
  268. /*
  269. Core highlighting function. Accepts a language name, or an alias, and a
  270. string with the code to highlight. Returns an object with the following
  271. properties:
  272. - relevance (int)
  273. - value (an HTML string with highlighting markup)
  274. */
  275. function highlight(name, value, ignore_illegals, continuation) {
  276. function escapeRe(value) {
  277. return new RegExp(value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'm');
  278. }
  279. function subMode(lexeme, mode) {
  280. var i, length;
  281. for (i = 0, length = mode.contains.length; i < length; i++) {
  282. if (testRe(mode.contains[i].beginRe, lexeme)) {
  283. if (mode.contains[i].endSameAsBegin) {
  284. mode.contains[i].endRe = escapeRe( mode.contains[i].beginRe.exec(lexeme)[0] );
  285. }
  286. return mode.contains[i];
  287. }
  288. }
  289. }
  290. function endOfMode(mode, lexeme) {
  291. if (testRe(mode.endRe, lexeme)) {
  292. while (mode.endsParent && mode.parent) {
  293. mode = mode.parent;
  294. }
  295. return mode;
  296. }
  297. if (mode.endsWithParent) {
  298. return endOfMode(mode.parent, lexeme);
  299. }
  300. }
  301. function isIllegal(lexeme, mode) {
  302. return !ignore_illegals && testRe(mode.illegalRe, lexeme);
  303. }
  304. function keywordMatch(mode, match) {
  305. var match_str = language.case_insensitive ? match[0].toLowerCase() : match[0];
  306. return mode.keywords.hasOwnProperty(match_str) && mode.keywords[match_str];
  307. }
  308. function buildSpan(classname, insideSpan, leaveOpen, noPrefix) {
  309. var classPrefix = noPrefix ? '' : options.classPrefix,
  310. openSpan = '<span class="' + classPrefix,
  311. closeSpan = leaveOpen ? '' : spanEndTag
  312. openSpan += classname + '">';
  313. return openSpan + insideSpan + closeSpan;
  314. }
  315. function processKeywords() {
  316. var keyword_match, last_index, match, result;
  317. if (!top.keywords)
  318. return escape(mode_buffer);
  319. result = '';
  320. last_index = 0;
  321. top.lexemesRe.lastIndex = 0;
  322. match = top.lexemesRe.exec(mode_buffer);
  323. while (match) {
  324. result += escape(mode_buffer.substring(last_index, match.index));
  325. keyword_match = keywordMatch(top, match);
  326. if (keyword_match) {
  327. relevance += keyword_match[1];
  328. result += buildSpan(keyword_match[0], escape(match[0]));
  329. } else {
  330. result += escape(match[0]);
  331. }
  332. last_index = top.lexemesRe.lastIndex;
  333. match = top.lexemesRe.exec(mode_buffer);
  334. }
  335. return result + escape(mode_buffer.substr(last_index));
  336. }
  337. function processSubLanguage() {
  338. var explicit = typeof top.subLanguage === 'string';
  339. if (explicit && !languages[top.subLanguage]) {
  340. return escape(mode_buffer);
  341. }
  342. var result = explicit ?
  343. highlight(top.subLanguage, mode_buffer, true, continuations[top.subLanguage]) :
  344. highlightAuto(mode_buffer, top.subLanguage.length ? top.subLanguage : undefined);
  345. // Counting embedded language score towards the host language may be disabled
  346. // with zeroing the containing mode relevance. Usecase in point is Markdown that
  347. // allows XML everywhere and makes every XML snippet to have a much larger Markdown
  348. // score.
  349. if (top.relevance > 0) {
  350. relevance += result.relevance;
  351. }
  352. if (explicit) {
  353. continuations[top.subLanguage] = result.top;
  354. }
  355. return buildSpan(result.language, result.value, false, true);
  356. }
  357. function processBuffer() {
  358. result += (top.subLanguage != null ? processSubLanguage() : processKeywords());
  359. mode_buffer = '';
  360. }
  361. function startNewMode(mode) {
  362. result += mode.className? buildSpan(mode.className, '', true): '';
  363. top = Object.create(mode, {parent: {value: top}});
  364. }
  365. function processLexeme(buffer, lexeme) {
  366. mode_buffer += buffer;
  367. if (lexeme == null) {
  368. processBuffer();
  369. return 0;
  370. }
  371. var new_mode = subMode(lexeme, top);
  372. if (new_mode) {
  373. if (new_mode.skip) {
  374. mode_buffer += lexeme;
  375. } else {
  376. if (new_mode.excludeBegin) {
  377. mode_buffer += lexeme;
  378. }
  379. processBuffer();
  380. if (!new_mode.returnBegin && !new_mode.excludeBegin) {
  381. mode_buffer = lexeme;
  382. }
  383. }
  384. startNewMode(new_mode, lexeme);
  385. return new_mode.returnBegin ? 0 : lexeme.length;
  386. }
  387. var end_mode = endOfMode(top, lexeme);
  388. if (end_mode) {
  389. var origin = top;
  390. if (origin.skip) {
  391. mode_buffer += lexeme;
  392. } else {
  393. if (!(origin.returnEnd || origin.excludeEnd)) {
  394. mode_buffer += lexeme;
  395. }
  396. processBuffer();
  397. if (origin.excludeEnd) {
  398. mode_buffer = lexeme;
  399. }
  400. }
  401. do {
  402. if (top.className) {
  403. result += spanEndTag;
  404. }
  405. if (!top.skip && !top.subLanguage) {
  406. relevance += top.relevance;
  407. }
  408. top = top.parent;
  409. } while (top !== end_mode.parent);
  410. if (end_mode.starts) {
  411. if (end_mode.endSameAsBegin) {
  412. end_mode.starts.endRe = end_mode.endRe;
  413. }
  414. startNewMode(end_mode.starts, '');
  415. }
  416. return origin.returnEnd ? 0 : lexeme.length;
  417. }
  418. if (isIllegal(lexeme, top))
  419. throw new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.className || '<unnamed>') + '"');
  420. /*
  421. Parser should not reach this point as all types of lexemes should be caught
  422. earlier, but if it does due to some bug make sure it advances at least one
  423. character forward to prevent infinite looping.
  424. */
  425. mode_buffer += lexeme;
  426. return lexeme.length || 1;
  427. }
  428. var language = getLanguage(name);
  429. if (!language) {
  430. throw new Error('Unknown language: "' + name + '"');
  431. }
  432. compileLanguage(language);
  433. var top = continuation || language;
  434. var continuations = {}; // keep continuations for sub-languages
  435. var result = '', current;
  436. for(current = top; current !== language; current = current.parent) {
  437. if (current.className) {
  438. result = buildSpan(current.className, '', true) + result;
  439. }
  440. }
  441. var mode_buffer = '';
  442. var relevance = 0;
  443. try {
  444. var match, count, index = 0;
  445. while (true) {
  446. top.terminators.lastIndex = index;
  447. match = top.terminators.exec(value);
  448. if (!match)
  449. break;
  450. count = processLexeme(value.substring(index, match.index), match[0]);
  451. index = match.index + count;
  452. }
  453. processLexeme(value.substr(index));
  454. for(current = top; current.parent; current = current.parent) { // close dangling modes
  455. if (current.className) {
  456. result += spanEndTag;
  457. }
  458. }
  459. return {
  460. relevance: relevance,
  461. value: result,
  462. language: name,
  463. top: top
  464. };
  465. } catch (e) {
  466. if (e.message && e.message.indexOf('Illegal') !== -1) {
  467. return {
  468. relevance: 0,
  469. value: escape(value)
  470. };
  471. } else {
  472. throw e;
  473. }
  474. }
  475. }
  476. /*
  477. Highlighting with language detection. Accepts a string with the code to
  478. highlight. Returns an object with the following properties:
  479. - language (detected language)
  480. - relevance (int)
  481. - value (an HTML string with highlighting markup)
  482. - second_best (object with the same structure for second-best heuristically
  483. detected language, may be absent)
  484. */
  485. function highlightAuto(text, languageSubset) {
  486. languageSubset = languageSubset || options.languages || objectKeys(languages);
  487. var result = {
  488. relevance: 0,
  489. value: escape(text)
  490. };
  491. var second_best = result;
  492. languageSubset.filter(getLanguage).filter(autoDetection).forEach(function(name) {
  493. var current = highlight(name, text, false);
  494. current.language = name;
  495. if (current.relevance > second_best.relevance) {
  496. second_best = current;
  497. }
  498. if (current.relevance > result.relevance) {
  499. second_best = result;
  500. result = current;
  501. }
  502. });
  503. if (second_best.language) {
  504. result.second_best = second_best;
  505. }
  506. return result;
  507. }
  508. /*
  509. Post-processing of the highlighted markup:
  510. - replace TABs with something more useful
  511. - replace real line-breaks with '<br>' for non-pre containers
  512. */
  513. function fixMarkup(value) {
  514. return !(options.tabReplace || options.useBR)
  515. ? value
  516. : value.replace(fixMarkupRe, function(match, p1) {
  517. if (options.useBR && match === '\n') {
  518. return '<br>';
  519. } else if (options.tabReplace) {
  520. return p1.replace(/\t/g, options.tabReplace);
  521. }
  522. return '';
  523. });
  524. }
  525. function buildClassName(prevClassName, currentLang, resultLang) {
  526. var language = currentLang ? aliases[currentLang] : resultLang,
  527. result = [prevClassName.trim()];
  528. if (!prevClassName.match(/\bhljs\b/)) {
  529. result.push('hljs');
  530. }
  531. if (prevClassName.indexOf(language) === -1) {
  532. result.push(language);
  533. }
  534. return result.join(' ').trim();
  535. }
  536. /*
  537. Applies highlighting to a DOM node containing code. Accepts a DOM node and
  538. two optional parameters for fixMarkup.
  539. */
  540. function highlightBlock(block) {
  541. var node, originalStream, result, resultNode, text;
  542. var language = blockLanguage(block);
  543. if (isNotHighlighted(language))
  544. return;
  545. if (options.useBR) {
  546. node = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
  547. node.innerHTML = block.innerHTML.replace(/\n/g, '').replace(/<br[ \/]*>/g, '\n');
  548. } else {
  549. node = block;
  550. }
  551. text = node.textContent;
  552. result = language ? highlight(language, text, true) : highlightAuto(text);
  553. originalStream = nodeStream(node);
  554. if (originalStream.length) {
  555. resultNode = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
  556. resultNode.innerHTML = result.value;
  557. result.value = mergeStreams(originalStream, nodeStream(resultNode), text);
  558. }
  559. result.value = fixMarkup(result.value);
  560. block.innerHTML = result.value;
  561. block.className = buildClassName(block.className, language, result.language);
  562. block.result = {
  563. language: result.language,
  564. re: result.relevance
  565. };
  566. if (result.second_best) {
  567. block.second_best = {
  568. language: result.second_best.language,
  569. re: result.second_best.relevance
  570. };
  571. }
  572. }
  573. /*
  574. Updates highlight.js global options with values passed in the form of an object.
  575. */
  576. function configure(user_options) {
  577. options = inherit(options, user_options);
  578. }
  579. /*
  580. Applies highlighting to all <pre><code>..</code></pre> blocks on a page.
  581. */
  582. function initHighlighting() {
  583. if (initHighlighting.called)
  584. return;
  585. initHighlighting.called = true;
  586. var blocks = document.querySelectorAll('pre code');
  587. ArrayProto.forEach.call(blocks, highlightBlock);
  588. }
  589. /*
  590. Attaches highlighting to the page load event.
  591. */
  592. function initHighlightingOnLoad() {
  593. addEventListener('DOMContentLoaded', initHighlighting, false);
  594. addEventListener('load', initHighlighting, false);
  595. }
  596. function registerLanguage(name, language) {
  597. var lang = languages[name] = language(hljs);
  598. if (lang.aliases) {
  599. lang.aliases.forEach(function(alias) {aliases[alias] = name;});
  600. }
  601. }
  602. function listLanguages() {
  603. return objectKeys(languages);
  604. }
  605. function getLanguage(name) {
  606. name = (name || '').toLowerCase();
  607. return languages[name] || languages[aliases[name]];
  608. }
  609. function autoDetection(name) {
  610. var lang = getLanguage(name);
  611. return lang && !lang.disableAutodetect;
  612. }
  613. /* Interface definition */
  614. hljs.highlight = highlight;
  615. hljs.highlightAuto = highlightAuto;
  616. hljs.fixMarkup = fixMarkup;
  617. hljs.highlightBlock = highlightBlock;
  618. hljs.configure = configure;
  619. hljs.initHighlighting = initHighlighting;
  620. hljs.initHighlightingOnLoad = initHighlightingOnLoad;
  621. hljs.registerLanguage = registerLanguage;
  622. hljs.listLanguages = listLanguages;
  623. hljs.getLanguage = getLanguage;
  624. hljs.autoDetection = autoDetection;
  625. hljs.inherit = inherit;
  626. // Common regexps
  627. hljs.IDENT_RE = '[a-zA-Z]\\w*';
  628. hljs.UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*';
  629. hljs.NUMBER_RE = '\\b\\d+(\\.\\d+)?';
  630. hljs.C_NUMBER_RE = '(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)'; // 0x..., 0..., decimal, float
  631. hljs.BINARY_NUMBER_RE = '\\b(0b[01]+)'; // 0b...
  632. hljs.RE_STARTERS_RE = '!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~';
  633. // Common modes
  634. hljs.BACKSLASH_ESCAPE = {
  635. begin: '\\\\[\\s\\S]', relevance: 0
  636. };
  637. hljs.APOS_STRING_MODE = {
  638. className: 'string',
  639. begin: '\'', end: '\'',
  640. illegal: '\\n',
  641. contains: [hljs.BACKSLASH_ESCAPE]
  642. };
  643. hljs.QUOTE_STRING_MODE = {
  644. className: 'string',
  645. begin: '"', end: '"',
  646. illegal: '\\n',
  647. contains: [hljs.BACKSLASH_ESCAPE]
  648. };
  649. hljs.PHRASAL_WORDS_MODE = {
  650. 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/
  651. };
  652. hljs.COMMENT = function (begin, end, inherits) {
  653. var mode = hljs.inherit(
  654. {
  655. className: 'comment',
  656. begin: begin, end: end,
  657. contains: []
  658. },
  659. inherits || {}
  660. );
  661. mode.contains.push(hljs.PHRASAL_WORDS_MODE);
  662. mode.contains.push({
  663. className: 'doctag',
  664. begin: '(?:TODO|FIXME|NOTE|BUG|XXX):',
  665. relevance: 0
  666. });
  667. return mode;
  668. };
  669. hljs.C_LINE_COMMENT_MODE = hljs.COMMENT('//', '$');
  670. hljs.C_BLOCK_COMMENT_MODE = hljs.COMMENT('/\\*', '\\*/');
  671. hljs.HASH_COMMENT_MODE = hljs.COMMENT('#', '$');
  672. hljs.NUMBER_MODE = {
  673. className: 'number',
  674. begin: hljs.NUMBER_RE,
  675. relevance: 0
  676. };
  677. hljs.C_NUMBER_MODE = {
  678. className: 'number',
  679. begin: hljs.C_NUMBER_RE,
  680. relevance: 0
  681. };
  682. hljs.BINARY_NUMBER_MODE = {
  683. className: 'number',
  684. begin: hljs.BINARY_NUMBER_RE,
  685. relevance: 0
  686. };
  687. hljs.CSS_NUMBER_MODE = {
  688. className: 'number',
  689. begin: hljs.NUMBER_RE + '(' +
  690. '%|em|ex|ch|rem' +
  691. '|vw|vh|vmin|vmax' +
  692. '|cm|mm|in|pt|pc|px' +
  693. '|deg|grad|rad|turn' +
  694. '|s|ms' +
  695. '|Hz|kHz' +
  696. '|dpi|dpcm|dppx' +
  697. ')?',
  698. relevance: 0
  699. };
  700. hljs.REGEXP_MODE = {
  701. className: 'regexp',
  702. begin: /\//, end: /\/[gimuy]*/,
  703. illegal: /\n/,
  704. contains: [
  705. hljs.BACKSLASH_ESCAPE,
  706. {
  707. begin: /\[/, end: /\]/,
  708. relevance: 0,
  709. contains: [hljs.BACKSLASH_ESCAPE]
  710. }
  711. ]
  712. };
  713. hljs.TITLE_MODE = {
  714. className: 'title',
  715. begin: hljs.IDENT_RE,
  716. relevance: 0
  717. };
  718. hljs.UNDERSCORE_TITLE_MODE = {
  719. className: 'title',
  720. begin: hljs.UNDERSCORE_IDENT_RE,
  721. relevance: 0
  722. };
  723. hljs.METHOD_GUARD = {
  724. // excludes method names from keyword processing
  725. begin: '\\.\\s*' + hljs.UNDERSCORE_IDENT_RE,
  726. relevance: 0
  727. };
  728. return hljs;
  729. }));