Skip to content

Commit

Permalink
add HTML renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Feb 16, 2020
1 parent 4726d40 commit 8c91b6e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/highlight.js
Expand Up @@ -452,9 +452,8 @@ const HLJS = function(hljs) {
list.unshift(current.className)
}
}
list.forEach((item) => { emitter.openNode(item) }
)
}
list.forEach(item => emitter.openNode(item))
}

var lastMatch = {};
function processLexeme(text_before_match, match) {
Expand Down Expand Up @@ -522,28 +521,30 @@ const HLJS = function(hljs) {
}

compileLanguage(language);
var current;
var top = continuation || language;
var continuations = {}; // keep continuations for sub-languages
var result = '';
var result;
var emitter = new TokenTree();
processContinuations();
var mode_buffer = '';
var relevance = 0;
var match, processedCount, index = 0;

try {
var match, count, index = 0;
while (true) {
top.terminators.lastIndex = index;
match = top.terminators.exec(codeToHighlight);
if (!match)
break;
count = processLexeme(codeToHighlight.substring(index, match.index), match);
index = match.index + count;
let beforeMatch = codeToHighlight.substring(index, match.index);
processedCount = processLexeme(beforeMatch, match);
index = match.index + processedCount;
}
processLexeme(codeToHighlight.substr(index));
emitter.closeAllNodes();
emitter.collapse();
emitter.finalize();
result = new HTMLRenderer(emitter, options).value();

return {
relevance: relevance,
value: result,
Expand Down
42 changes: 42 additions & 0 deletions src/lib/html_renderer.js
@@ -0,0 +1,42 @@
const SPAN_CLOSE = '</span>';

import {escapeHTML} from './utils';

export default class HTMLRenderer {
constructor(tree, options) {
this.buffer = "";
this.classPrefix = options.classPrefix;
tree.walk(this);
}

// renderer API

addText(text) {
this.buffer += escapeHTML(text)
}

openNode(node) {
let className = node.kind;
if (!node.kind) return;

if (!node.sublanguage)
className = `${this.classPrefix}${className}`;
this.span(className);
}

closeNode(node) {
if (!node.kind) return;

this.buffer += SPAN_CLOSE;
}

// helpers

span(className) {
this.buffer += `<span class="${className}">`
}

value() {
return this.buffer;
}
}
12 changes: 10 additions & 2 deletions src/lib/token_tree.js
Expand Up @@ -3,12 +3,15 @@ export default class TokenTree {
this.rootNode = { root: true, children: [] };
this.stack = [ this.rootNode ];
}

get top() {
return this.stack[this.stack.length - 1];
}

add(node) {
this.top.children.push(node);
}

addKeyword(text, kind) {
if (text === "") { return; }

Expand All @@ -17,37 +20,42 @@ export default class TokenTree {
children: [ text ]
});
}

addText(text) {
if (text === "") { return; }

this.add(text);
}

addSublanguage({rootNode}, name) {
let node = rootNode;
delete node.root; // no longer a root node
node.kind = name;
node.sublanguage = true;
this.add(node);
}

openNode(kind) {
var node = { kind, children: [] };
this.add(node);
this.stack.push(node);
}

closeNode() {
if (this.stack.length > 1)
this.stack.pop();
}

closeAllNodes() {
while (this.stack.length > 1)
this.closeNode();
}


toJSON() {
return JSON.stringify(this.rootNode, null, 4);
}
collapse() {

finalize() {
return;
TokenTree._collapse(this.rootNode);
}
Expand Down

0 comments on commit 8c91b6e

Please sign in to comment.