mirror of
https://github.com/Jermolene/TiddlyWiki5.git
synced 2026-01-01 14:52:08 -08:00
Added syntax highlighting for JavaScript tiddlers
This commit is contained in:
parent
542561a0fa
commit
8adfcbdc69
2 changed files with 80 additions and 4 deletions
|
|
@ -44,11 +44,47 @@ var JavaScriptParser = function(options) {
|
|||
this.store = options.store;
|
||||
};
|
||||
|
||||
// Parse a string of JavaScript code and return the parse tree as a wikitext parse tree
|
||||
// Parse a string of JavaScript code or JSON and return the parse tree as a wikitext parse tree
|
||||
JavaScriptParser.prototype.parse = function(type,code) {
|
||||
if(type === "application/json") {
|
||||
code = "(" + code + ")";
|
||||
if(type === "application/javascript") {
|
||||
return this.parseJavaScript(code);
|
||||
} else {
|
||||
return this.parseJSON(code);
|
||||
}
|
||||
}
|
||||
|
||||
JavaScriptParser.prototype.parseJavaScript = function(code) {
|
||||
// Get the parse tree
|
||||
var parseTree = esprima.parse(code,{
|
||||
tokens: true,
|
||||
range: true
|
||||
}),
|
||||
result = [],
|
||||
t,endLastToken = 0;
|
||||
for(t=0; t<parseTree.tokens.length; t++) {
|
||||
var token = parseTree.tokens[t];
|
||||
if(token.range[0] > endLastToken) {
|
||||
result.push(Renderer.TextNode(code.substring(endLastToken,token.range[0])));
|
||||
}
|
||||
result.push(Renderer.ElementNode("span",{
|
||||
"class": "javascript-" + token.type.toLowerCase()
|
||||
},[
|
||||
Renderer.TextNode(token.value)
|
||||
]));
|
||||
endLastToken = token.range[1] + 1;
|
||||
}
|
||||
if(endLastToken < code.length) {
|
||||
result.push(Renderer.TextNode(code.substring(endLastToken)));
|
||||
}
|
||||
return new WikiTextParseTree([
|
||||
Renderer.ElementNode("pre",{"class": "javascript-source"},result)
|
||||
],new Dependencies(),this.store);
|
||||
};
|
||||
|
||||
JavaScriptParser.prototype.parseJSON = function(code) {
|
||||
// Wrap it in parenthesis to make it a program
|
||||
code = "(" + code + ")";
|
||||
// Get the parse tree
|
||||
return new WikiTextParseTree([
|
||||
renderObject(esprima.parse(code))
|
||||
],new Dependencies(),this.store);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue