1 |
|
2 |
|
3 | function deepFreeze(obj) {
|
4 | Object.freeze(obj);
|
5 |
|
6 | var objIsFunction = typeof obj === 'function';
|
7 |
|
8 | Object.getOwnPropertyNames(obj).forEach(function(prop) {
|
9 | if (Object.hasOwnProperty.call(obj, prop)
|
10 | && obj[prop] !== null
|
11 | && (typeof obj[prop] === "object" || typeof obj[prop] === "function")
|
12 |
|
13 |
|
14 | && (objIsFunction ? prop !== 'caller' && prop !== 'callee' && prop !== 'arguments' : true)
|
15 | && !Object.isFrozen(obj[prop])) {
|
16 | deepFreeze(obj[prop]);
|
17 | }
|
18 | });
|
19 |
|
20 | return obj;
|
21 | }
|
22 |
|
23 | class Response {
|
24 | |
25 |
|
26 |
|
27 | constructor(mode) {
|
28 |
|
29 | if (mode.data === undefined) mode.data = {};
|
30 |
|
31 | this.data = mode.data;
|
32 | }
|
33 |
|
34 | ignoreMatch() {
|
35 | this.ignore = true;
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function escapeHTML(value) {
|
44 | return value
|
45 | .replace(/&/g, '&')
|
46 | .replace(/</g, '<')
|
47 | .replace(/>/g, '>')
|
48 | .replace(/"/g, '"')
|
49 | .replace(/'/g, ''');
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function inherit(original, ...objects) {
|
61 |
|
62 | var result = {};
|
63 |
|
64 | for (const key in original) {
|
65 | result[key] = original[key];
|
66 | }
|
67 | objects.forEach(function(obj) {
|
68 | for (const key in obj) {
|
69 | result[key] = obj[key];
|
70 | }
|
71 | });
|
72 | return (result);
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function tag(node) {
|
88 | return node.nodeName.toLowerCase();
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function nodeStream(node) {
|
95 |
|
96 | var result = [];
|
97 | (function _nodeStream(node, offset) {
|
98 | for (var child = node.firstChild; child; child = child.nextSibling) {
|
99 | if (child.nodeType === 3) {
|
100 | offset += child.nodeValue.length;
|
101 | } else if (child.nodeType === 1) {
|
102 | result.push({
|
103 | event: 'start',
|
104 | offset: offset,
|
105 | node: child
|
106 | });
|
107 | offset = _nodeStream(child, offset);
|
108 |
|
109 |
|
110 |
|
111 | if (!tag(child).match(/br|hr|img|input/)) {
|
112 | result.push({
|
113 | event: 'stop',
|
114 | offset: offset,
|
115 | node: child
|
116 | });
|
117 | }
|
118 | }
|
119 | }
|
120 | return offset;
|
121 | })(node, 0);
|
122 | return result;
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function mergeStreams(original, highlighted, value) {
|
131 | var processed = 0;
|
132 | var result = '';
|
133 | var nodeStack = [];
|
134 |
|
135 | function selectStream() {
|
136 | if (!original.length || !highlighted.length) {
|
137 | return original.length ? original : highlighted;
|
138 | }
|
139 | if (original[0].offset !== highlighted[0].offset) {
|
140 | return (original[0].offset < highlighted[0].offset) ? original : highlighted;
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | return highlighted[0].event === 'start' ? original : highlighted;
|
159 | }
|
160 |
|
161 | |
162 |
|
163 |
|
164 | function open(node) {
|
165 |
|
166 | function attr_str(attr) {
|
167 | return ' ' + attr.nodeName + '="' + escapeHTML(attr.value) + '"';
|
168 | }
|
169 |
|
170 | result += '<' + tag(node) + [].map.call(node.attributes, attr_str).join('') + '>';
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 | function close(node) {
|
177 | result += '</' + tag(node) + '>';
|
178 | }
|
179 |
|
180 | |
181 |
|
182 |
|
183 | function render(event) {
|
184 | (event.event === 'start' ? open : close)(event.node);
|
185 | }
|
186 |
|
187 | while (original.length || highlighted.length) {
|
188 | var stream = selectStream();
|
189 | result += escapeHTML(value.substring(processed, stream[0].offset));
|
190 | processed = stream[0].offset;
|
191 | if (stream === original) {
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | nodeStack.reverse().forEach(close);
|
199 | do {
|
200 | render(stream.splice(0, 1)[0]);
|
201 | stream = selectStream();
|
202 | } while (stream === original && stream.length && stream[0].offset === processed);
|
203 | nodeStack.reverse().forEach(open);
|
204 | } else {
|
205 | if (stream[0].event === 'start') {
|
206 | nodeStack.push(stream[0].node);
|
207 | } else {
|
208 | nodeStack.pop();
|
209 | }
|
210 | render(stream.splice(0, 1)[0]);
|
211 | }
|
212 | }
|
213 | return result + escapeHTML(value.substr(processed));
|
214 | }
|
215 |
|
216 | var utils = Object.freeze({
|
217 | __proto__: null,
|
218 | escapeHTML: escapeHTML,
|
219 | inherit: inherit,
|
220 | nodeStream: nodeStream,
|
221 | mergeStreams: mergeStreams
|
222 | });
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | const SPAN_CLOSE = '</span>';
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | const emitsWrappingTags = (node) => {
|
243 | return !!node.kind;
|
244 | };
|
245 |
|
246 |
|
247 | class HTMLRenderer {
|
248 | |
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | constructor(parseTree, options) {
|
255 | this.buffer = "";
|
256 | this.classPrefix = options.classPrefix;
|
257 | parseTree.walk(this);
|
258 | }
|
259 |
|
260 | |
261 |
|
262 |
|
263 |
|
264 | addText(text) {
|
265 | this.buffer += escapeHTML(text);
|
266 | }
|
267 |
|
268 | |
269 |
|
270 |
|
271 |
|
272 | openNode(node) {
|
273 | if (!emitsWrappingTags(node)) return;
|
274 |
|
275 | let className = node.kind;
|
276 | if (!node.sublanguage) {
|
277 | className = `${this.classPrefix}${className}`;
|
278 | }
|
279 | this.span(className);
|
280 | }
|
281 |
|
282 | |
283 |
|
284 |
|
285 |
|
286 | closeNode(node) {
|
287 | if (!emitsWrappingTags(node)) return;
|
288 |
|
289 | this.buffer += SPAN_CLOSE;
|
290 | }
|
291 |
|
292 | |
293 |
|
294 |
|
295 | value() {
|
296 | return this.buffer;
|
297 | }
|
298 |
|
299 |
|
300 |
|
301 | |
302 |
|
303 |
|
304 |
|
305 | span(className) {
|
306 | this.buffer += `<span class="${className}">`;
|
307 | }
|
308 | }
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | class TokenTree {
|
315 | constructor() {
|
316 |
|
317 | this.rootNode = { children: [] };
|
318 | this.stack = [this.rootNode];
|
319 | }
|
320 |
|
321 | get top() {
|
322 | return this.stack[this.stack.length - 1];
|
323 | }
|
324 |
|
325 | get root() { return this.rootNode; }
|
326 |
|
327 |
|
328 | add(node) {
|
329 | this.top.children.push(node);
|
330 | }
|
331 |
|
332 |
|
333 | openNode(kind) {
|
334 |
|
335 | const node = { kind, children: [] };
|
336 | this.add(node);
|
337 | this.stack.push(node);
|
338 | }
|
339 |
|
340 | closeNode() {
|
341 | if (this.stack.length > 1) {
|
342 | return this.stack.pop();
|
343 | }
|
344 |
|
345 | return undefined;
|
346 | }
|
347 |
|
348 | closeAllNodes() {
|
349 | while (this.closeNode());
|
350 | }
|
351 |
|
352 | toJSON() {
|
353 | return JSON.stringify(this.rootNode, null, 4);
|
354 | }
|
355 |
|
356 | |
357 |
|
358 |
|
359 |
|
360 | walk(builder) {
|
361 |
|
362 | return this.constructor._walk(builder, this.rootNode);
|
363 |
|
364 |
|
365 | }
|
366 |
|
367 | |
368 |
|
369 |
|
370 |
|
371 | static _walk(builder, node) {
|
372 | if (typeof node === "string") {
|
373 | builder.addText(node);
|
374 | } else if (node.children) {
|
375 | builder.openNode(node);
|
376 | node.children.forEach((child) => this._walk(builder, child));
|
377 | builder.closeNode(node);
|
378 | }
|
379 | return builder;
|
380 | }
|
381 |
|
382 | |
383 |
|
384 |
|
385 | static _collapse(node) {
|
386 | if (typeof node === "string") return;
|
387 | if (!node.children) return;
|
388 |
|
389 | if (node.children.every(el => typeof el === "string")) {
|
390 |
|
391 |
|
392 | node.children = [node.children.join("")];
|
393 | } else {
|
394 | node.children.forEach((child) => {
|
395 | TokenTree._collapse(child);
|
396 | });
|
397 | }
|
398 | }
|
399 | }
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | class TokenTreeEmitter extends TokenTree {
|
422 | |
423 |
|
424 |
|
425 | constructor(options) {
|
426 | super();
|
427 | this.options = options;
|
428 | }
|
429 |
|
430 | |
431 |
|
432 |
|
433 |
|
434 | addKeyword(text, kind) {
|
435 | if (text === "") { return; }
|
436 |
|
437 | this.openNode(kind);
|
438 | this.addText(text);
|
439 | this.closeNode();
|
440 | }
|
441 |
|
442 | |
443 |
|
444 |
|
445 | addText(text) {
|
446 | if (text === "") { return; }
|
447 |
|
448 | this.add(text);
|
449 | }
|
450 |
|
451 | |
452 |
|
453 |
|
454 |
|
455 | addSublanguage(emitter, name) {
|
456 |
|
457 | const node = emitter.root;
|
458 | node.kind = name;
|
459 | node.sublanguage = true;
|
460 | this.add(node);
|
461 | }
|
462 |
|
463 | toHTML() {
|
464 | const renderer = new HTMLRenderer(this, this.options);
|
465 | return renderer.value();
|
466 | }
|
467 |
|
468 | finalize() {
|
469 | return true;
|
470 | }
|
471 | }
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 | function escape(value) {
|
478 | return new RegExp(value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'), 'm');
|
479 | }
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 | function source(re) {
|
486 | if (!re) return null;
|
487 | if (typeof re === "string") return re;
|
488 |
|
489 | return re.source;
|
490 | }
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | function concat(...args) {
|
497 | const joined = args.map((x) => source(x)).join("");
|
498 | return joined;
|
499 | }
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 | function countMatchGroups(re) {
|
506 | return (new RegExp(re.toString() + '|')).exec('').length - 1;
|
507 | }
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | function startsWith(re, lexeme) {
|
515 | var match = re && re.exec(lexeme);
|
516 | return match && match.index === 0;
|
517 | }
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 | function join(regexps, separator = "|") {
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 | var backreferenceRe = /\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./;
|
538 | var numCaptures = 0;
|
539 | var ret = '';
|
540 | for (var i = 0; i < regexps.length; i++) {
|
541 | numCaptures += 1;
|
542 | var offset = numCaptures;
|
543 | var re = source(regexps[i]);
|
544 | if (i > 0) {
|
545 | ret += separator;
|
546 | }
|
547 | ret += "(";
|
548 | while (re.length > 0) {
|
549 | var match = backreferenceRe.exec(re);
|
550 | if (match == null) {
|
551 | ret += re;
|
552 | break;
|
553 | }
|
554 | ret += re.substring(0, match.index);
|
555 | re = re.substring(match.index + match[0].length);
|
556 | if (match[0][0] === '\\' && match[1]) {
|
557 |
|
558 | ret += '\\' + String(Number(match[1]) + offset);
|
559 | } else {
|
560 | ret += match[0];
|
561 | if (match[0] === '(') {
|
562 | numCaptures++;
|
563 | }
|
564 | }
|
565 | }
|
566 | ret += ")";
|
567 | }
|
568 | return ret;
|
569 | }
|
570 |
|
571 |
|
572 | const IDENT_RE = '[a-zA-Z]\\w*';
|
573 | const UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*';
|
574 | const NUMBER_RE = '\\b\\d+(\\.\\d+)?';
|
575 | const C_NUMBER_RE = '(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)';
|
576 | const BINARY_NUMBER_RE = '\\b(0b[01]+)';
|
577 | const RE_STARTERS_RE = '!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~';
|
578 |
|
579 |
|
580 |
|
581 |
|
582 | const SHEBANG = (opts = {}) => {
|
583 | const beginShebang = /^#![ ]*\//;
|
584 | if (opts.binary) {
|
585 | opts.begin = concat(
|
586 | beginShebang,
|
587 | /.*\b/,
|
588 | opts.binary,
|
589 | /\b.*/);
|
590 | }
|
591 | return inherit({
|
592 | className: 'meta',
|
593 | begin: beginShebang,
|
594 | end: /$/,
|
595 | relevance: 0,
|
596 |
|
597 | "on:begin": (m, resp) => {
|
598 | if (m.index !== 0) resp.ignoreMatch();
|
599 | }
|
600 | }, opts);
|
601 | };
|
602 |
|
603 |
|
604 | const BACKSLASH_ESCAPE = {
|
605 | begin: '\\\\[\\s\\S]', relevance: 0
|
606 | };
|
607 | const APOS_STRING_MODE = {
|
608 | className: 'string',
|
609 | begin: '\'',
|
610 | end: '\'',
|
611 | illegal: '\\n',
|
612 | contains: [BACKSLASH_ESCAPE]
|
613 | };
|
614 | const QUOTE_STRING_MODE = {
|
615 | className: 'string',
|
616 | begin: '"',
|
617 | end: '"',
|
618 | illegal: '\\n',
|
619 | contains: [BACKSLASH_ESCAPE]
|
620 | };
|
621 | const PHRASAL_WORDS_MODE = {
|
622 | 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/
|
623 | };
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 | const COMMENT = function(begin, end, modeOptions = {}) {
|
633 | var mode = inherit(
|
634 | {
|
635 | className: 'comment',
|
636 | begin,
|
637 | end,
|
638 | contains: []
|
639 | },
|
640 | modeOptions
|
641 | );
|
642 | mode.contains.push(PHRASAL_WORDS_MODE);
|
643 | mode.contains.push({
|
644 | className: 'doctag',
|
645 | begin: '(?:TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):',
|
646 | relevance: 0
|
647 | });
|
648 | return mode;
|
649 | };
|
650 | const C_LINE_COMMENT_MODE = COMMENT('//', '$');
|
651 | const C_BLOCK_COMMENT_MODE = COMMENT('/\\*', '\\*/');
|
652 | const HASH_COMMENT_MODE = COMMENT('#', '$');
|
653 | const NUMBER_MODE = {
|
654 | className: 'number',
|
655 | begin: NUMBER_RE,
|
656 | relevance: 0
|
657 | };
|
658 | const C_NUMBER_MODE = {
|
659 | className: 'number',
|
660 | begin: C_NUMBER_RE,
|
661 | relevance: 0
|
662 | };
|
663 | const BINARY_NUMBER_MODE = {
|
664 | className: 'number',
|
665 | begin: BINARY_NUMBER_RE,
|
666 | relevance: 0
|
667 | };
|
668 | const CSS_NUMBER_MODE = {
|
669 | className: 'number',
|
670 | begin: NUMBER_RE + '(' +
|
671 | '%|em|ex|ch|rem' +
|
672 | '|vw|vh|vmin|vmax' +
|
673 | '|cm|mm|in|pt|pc|px' +
|
674 | '|deg|grad|rad|turn' +
|
675 | '|s|ms' +
|
676 | '|Hz|kHz' +
|
677 | '|dpi|dpcm|dppx' +
|
678 | ')?',
|
679 | relevance: 0
|
680 | };
|
681 | const REGEXP_MODE = {
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 | begin: /(?=\/[^/\n]*\/)/,
|
689 | contains: [{
|
690 | className: 'regexp',
|
691 | begin: /\//,
|
692 | end: /\/[gimuy]*/,
|
693 | illegal: /\n/,
|
694 | contains: [
|
695 | BACKSLASH_ESCAPE,
|
696 | {
|
697 | begin: /\[/,
|
698 | end: /\]/,
|
699 | relevance: 0,
|
700 | contains: [BACKSLASH_ESCAPE]
|
701 | }
|
702 | ]
|
703 | }]
|
704 | };
|
705 | const TITLE_MODE = {
|
706 | className: 'title',
|
707 | begin: IDENT_RE,
|
708 | relevance: 0
|
709 | };
|
710 | const UNDERSCORE_TITLE_MODE = {
|
711 | className: 'title',
|
712 | begin: UNDERSCORE_IDENT_RE,
|
713 | relevance: 0
|
714 | };
|
715 | const METHOD_GUARD = {
|
716 |
|
717 | begin: '\\.\\s*' + UNDERSCORE_IDENT_RE,
|
718 | relevance: 0
|
719 | };
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 | const END_SAME_AS_BEGIN = function(mode) {
|
729 | return Object.assign(mode,
|
730 | {
|
731 |
|
732 | 'on:begin': (m, resp) => { resp.data._beginMatch = m[1]; },
|
733 |
|
734 | 'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch(); }
|
735 | });
|
736 | };
|
737 |
|
738 | var MODES = Object.freeze({
|
739 | __proto__: null,
|
740 | IDENT_RE: IDENT_RE,
|
741 | UNDERSCORE_IDENT_RE: UNDERSCORE_IDENT_RE,
|
742 | NUMBER_RE: NUMBER_RE,
|
743 | C_NUMBER_RE: C_NUMBER_RE,
|
744 | BINARY_NUMBER_RE: BINARY_NUMBER_RE,
|
745 | RE_STARTERS_RE: RE_STARTERS_RE,
|
746 | SHEBANG: SHEBANG,
|
747 | BACKSLASH_ESCAPE: BACKSLASH_ESCAPE,
|
748 | APOS_STRING_MODE: APOS_STRING_MODE,
|
749 | QUOTE_STRING_MODE: QUOTE_STRING_MODE,
|
750 | PHRASAL_WORDS_MODE: PHRASAL_WORDS_MODE,
|
751 | COMMENT: COMMENT,
|
752 | C_LINE_COMMENT_MODE: C_LINE_COMMENT_MODE,
|
753 | C_BLOCK_COMMENT_MODE: C_BLOCK_COMMENT_MODE,
|
754 | HASH_COMMENT_MODE: HASH_COMMENT_MODE,
|
755 | NUMBER_MODE: NUMBER_MODE,
|
756 | C_NUMBER_MODE: C_NUMBER_MODE,
|
757 | BINARY_NUMBER_MODE: BINARY_NUMBER_MODE,
|
758 | CSS_NUMBER_MODE: CSS_NUMBER_MODE,
|
759 | REGEXP_MODE: REGEXP_MODE,
|
760 | TITLE_MODE: TITLE_MODE,
|
761 | UNDERSCORE_TITLE_MODE: UNDERSCORE_TITLE_MODE,
|
762 | METHOD_GUARD: METHOD_GUARD,
|
763 | END_SAME_AS_BEGIN: END_SAME_AS_BEGIN
|
764 | });
|
765 |
|
766 |
|
767 | var COMMON_KEYWORDS = 'of and for in not or if then'.split(' ');
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 | function compileLanguage(language) {
|
780 | |
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | function langRe(value, global) {
|
787 | return new RegExp(
|
788 | source(value),
|
789 | 'm' + (language.case_insensitive ? 'i' : '') + (global ? 'g' : '')
|
790 | );
|
791 | }
|
792 |
|
793 | |
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 | class MultiRegex {
|
807 | constructor() {
|
808 | this.matchIndexes = {};
|
809 |
|
810 | this.regexes = [];
|
811 | this.matchAt = 1;
|
812 | this.position = 0;
|
813 | }
|
814 |
|
815 |
|
816 | addRule(re, opts) {
|
817 | opts.position = this.position++;
|
818 |
|
819 | this.matchIndexes[this.matchAt] = opts;
|
820 | this.regexes.push([opts, re]);
|
821 | this.matchAt += countMatchGroups(re) + 1;
|
822 | }
|
823 |
|
824 | compile() {
|
825 | if (this.regexes.length === 0) {
|
826 |
|
827 |
|
828 | this.exec = () => null;
|
829 | }
|
830 | const terminators = this.regexes.map(el => el[1]);
|
831 | this.matcherRe = langRe(join(terminators), true);
|
832 | this.lastIndex = 0;
|
833 | }
|
834 |
|
835 |
|
836 | exec(s) {
|
837 | this.matcherRe.lastIndex = this.lastIndex;
|
838 | const match = this.matcherRe.exec(s);
|
839 | if (!match) { return null; }
|
840 |
|
841 |
|
842 | const i = match.findIndex((el, i) => i > 0 && el !== undefined);
|
843 |
|
844 | const matchData = this.matchIndexes[i];
|
845 |
|
846 |
|
847 | match.splice(0, i);
|
848 |
|
849 | return Object.assign(match, matchData);
|
850 | }
|
851 | }
|
852 |
|
853 | |
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 | class ResumableMultiRegex {
|
885 | constructor() {
|
886 |
|
887 | this.rules = [];
|
888 |
|
889 | this.multiRegexes = [];
|
890 | this.count = 0;
|
891 |
|
892 | this.lastIndex = 0;
|
893 | this.regexIndex = 0;
|
894 | }
|
895 |
|
896 |
|
897 | getMatcher(index) {
|
898 | if (this.multiRegexes[index]) return this.multiRegexes[index];
|
899 |
|
900 | const matcher = new MultiRegex();
|
901 | this.rules.slice(index).forEach(([re, opts]) => matcher.addRule(re, opts));
|
902 | matcher.compile();
|
903 | this.multiRegexes[index] = matcher;
|
904 | return matcher;
|
905 | }
|
906 |
|
907 | considerAll() {
|
908 | this.regexIndex = 0;
|
909 | }
|
910 |
|
911 |
|
912 | addRule(re, opts) {
|
913 | this.rules.push([re, opts]);
|
914 | if (opts.type === "begin") this.count++;
|
915 | }
|
916 |
|
917 |
|
918 | exec(s) {
|
919 | const m = this.getMatcher(this.regexIndex);
|
920 | m.lastIndex = this.lastIndex;
|
921 | const result = m.exec(s);
|
922 | if (result) {
|
923 | this.regexIndex += result.position + 1;
|
924 | if (this.regexIndex === this.count) {
|
925 | this.regexIndex = 0;
|
926 | }
|
927 | }
|
928 |
|
929 |
|
930 | return result;
|
931 | }
|
932 | }
|
933 |
|
934 | |
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 | function buildModeRegex(mode) {
|
942 | const mm = new ResumableMultiRegex();
|
943 |
|
944 | mode.contains.forEach(term => mm.addRule(term.begin, { rule: term, type: "begin" }));
|
945 |
|
946 | if (mode.terminator_end) {
|
947 | mm.addRule(mode.terminator_end, { type: "end" });
|
948 | }
|
949 | if (mode.illegal) {
|
950 | mm.addRule(mode.illegal, { type: "illegal" });
|
951 | }
|
952 |
|
953 | return mm;
|
954 | }
|
955 |
|
956 |
|
957 | |
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 | function skipIfhasPrecedingOrTrailingDot(match, response) {
|
967 | const before = match.input[match.index - 1];
|
968 | const after = match.input[match.index + match[0].length];
|
969 | if (before === "." || after === ".") {
|
970 | response.ignoreMatch();
|
971 | }
|
972 | }
|
973 |
|
974 | |
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 | |
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 | function compileMode(mode, parent) {
|
1014 | const cmode = (mode);
|
1015 | if (mode.compiled) return cmode;
|
1016 | mode.compiled = true;
|
1017 |
|
1018 |
|
1019 | mode.__beforeBegin = null;
|
1020 |
|
1021 | mode.keywords = mode.keywords || mode.beginKeywords;
|
1022 |
|
1023 | let kw_pattern = null;
|
1024 | if (typeof mode.keywords === "object") {
|
1025 | kw_pattern = mode.keywords.$pattern;
|
1026 | delete mode.keywords.$pattern;
|
1027 | }
|
1028 |
|
1029 | if (mode.keywords) {
|
1030 | mode.keywords = compileKeywords(mode.keywords, language.case_insensitive);
|
1031 | }
|
1032 |
|
1033 |
|
1034 | if (mode.lexemes && kw_pattern) {
|
1035 | throw new Error("ERR: Prefer `keywords.$pattern` to `mode.lexemes`, BOTH are not allowed. (see mode reference) ");
|
1036 | }
|
1037 |
|
1038 |
|
1039 |
|
1040 | cmode.keywordPatternRe = langRe(mode.lexemes || kw_pattern || /\w+/, true);
|
1041 |
|
1042 | if (parent) {
|
1043 | if (mode.beginKeywords) {
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 | mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')(?=\\b|\\s)';
|
1050 | mode.__beforeBegin = skipIfhasPrecedingOrTrailingDot;
|
1051 | }
|
1052 | if (!mode.begin) mode.begin = /\B|\b/;
|
1053 | cmode.beginRe = langRe(mode.begin);
|
1054 | if (mode.endSameAsBegin) mode.end = mode.begin;
|
1055 | if (!mode.end && !mode.endsWithParent) mode.end = /\B|\b/;
|
1056 | if (mode.end) cmode.endRe = langRe(mode.end);
|
1057 | cmode.terminator_end = source(mode.end) || '';
|
1058 | if (mode.endsWithParent && parent.terminator_end) {
|
1059 | cmode.terminator_end += (mode.end ? '|' : '') + parent.terminator_end;
|
1060 | }
|
1061 | }
|
1062 | if (mode.illegal) cmode.illegalRe = langRe(mode.illegal);
|
1063 |
|
1064 | if (mode.relevance === undefined) mode.relevance = 1;
|
1065 | if (!mode.contains) mode.contains = [];
|
1066 |
|
1067 | mode.contains = [].concat(...mode.contains.map(function(c) {
|
1068 | return expand_or_clone_mode(c === 'self' ? mode : c);
|
1069 | }));
|
1070 | mode.contains.forEach(function(c) { compileMode( (c), cmode); });
|
1071 |
|
1072 | if (mode.starts) {
|
1073 | compileMode(mode.starts, parent);
|
1074 | }
|
1075 |
|
1076 | cmode.matcher = buildModeRegex(cmode);
|
1077 | return cmode;
|
1078 | }
|
1079 |
|
1080 |
|
1081 | if (language.contains && language.contains.includes('self')) {
|
1082 | throw new Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.");
|
1083 | }
|
1084 | return compileMode( (language));
|
1085 | }
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 | function dependencyOnParent(mode) {
|
1099 | if (!mode) return false;
|
1100 |
|
1101 | return mode.endsWithParent || dependencyOnParent(mode.starts);
|
1102 | }
|
1103 |
|
1104 |
|
1105 |
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 | function expand_or_clone_mode(mode) {
|
1115 | if (mode.variants && !mode.cached_variants) {
|
1116 | mode.cached_variants = mode.variants.map(function(variant) {
|
1117 | return inherit(mode, { variants: null }, variant);
|
1118 | });
|
1119 | }
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 | if (mode.cached_variants) {
|
1125 | return mode.cached_variants;
|
1126 | }
|
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 |
|
1132 | if (dependencyOnParent(mode)) {
|
1133 | return inherit(mode, { starts: mode.starts ? inherit(mode.starts) : null });
|
1134 | }
|
1135 |
|
1136 | if (Object.isFrozen(mode)) {
|
1137 | return inherit(mode);
|
1138 | }
|
1139 |
|
1140 |
|
1141 | return mode;
|
1142 | }
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 | function compileKeywords(rawKeywords, case_insensitive) {
|
1155 |
|
1156 | var compiled_keywords = {};
|
1157 |
|
1158 | if (typeof rawKeywords === 'string') {
|
1159 | splitAndCompile('keyword', rawKeywords);
|
1160 | } else {
|
1161 | Object.keys(rawKeywords).forEach(function(className) {
|
1162 | splitAndCompile(className, rawKeywords[className]);
|
1163 | });
|
1164 | }
|
1165 | return compiled_keywords;
|
1166 |
|
1167 |
|
1168 |
|
1169 | |
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 | function splitAndCompile(className, keywordList) {
|
1178 | if (case_insensitive) {
|
1179 | keywordList = keywordList.toLowerCase();
|
1180 | }
|
1181 | keywordList.split(' ').forEach(function(keyword) {
|
1182 | var pair = keyword.split('|');
|
1183 | compiled_keywords[pair[0]] = [className, scoreForKeyword(pair[0], pair[1])];
|
1184 | });
|
1185 | }
|
1186 | }
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 | function scoreForKeyword(keyword, providedScore) {
|
1197 |
|
1198 |
|
1199 | if (providedScore) {
|
1200 | return Number(providedScore);
|
1201 | }
|
1202 |
|
1203 | return commonKeyword(keyword) ? 0 : 1;
|
1204 | }
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 | function commonKeyword(keyword) {
|
1211 | return COMMON_KEYWORDS.includes(keyword.toLowerCase());
|
1212 | }
|
1213 |
|
1214 | var version = "10.1.2";
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 | const escape$1 = escapeHTML;
|
1222 | const inherit$1 = inherit;
|
1223 |
|
1224 | const { nodeStream: nodeStream$1, mergeStreams: mergeStreams$1 } = utils;
|
1225 | const NO_MATCH = Symbol("nomatch");
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 | const HLJS = function(hljs) {
|
1232 |
|
1233 |
|
1234 | var ArrayProto = [];
|
1235 |
|
1236 |
|
1237 |
|
1238 | var languages = Object.create(null);
|
1239 |
|
1240 | var aliases = Object.create(null);
|
1241 |
|
1242 | var plugins = [];
|
1243 |
|
1244 |
|
1245 |
|
1246 | var SAFE_MODE = true;
|
1247 | var fixMarkupRe = /(^(<[^>]+>|\t|)+|\n)/gm;
|
1248 | var LANGUAGE_NOT_FOUND = "Could not find the language '{}', did you forget to load/include a language module?";
|
1249 |
|
1250 | const PLAINTEXT_LANGUAGE = { disableAutodetect: true, name: 'Plain text', contains: [] };
|
1251 |
|
1252 |
|
1253 |
|
1254 |
|
1255 | var options = {
|
1256 | noHighlightRe: /^(no-?highlight)$/i,
|
1257 | languageDetectRe: /\blang(?:uage)?-([\w-]+)\b/i,
|
1258 | classPrefix: 'hljs-',
|
1259 | tabReplace: null,
|
1260 | useBR: false,
|
1261 | languages: null,
|
1262 |
|
1263 |
|
1264 | __emitter: TokenTreeEmitter
|
1265 | };
|
1266 |
|
1267 |
|
1268 |
|
1269 | |
1270 |
|
1271 |
|
1272 |
|
1273 | function shouldNotHighlight(languageName) {
|
1274 | return options.noHighlightRe.test(languageName);
|
1275 | }
|
1276 |
|
1277 | |
1278 |
|
1279 |
|
1280 | function blockLanguage(block) {
|
1281 | var classes = block.className + ' ';
|
1282 |
|
1283 | classes += block.parentNode ? block.parentNode.className : '';
|
1284 |
|
1285 |
|
1286 | const match = options.languageDetectRe.exec(classes);
|
1287 | if (match) {
|
1288 | var language = getLanguage(match[1]);
|
1289 | if (!language) {
|
1290 | console.warn(LANGUAGE_NOT_FOUND.replace("{}", match[1]));
|
1291 | console.warn("Falling back to no-highlight mode for this block.", block);
|
1292 | }
|
1293 | return language ? match[1] : 'no-highlight';
|
1294 | }
|
1295 |
|
1296 | return classes
|
1297 | .split(/\s+/)
|
1298 | .find((_class) => shouldNotHighlight(_class) || getLanguage(_class));
|
1299 | }
|
1300 |
|
1301 | |
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 | function highlight(languageName, code, ignoreIllegals, continuation) {
|
1318 |
|
1319 | var context = {
|
1320 | code,
|
1321 | language: languageName
|
1322 | };
|
1323 |
|
1324 |
|
1325 | fire("before:highlight", context);
|
1326 |
|
1327 |
|
1328 |
|
1329 | var result = context.result ?
|
1330 | context.result :
|
1331 | _highlight(context.language, context.code, ignoreIllegals, continuation);
|
1332 |
|
1333 | result.code = context.code;
|
1334 |
|
1335 | fire("after:highlight", result);
|
1336 |
|
1337 | return result;
|
1338 | }
|
1339 |
|
1340 | |
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 | function _highlight(languageName, code, ignoreIllegals, continuation) {
|
1349 | var codeToHighlight = code;
|
1350 |
|
1351 | |
1352 |
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 | function keywordData(mode, match) {
|
1358 | var matchText = language.case_insensitive ? match[0].toLowerCase() : match[0];
|
1359 | return Object.prototype.hasOwnProperty.call(mode.keywords, matchText) && mode.keywords[matchText];
|
1360 | }
|
1361 |
|
1362 | function processKeywords() {
|
1363 | if (!top.keywords) {
|
1364 | emitter.addText(mode_buffer);
|
1365 | return;
|
1366 | }
|
1367 |
|
1368 | let last_index = 0;
|
1369 | top.keywordPatternRe.lastIndex = 0;
|
1370 | let match = top.keywordPatternRe.exec(mode_buffer);
|
1371 | let buf = "";
|
1372 |
|
1373 | while (match) {
|
1374 | buf += mode_buffer.substring(last_index, match.index);
|
1375 | const data = keywordData(top, match);
|
1376 | if (data) {
|
1377 | const [kind, keywordRelevance] = data;
|
1378 | emitter.addText(buf);
|
1379 | buf = "";
|
1380 |
|
1381 | relevance += keywordRelevance;
|
1382 | emitter.addKeyword(match[0], kind);
|
1383 | } else {
|
1384 | buf += match[0];
|
1385 | }
|
1386 | last_index = top.keywordPatternRe.lastIndex;
|
1387 | match = top.keywordPatternRe.exec(mode_buffer);
|
1388 | }
|
1389 | buf += mode_buffer.substr(last_index);
|
1390 | emitter.addText(buf);
|
1391 | }
|
1392 |
|
1393 | function processSubLanguage() {
|
1394 | if (mode_buffer === "") return;
|
1395 |
|
1396 | var result = null;
|
1397 |
|
1398 | if (typeof top.subLanguage === 'string') {
|
1399 | if (!languages[top.subLanguage]) {
|
1400 | emitter.addText(mode_buffer);
|
1401 | return;
|
1402 | }
|
1403 | result = _highlight(top.subLanguage, mode_buffer, true, continuations[top.subLanguage]);
|
1404 | continuations[top.subLanguage] = result.top;
|
1405 | } else {
|
1406 | result = highlightAuto(mode_buffer, top.subLanguage.length ? top.subLanguage : null);
|
1407 | }
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 | if (top.relevance > 0) {
|
1414 | relevance += result.relevance;
|
1415 | }
|
1416 | emitter.addSublanguage(result.emitter, result.language);
|
1417 | }
|
1418 |
|
1419 | function processBuffer() {
|
1420 | if (top.subLanguage != null) {
|
1421 | processSubLanguage();
|
1422 | } else {
|
1423 | processKeywords();
|
1424 | }
|
1425 | mode_buffer = '';
|
1426 | }
|
1427 |
|
1428 | |
1429 |
|
1430 |
|
1431 | function startNewMode(mode) {
|
1432 | if (mode.className) {
|
1433 | emitter.openNode(mode.className);
|
1434 | }
|
1435 | top = Object.create(mode, { parent: { value: top } });
|
1436 | return top;
|
1437 | }
|
1438 |
|
1439 | |
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 |
|
1445 | function endOfMode(mode, match, matchPlusRemainder) {
|
1446 | let matched = startsWith(mode.endRe, matchPlusRemainder);
|
1447 |
|
1448 | if (matched) {
|
1449 | if (mode["on:end"]) {
|
1450 | const resp = new Response(mode);
|
1451 | mode["on:end"](match, resp);
|
1452 | if (resp.ignore) matched = false;
|
1453 | }
|
1454 |
|
1455 | if (matched) {
|
1456 | while (mode.endsParent && mode.parent) {
|
1457 | mode = mode.parent;
|
1458 | }
|
1459 | return mode;
|
1460 | }
|
1461 | }
|
1462 |
|
1463 |
|
1464 | if (mode.endsWithParent) {
|
1465 | return endOfMode(mode.parent, match, matchPlusRemainder);
|
1466 | }
|
1467 | }
|
1468 |
|
1469 | |
1470 |
|
1471 |
|
1472 |
|
1473 |
|
1474 | function doIgnore(lexeme) {
|
1475 | if (top.matcher.regexIndex === 0) {
|
1476 |
|
1477 |
|
1478 | mode_buffer += lexeme[0];
|
1479 | return 1;
|
1480 | } else {
|
1481 |
|
1482 |
|
1483 | continueScanAtSamePosition = true;
|
1484 | return 0;
|
1485 | }
|
1486 | }
|
1487 |
|
1488 | |
1489 |
|
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 | function doBeginMatch(match) {
|
1495 | var lexeme = match[0];
|
1496 | var new_mode = match.rule;
|
1497 |
|
1498 | const resp = new Response(new_mode);
|
1499 |
|
1500 | const beforeCallbacks = [new_mode.__beforeBegin, new_mode["on:begin"]];
|
1501 | for (const cb of beforeCallbacks) {
|
1502 | if (!cb) continue;
|
1503 | cb(match, resp);
|
1504 | if (resp.ignore) return doIgnore(lexeme);
|
1505 | }
|
1506 |
|
1507 | if (new_mode && new_mode.endSameAsBegin) {
|
1508 | new_mode.endRe = escape(lexeme);
|
1509 | }
|
1510 |
|
1511 | if (new_mode.skip) {
|
1512 | mode_buffer += lexeme;
|
1513 | } else {
|
1514 | if (new_mode.excludeBegin) {
|
1515 | mode_buffer += lexeme;
|
1516 | }
|
1517 | processBuffer();
|
1518 | if (!new_mode.returnBegin && !new_mode.excludeBegin) {
|
1519 | mode_buffer = lexeme;
|
1520 | }
|
1521 | }
|
1522 | startNewMode(new_mode);
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 | return new_mode.returnBegin ? 0 : lexeme.length;
|
1528 | }
|
1529 |
|
1530 | |
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 | function doEndMatch(match) {
|
1536 | var lexeme = match[0];
|
1537 | var matchPlusRemainder = codeToHighlight.substr(match.index);
|
1538 |
|
1539 | var end_mode = endOfMode(top, match, matchPlusRemainder);
|
1540 | if (!end_mode) { return NO_MATCH; }
|
1541 |
|
1542 | var origin = top;
|
1543 | if (origin.skip) {
|
1544 | mode_buffer += lexeme;
|
1545 | } else {
|
1546 | if (!(origin.returnEnd || origin.excludeEnd)) {
|
1547 | mode_buffer += lexeme;
|
1548 | }
|
1549 | processBuffer();
|
1550 | if (origin.excludeEnd) {
|
1551 | mode_buffer = lexeme;
|
1552 | }
|
1553 | }
|
1554 | do {
|
1555 | if (top.className) {
|
1556 | emitter.closeNode();
|
1557 | }
|
1558 | if (!top.skip && !top.subLanguage) {
|
1559 | relevance += top.relevance;
|
1560 | }
|
1561 | top = top.parent;
|
1562 | } while (top !== end_mode.parent);
|
1563 | if (end_mode.starts) {
|
1564 | if (end_mode.endSameAsBegin) {
|
1565 | end_mode.starts.endRe = end_mode.endRe;
|
1566 | }
|
1567 | startNewMode(end_mode.starts);
|
1568 | }
|
1569 | return origin.returnEnd ? 0 : lexeme.length;
|
1570 | }
|
1571 |
|
1572 | function processContinuations() {
|
1573 | var list = [];
|
1574 | for (var current = top; current !== language; current = current.parent) {
|
1575 | if (current.className) {
|
1576 | list.unshift(current.className);
|
1577 | }
|
1578 | }
|
1579 | list.forEach(item => emitter.openNode(item));
|
1580 | }
|
1581 |
|
1582 |
|
1583 | var lastMatch = {};
|
1584 |
|
1585 | |
1586 |
|
1587 |
|
1588 |
|
1589 |
|
1590 |
|
1591 | function processLexeme(textBeforeMatch, match) {
|
1592 | var lexeme = match && match[0];
|
1593 |
|
1594 |
|
1595 | mode_buffer += textBeforeMatch;
|
1596 |
|
1597 | if (lexeme == null) {
|
1598 | processBuffer();
|
1599 | return 0;
|
1600 | }
|
1601 |
|
1602 |
|
1603 |
|
1604 |
|
1605 |
|
1606 | if (lastMatch.type === "begin" && match.type === "end" && lastMatch.index === match.index && lexeme === "") {
|
1607 |
|
1608 | mode_buffer += codeToHighlight.slice(match.index, match.index + 1);
|
1609 | if (!SAFE_MODE) {
|
1610 |
|
1611 | const err = new Error('0 width match regex');
|
1612 | err.languageName = languageName;
|
1613 | err.badRule = lastMatch.rule;
|
1614 | throw err;
|
1615 | }
|
1616 | return 1;
|
1617 | }
|
1618 | lastMatch = match;
|
1619 |
|
1620 | if (match.type === "begin") {
|
1621 | return doBeginMatch(match);
|
1622 | } else if (match.type === "illegal" && !ignoreIllegals) {
|
1623 |
|
1624 |
|
1625 | const err = new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.className || '<unnamed>') + '"');
|
1626 | err.mode = top;
|
1627 | throw err;
|
1628 | } else if (match.type === "end") {
|
1629 | var processed = doEndMatch(match);
|
1630 | if (processed !== NO_MATCH) {
|
1631 | return processed;
|
1632 | }
|
1633 | }
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 | if (match.type === "illegal" && lexeme === "") {
|
1639 |
|
1640 | return 1;
|
1641 | }
|
1642 |
|
1643 |
|
1644 |
|
1645 |
|
1646 |
|
1647 | if (iterations > 100000 && iterations > match.index * 3) {
|
1648 | const err = new Error('potential infinite loop, way more iterations than matches');
|
1649 | throw err;
|
1650 | }
|
1651 |
|
1652 | |
1653 |
|
1654 |
|
1655 |
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 | mode_buffer += lexeme;
|
1665 | return lexeme.length;
|
1666 | }
|
1667 |
|
1668 | var language = getLanguage(languageName);
|
1669 | if (!language) {
|
1670 | console.error(LANGUAGE_NOT_FOUND.replace("{}", languageName));
|
1671 | throw new Error('Unknown language: "' + languageName + '"');
|
1672 | }
|
1673 |
|
1674 | var md = compileLanguage(language);
|
1675 | var result = '';
|
1676 |
|
1677 | var top = continuation || md;
|
1678 |
|
1679 | var continuations = {};
|
1680 | var emitter = new options.__emitter(options);
|
1681 | processContinuations();
|
1682 | var mode_buffer = '';
|
1683 | var relevance = 0;
|
1684 | var index = 0;
|
1685 | var iterations = 0;
|
1686 | var continueScanAtSamePosition = false;
|
1687 |
|
1688 | try {
|
1689 | top.matcher.considerAll();
|
1690 |
|
1691 | for (;;) {
|
1692 | iterations++;
|
1693 | if (continueScanAtSamePosition) {
|
1694 |
|
1695 |
|
1696 | continueScanAtSamePosition = false;
|
1697 | } else {
|
1698 | top.matcher.lastIndex = index;
|
1699 | top.matcher.considerAll();
|
1700 | }
|
1701 | const match = top.matcher.exec(codeToHighlight);
|
1702 |
|
1703 | if (!match) break;
|
1704 |
|
1705 | const beforeMatch = codeToHighlight.substring(index, match.index);
|
1706 | const processedCount = processLexeme(beforeMatch, match);
|
1707 | index = match.index + processedCount;
|
1708 | }
|
1709 | processLexeme(codeToHighlight.substr(index));
|
1710 | emitter.closeAllNodes();
|
1711 | emitter.finalize();
|
1712 | result = emitter.toHTML();
|
1713 |
|
1714 | return {
|
1715 | relevance: relevance,
|
1716 | value: result,
|
1717 | language: languageName,
|
1718 | illegal: false,
|
1719 | emitter: emitter,
|
1720 | top: top
|
1721 | };
|
1722 | } catch (err) {
|
1723 | if (err.message && err.message.includes('Illegal')) {
|
1724 | return {
|
1725 | illegal: true,
|
1726 | illegalBy: {
|
1727 | msg: err.message,
|
1728 | context: codeToHighlight.slice(index - 100, index + 100),
|
1729 | mode: err.mode
|
1730 | },
|
1731 | sofar: result,
|
1732 | relevance: 0,
|
1733 | value: escape$1(codeToHighlight),
|
1734 | emitter: emitter
|
1735 | };
|
1736 | } else if (SAFE_MODE) {
|
1737 | return {
|
1738 | illegal: false,
|
1739 | relevance: 0,
|
1740 | value: escape$1(codeToHighlight),
|
1741 | emitter: emitter,
|
1742 | language: languageName,
|
1743 | top: top,
|
1744 | errorRaised: err
|
1745 | };
|
1746 | } else {
|
1747 | throw err;
|
1748 | }
|
1749 | }
|
1750 | }
|
1751 |
|
1752 | |
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 | function justTextHighlightResult(code) {
|
1760 | const result = {
|
1761 | relevance: 0,
|
1762 | emitter: new options.__emitter(options),
|
1763 | value: escape$1(code),
|
1764 | illegal: false,
|
1765 | top: PLAINTEXT_LANGUAGE
|
1766 | };
|
1767 | result.emitter.addText(code);
|
1768 | return result;
|
1769 | }
|
1770 |
|
1771 | |
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 |
|
1784 |
|
1785 | function highlightAuto(code, languageSubset) {
|
1786 | languageSubset = languageSubset || options.languages || Object.keys(languages);
|
1787 | var result = justTextHighlightResult(code);
|
1788 | var secondBest = result;
|
1789 | languageSubset.filter(getLanguage).filter(autoDetection).forEach(function(name) {
|
1790 | var current = _highlight(name, code, false);
|
1791 | current.language = name;
|
1792 | if (current.relevance > secondBest.relevance) {
|
1793 | secondBest = current;
|
1794 | }
|
1795 | if (current.relevance > result.relevance) {
|
1796 | secondBest = result;
|
1797 | result = current;
|
1798 | }
|
1799 | });
|
1800 | if (secondBest.language) {
|
1801 |
|
1802 | result.second_best = secondBest;
|
1803 | }
|
1804 | return result;
|
1805 | }
|
1806 |
|
1807 | |
1808 |
|
1809 |
|
1810 |
|
1811 |
|
1812 |
|
1813 |
|
1814 |
|
1815 |
|
1816 | function fixMarkup(html) {
|
1817 | if (!(options.tabReplace || options.useBR)) {
|
1818 | return html;
|
1819 | }
|
1820 |
|
1821 | return html.replace(fixMarkupRe, match => {
|
1822 | if (match === '\n') {
|
1823 | return options.useBR ? '<br>' : match;
|
1824 | } else if (options.tabReplace) {
|
1825 | return match.replace(/\t/g, options.tabReplace);
|
1826 | }
|
1827 | return match;
|
1828 | });
|
1829 | }
|
1830 |
|
1831 | |
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 |
|
1837 |
|
1838 | function buildClassName(prevClassName, currentLang, resultLang) {
|
1839 | var language = currentLang ? aliases[currentLang] : resultLang;
|
1840 | var result = [prevClassName.trim()];
|
1841 |
|
1842 | if (!prevClassName.match(/\bhljs\b/)) {
|
1843 | result.push('hljs');
|
1844 | }
|
1845 |
|
1846 | if (!prevClassName.includes(language)) {
|
1847 | result.push(language);
|
1848 | }
|
1849 |
|
1850 | return result.join(' ').trim();
|
1851 | }
|
1852 |
|
1853 | |
1854 |
|
1855 |
|
1856 |
|
1857 |
|
1858 |
|
1859 | function highlightBlock(element) {
|
1860 |
|
1861 | let node = null;
|
1862 | const language = blockLanguage(element);
|
1863 |
|
1864 | if (shouldNotHighlight(language)) return;
|
1865 |
|
1866 | fire("before:highlightBlock",
|
1867 | { block: element, language: language });
|
1868 |
|
1869 | if (options.useBR) {
|
1870 | node = document.createElement('div');
|
1871 | node.innerHTML = element.innerHTML.replace(/\n/g, '').replace(/<br[ /]*>/g, '\n');
|
1872 | } else {
|
1873 | node = element;
|
1874 | }
|
1875 | const text = node.textContent;
|
1876 | const result = language ? highlight(language, text, true) : highlightAuto(text);
|
1877 |
|
1878 | const originalStream = nodeStream$1(node);
|
1879 | if (originalStream.length) {
|
1880 | const resultNode = document.createElement('div');
|
1881 | resultNode.innerHTML = result.value;
|
1882 | result.value = mergeStreams$1(originalStream, nodeStream$1(resultNode), text);
|
1883 | }
|
1884 | result.value = fixMarkup(result.value);
|
1885 |
|
1886 | fire("after:highlightBlock", { block: element, result: result });
|
1887 |
|
1888 | element.innerHTML = result.value;
|
1889 | element.className = buildClassName(element.className, language, result.language);
|
1890 | element.result = {
|
1891 | language: result.language,
|
1892 |
|
1893 | re: result.relevance,
|
1894 | relavance: result.relevance
|
1895 | };
|
1896 | if (result.second_best) {
|
1897 | element.second_best = {
|
1898 | language: result.second_best.language,
|
1899 |
|
1900 | re: result.second_best.relevance,
|
1901 | relavance: result.second_best.relevance
|
1902 | };
|
1903 | }
|
1904 | }
|
1905 |
|
1906 | |
1907 |
|
1908 |
|
1909 |
|
1910 |
|
1911 | function configure(userOptions) {
|
1912 | options = inherit$1(options, userOptions);
|
1913 | }
|
1914 |
|
1915 | |
1916 |
|
1917 |
|
1918 |
|
1919 |
|
1920 | const initHighlighting = () => {
|
1921 | if (initHighlighting.called) return;
|
1922 | initHighlighting.called = true;
|
1923 |
|
1924 | var blocks = document.querySelectorAll('pre code');
|
1925 | ArrayProto.forEach.call(blocks, highlightBlock);
|
1926 | };
|
1927 |
|
1928 |
|
1929 | function initHighlightingOnLoad() {
|
1930 |
|
1931 | window.addEventListener('DOMContentLoaded', initHighlighting, false);
|
1932 | }
|
1933 |
|
1934 | |
1935 |
|
1936 |
|
1937 |
|
1938 |
|
1939 |
|
1940 | function registerLanguage(languageName, languageDefinition) {
|
1941 | var lang = null;
|
1942 | try {
|
1943 | lang = languageDefinition(hljs);
|
1944 | } catch (error) {
|
1945 | console.error("Language definition for '{}' could not be registered.".replace("{}", languageName));
|
1946 |
|
1947 | if (!SAFE_MODE) { throw error; } else { console.error(error); }
|
1948 |
|
1949 |
|
1950 |
|
1951 |
|
1952 | lang = PLAINTEXT_LANGUAGE;
|
1953 | }
|
1954 |
|
1955 | if (!lang.name) lang.name = languageName;
|
1956 | languages[languageName] = lang;
|
1957 | lang.rawDefinition = languageDefinition.bind(null, hljs);
|
1958 |
|
1959 | if (lang.aliases) {
|
1960 | registerAliases(lang.aliases, { languageName });
|
1961 | }
|
1962 | }
|
1963 |
|
1964 | |
1965 |
|
1966 |
|
1967 | function listLanguages() {
|
1968 | return Object.keys(languages);
|
1969 | }
|
1970 |
|
1971 | |
1972 |
|
1973 |
|
1974 |
|
1975 |
|
1976 |
|
1977 |
|
1978 |
|
1979 |
|
1980 | function requireLanguage(name) {
|
1981 | var lang = getLanguage(name);
|
1982 | if (lang) { return lang; }
|
1983 |
|
1984 | var err = new Error('The \'{}\' language is required, but not loaded.'.replace('{}', name));
|
1985 | throw err;
|
1986 | }
|
1987 |
|
1988 | |
1989 |
|
1990 |
|
1991 |
|
1992 | function getLanguage(name) {
|
1993 | name = (name || '').toLowerCase();
|
1994 | return languages[name] || languages[aliases[name]];
|
1995 | }
|
1996 |
|
1997 | |
1998 |
|
1999 |
|
2000 |
|
2001 |
|
2002 | function registerAliases(aliasList, { languageName }) {
|
2003 | if (typeof aliasList === 'string') {
|
2004 | aliasList = [aliasList];
|
2005 | }
|
2006 | aliasList.forEach(alias => { aliases[alias] = languageName; });
|
2007 | }
|
2008 |
|
2009 | |
2010 |
|
2011 |
|
2012 |
|
2013 | function autoDetection(name) {
|
2014 | var lang = getLanguage(name);
|
2015 | return lang && !lang.disableAutodetect;
|
2016 | }
|
2017 |
|
2018 | |
2019 |
|
2020 |
|
2021 | function addPlugin(plugin) {
|
2022 | plugins.push(plugin);
|
2023 | }
|
2024 |
|
2025 | |
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 | function fire(event, args) {
|
2031 | var cb = event;
|
2032 | plugins.forEach(function(plugin) {
|
2033 | if (plugin[cb]) {
|
2034 | plugin[cb](args);
|
2035 | }
|
2036 | });
|
2037 | }
|
2038 |
|
2039 |
|
2040 |
|
2041 | Object.assign(hljs, {
|
2042 | highlight,
|
2043 | highlightAuto,
|
2044 | fixMarkup,
|
2045 | highlightBlock,
|
2046 | configure,
|
2047 | initHighlighting,
|
2048 | initHighlightingOnLoad,
|
2049 | registerLanguage,
|
2050 | listLanguages,
|
2051 | getLanguage,
|
2052 | registerAliases,
|
2053 | requireLanguage,
|
2054 | autoDetection,
|
2055 | inherit: inherit$1,
|
2056 | addPlugin
|
2057 | });
|
2058 |
|
2059 | hljs.debugMode = function() { SAFE_MODE = false; };
|
2060 | hljs.safeMode = function() { SAFE_MODE = true; };
|
2061 | hljs.versionString = version;
|
2062 |
|
2063 | for (const key in MODES) {
|
2064 |
|
2065 | if (typeof MODES[key] === "object") {
|
2066 |
|
2067 | deepFreeze(MODES[key]);
|
2068 | }
|
2069 | }
|
2070 |
|
2071 |
|
2072 | Object.assign(hljs, MODES);
|
2073 |
|
2074 | return hljs;
|
2075 | };
|
2076 |
|
2077 |
|
2078 | var highlight = HLJS({});
|
2079 |
|
2080 | module.exports = highlight;
|