Skip to content

Commit 8c91b6e

Browse files
committed
add HTML renderer
1 parent 4726d40 commit 8c91b6e

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/highlight.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,8 @@ const HLJS = function(hljs) {
452452
list.unshift(current.className)
453453
}
454454
}
455-
list.forEach((item) => { emitter.openNode(item) }
456-
)
457-
}
455+
list.forEach(item => emitter.openNode(item))
456+
}
458457

459458
var lastMatch = {};
460459
function processLexeme(text_before_match, match) {
@@ -522,28 +521,30 @@ const HLJS = function(hljs) {
522521
}
523522

524523
compileLanguage(language);
525-
var current;
526524
var top = continuation || language;
527525
var continuations = {}; // keep continuations for sub-languages
528-
var result = '';
526+
var result;
529527
var emitter = new TokenTree();
530528
processContinuations();
531529
var mode_buffer = '';
532530
var relevance = 0;
531+
var match, processedCount, index = 0;
532+
533533
try {
534-
var match, count, index = 0;
535534
while (true) {
536535
top.terminators.lastIndex = index;
537536
match = top.terminators.exec(codeToHighlight);
538537
if (!match)
539538
break;
540-
count = processLexeme(codeToHighlight.substring(index, match.index), match);
541-
index = match.index + count;
539+
let beforeMatch = codeToHighlight.substring(index, match.index);
540+
processedCount = processLexeme(beforeMatch, match);
541+
index = match.index + processedCount;
542542
}
543543
processLexeme(codeToHighlight.substr(index));
544544
emitter.closeAllNodes();
545-
emitter.collapse();
545+
emitter.finalize();
546546
result = new HTMLRenderer(emitter, options).value();
547+
547548
return {
548549
relevance: relevance,
549550
value: result,

src/lib/html_renderer.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const SPAN_CLOSE = '</span>';
2+
3+
import {escapeHTML} from './utils';
4+
5+
export default class HTMLRenderer {
6+
constructor(tree, options) {
7+
this.buffer = "";
8+
this.classPrefix = options.classPrefix;
9+
tree.walk(this);
10+
}
11+
12+
// renderer API
13+
14+
addText(text) {
15+
this.buffer += escapeHTML(text)
16+
}
17+
18+
openNode(node) {
19+
let className = node.kind;
20+
if (!node.kind) return;
21+
22+
if (!node.sublanguage)
23+
className = `${this.classPrefix}${className}`;
24+
this.span(className);
25+
}
26+
27+
closeNode(node) {
28+
if (!node.kind) return;
29+
30+
this.buffer += SPAN_CLOSE;
31+
}
32+
33+
// helpers
34+
35+
span(className) {
36+
this.buffer += `<span class="${className}">`
37+
}
38+
39+
value() {
40+
return this.buffer;
41+
}
42+
}

src/lib/token_tree.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ export default class TokenTree {
33
this.rootNode = { root: true, children: [] };
44
this.stack = [ this.rootNode ];
55
}
6+
67
get top() {
78
return this.stack[this.stack.length - 1];
89
}
10+
911
add(node) {
1012
this.top.children.push(node);
1113
}
14+
1215
addKeyword(text, kind) {
1316
if (text === "") { return; }
1417

@@ -17,37 +20,42 @@ export default class TokenTree {
1720
children: [ text ]
1821
});
1922
}
23+
2024
addText(text) {
2125
if (text === "") { return; }
2226

2327
this.add(text);
2428
}
29+
2530
addSublanguage({rootNode}, name) {
2631
let node = rootNode;
2732
delete node.root; // no longer a root node
2833
node.kind = name;
2934
node.sublanguage = true;
3035
this.add(node);
3136
}
37+
3238
openNode(kind) {
3339
var node = { kind, children: [] };
3440
this.add(node);
3541
this.stack.push(node);
3642
}
43+
3744
closeNode() {
3845
if (this.stack.length > 1)
3946
this.stack.pop();
4047
}
48+
4149
closeAllNodes() {
4250
while (this.stack.length > 1)
4351
this.closeNode();
4452
}
4553

46-
4754
toJSON() {
4855
return JSON.stringify(this.rootNode, null, 4);
4956
}
50-
collapse() {
57+
58+
finalize() {
5159
return;
5260
TokenTree._collapse(this.rootNode);
5361
}

0 commit comments

Comments
 (0)