mirror of
https://github.com/TiddlyWiki/TiddlyWiki5.git
synced 2026-05-02 10:27:41 +00:00
Refactoring the wikitext parser
To match the structure of the JavaScript parser, and make it less complicated
This commit is contained in:
@@ -9,18 +9,44 @@ Parses a block of tiddlywiki-format wiki text into a parse tree object.
|
||||
/*jslint node: true */
|
||||
"use strict";
|
||||
|
||||
var WikiTextCompiler = require("./WikiTextCompiler.js").WikiTextCompiler,
|
||||
var WikiTextRules = require("./WikiTextRules.js"),
|
||||
WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
|
||||
utils = require("./Utils.js"),
|
||||
util = require("util");
|
||||
|
||||
var WikiTextParser = function(text,processor) {
|
||||
this.processor = processor;
|
||||
/*
|
||||
Creates a new instance of the wiki text parser with the specified options. The
|
||||
options are a hashmap of mandatory members as follows:
|
||||
|
||||
store: The store object to use to parse any cascaded content (eg transclusion)
|
||||
|
||||
Planned:
|
||||
|
||||
enableRules: An array of names of wiki text rules to enable. If not specified, all rules are available
|
||||
extraRules: An array of additional rule handlers to add
|
||||
enableMacros: An array of names of macros to enable. If not specified, all macros are available
|
||||
extraMacros: An array of additional macro handlers to add
|
||||
*/
|
||||
|
||||
var WikiTextParser = function(options) {
|
||||
this.store = options.store;
|
||||
this.autoLinkWikiWords = true;
|
||||
this.rules = WikiTextRules.rules;
|
||||
var pattern = [];
|
||||
for(var n=0; n<this.rules.length; n++) {
|
||||
pattern.push("(" + this.rules[n].match + ")");
|
||||
}
|
||||
this.rulesRegExp = new RegExp(pattern.join("|"),"mg");
|
||||
|
||||
};
|
||||
|
||||
WikiTextParser.prototype.parse = function(text) {
|
||||
this.source = text;
|
||||
this.nextMatch = 0;
|
||||
this.children = [];
|
||||
this.output = null;
|
||||
this.subWikify(this.children);
|
||||
return new WikiTextParseTree(this.children,this.store);
|
||||
};
|
||||
|
||||
WikiTextParser.prototype.outputText = function(place,startPos,endPos) {
|
||||
@@ -42,8 +68,8 @@ WikiTextParser.prototype.subWikifyUnterm = function(output) {
|
||||
var oldOutput = this.output;
|
||||
this.output = output;
|
||||
// Get the first match
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.processor.rulesRegExp.exec(this.source);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.rulesRegExp.exec(this.source);
|
||||
while(ruleMatch) {
|
||||
// Output any text before the match
|
||||
if(ruleMatch.index > this.nextMatch)
|
||||
@@ -52,18 +78,18 @@ WikiTextParser.prototype.subWikifyUnterm = function(output) {
|
||||
this.matchStart = ruleMatch.index;
|
||||
this.matchLength = ruleMatch[0].length;
|
||||
this.matchText = ruleMatch[0];
|
||||
this.nextMatch = this.processor.rulesRegExp.lastIndex;
|
||||
this.nextMatch = this.rulesRegExp.lastIndex;
|
||||
// Figure out which rule matched and call its handler
|
||||
var t;
|
||||
for(t=1; t<ruleMatch.length; t++) {
|
||||
if(ruleMatch[t]) {
|
||||
this.processor.rules[t-1].handler(this);
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
this.rules[t-1].handler(this);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Get the next match
|
||||
ruleMatch = this.processor.rulesRegExp.exec(this.source);
|
||||
ruleMatch = this.rulesRegExp.exec(this.source);
|
||||
}
|
||||
// Output any text after the last match
|
||||
if(this.nextMatch < this.source.length) {
|
||||
@@ -81,8 +107,8 @@ WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
||||
// Get the first matches for the rule and terminator RegExps
|
||||
terminatorRegExp.lastIndex = this.nextMatch;
|
||||
var terminatorMatch = terminatorRegExp.exec(this.source);
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.processor.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
while(terminatorMatch || ruleMatch) {
|
||||
// Check for a terminator match before the next rule match
|
||||
if(terminatorMatch && (!ruleMatch || terminatorMatch.index <= ruleMatch.index)) {
|
||||
@@ -105,20 +131,20 @@ WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
||||
this.matchStart = ruleMatch.index;
|
||||
this.matchLength = ruleMatch[0].length;
|
||||
this.matchText = ruleMatch[0];
|
||||
this.nextMatch = this.processor.rulesRegExp.lastIndex;
|
||||
this.nextMatch = this.rulesRegExp.lastIndex;
|
||||
// Figure out which rule matched and call its handler
|
||||
var t;
|
||||
for(t=1; t<ruleMatch.length; t++) {
|
||||
if(ruleMatch[t]) {
|
||||
this.processor.rules[t-1].handler(this);
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
this.rules[t-1].handler(this);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Get the next match
|
||||
terminatorRegExp.lastIndex = this.nextMatch;
|
||||
terminatorMatch = terminatorRegExp.exec(this.source);
|
||||
ruleMatch = this.processor.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
ruleMatch = this.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
}
|
||||
// Output any text after the last match
|
||||
if(this.nextMatch < this.source.length) {
|
||||
@@ -129,15 +155,6 @@ WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
||||
this.output = oldOutput;
|
||||
};
|
||||
|
||||
WikiTextParser.prototype.render = function(type,treenode,store,title) {
|
||||
/*jslint evil: true */
|
||||
var compiler = new WikiTextCompiler(store,title,this);
|
||||
var code = compiler.compile(type,treenode);
|
||||
var fn = eval(code);
|
||||
var tiddler = store.getTiddler(title);
|
||||
return fn(tiddler,store,utils);
|
||||
};
|
||||
|
||||
exports.WikiTextParser = WikiTextParser;
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user