2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/WikiTextParser.js
|
2011-12-05 16:50:25 +00:00
|
|
|
|
|
|
|
Parses a block of tiddlywiki-format wiki text into a parse tree object.
|
|
|
|
|
2011-12-13 12:30:09 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
2011-12-05 16:50:25 +00:00
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-12-05 16:50:25 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-01-05 11:08:05 +00:00
|
|
|
var WikiTextRules = require("./WikiTextRules.js"),
|
|
|
|
WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
|
2011-12-05 16:50:25 +00:00
|
|
|
utils = require("./Utils.js"),
|
|
|
|
util = require("util");
|
|
|
|
|
2012-01-05 11:08:05 +00:00
|
|
|
/*
|
|
|
|
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;
|
2011-12-05 16:50:25 +00:00
|
|
|
this.autoLinkWikiWords = true;
|
2012-01-05 11:08:05 +00:00
|
|
|
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) {
|
2011-12-05 16:50:25 +00:00
|
|
|
this.source = text;
|
|
|
|
this.nextMatch = 0;
|
2011-12-11 18:28:09 +00:00
|
|
|
this.children = [];
|
2012-01-06 19:41:42 +00:00
|
|
|
this.dependencies = [];
|
2011-12-05 16:50:25 +00:00
|
|
|
this.output = null;
|
2011-12-11 18:28:09 +00:00
|
|
|
this.subWikify(this.children);
|
2012-01-08 10:24:09 +00:00
|
|
|
var tree = new WikiTextParseTree(this.children,this.dependencies,this.store);
|
|
|
|
this.source = null;
|
|
|
|
this.children = null;
|
|
|
|
return tree;
|
2011-12-05 16:50:25 +00:00
|
|
|
};
|
|
|
|
|
2012-01-07 10:30:50 +00:00
|
|
|
WikiTextParser.prototype.addDependency = function(dependency) {
|
|
|
|
if(dependency === null) {
|
|
|
|
this.dependencies = null;
|
|
|
|
} else if(this.dependencies && this.dependencies.indexOf(dependency) === -1) {
|
|
|
|
this.dependencies.push(dependency);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-05 18:21:52 +00:00
|
|
|
WikiTextParser.prototype.outputText = function(place,startPos,endPos) {
|
2011-12-05 16:50:25 +00:00
|
|
|
if(startPos < endPos) {
|
|
|
|
place.push({type: "text", value: this.source.substring(startPos,endPos)});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-05 18:21:52 +00:00
|
|
|
WikiTextParser.prototype.subWikify = function(output,terminator) {
|
2011-12-05 16:50:25 +00:00
|
|
|
// Handle the terminated and unterminated cases separately, this speeds up wikifikation by about 30%
|
|
|
|
if(terminator)
|
|
|
|
this.subWikifyTerm(output,new RegExp("(" + terminator + ")","mg"));
|
|
|
|
else
|
|
|
|
this.subWikifyUnterm(output);
|
|
|
|
};
|
|
|
|
|
2011-12-05 18:21:52 +00:00
|
|
|
WikiTextParser.prototype.subWikifyUnterm = function(output) {
|
2011-12-05 16:50:25 +00:00
|
|
|
// subWikify can be indirectly recursive, so we need to save the old output pointer
|
|
|
|
var oldOutput = this.output;
|
|
|
|
this.output = output;
|
|
|
|
// Get the first match
|
2012-01-05 11:08:05 +00:00
|
|
|
this.rulesRegExp.lastIndex = this.nextMatch;
|
|
|
|
var ruleMatch = this.rulesRegExp.exec(this.source);
|
2011-12-05 16:50:25 +00:00
|
|
|
while(ruleMatch) {
|
|
|
|
// Output any text before the match
|
|
|
|
if(ruleMatch.index > this.nextMatch)
|
|
|
|
this.outputText(this.output,this.nextMatch,ruleMatch.index);
|
|
|
|
// Set the match parameters for the handler
|
|
|
|
this.matchStart = ruleMatch.index;
|
|
|
|
this.matchLength = ruleMatch[0].length;
|
|
|
|
this.matchText = ruleMatch[0];
|
2012-01-05 11:08:05 +00:00
|
|
|
this.nextMatch = this.rulesRegExp.lastIndex;
|
2011-12-05 16:50:25 +00:00
|
|
|
// Figure out which rule matched and call its handler
|
|
|
|
var t;
|
|
|
|
for(t=1; t<ruleMatch.length; t++) {
|
|
|
|
if(ruleMatch[t]) {
|
2012-01-05 11:08:05 +00:00
|
|
|
this.rules[t-1].handler(this);
|
|
|
|
this.rulesRegExp.lastIndex = this.nextMatch;
|
2011-12-05 16:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get the next match
|
2012-01-05 11:08:05 +00:00
|
|
|
ruleMatch = this.rulesRegExp.exec(this.source);
|
2011-12-05 16:50:25 +00:00
|
|
|
}
|
|
|
|
// Output any text after the last match
|
|
|
|
if(this.nextMatch < this.source.length) {
|
|
|
|
this.outputText(this.output,this.nextMatch,this.source.length);
|
|
|
|
this.nextMatch = this.source.length;
|
|
|
|
}
|
|
|
|
// Restore the output pointer
|
|
|
|
this.output = oldOutput;
|
|
|
|
};
|
|
|
|
|
2011-12-05 18:21:52 +00:00
|
|
|
WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
2011-12-05 16:50:25 +00:00
|
|
|
// subWikify can be indirectly recursive, so we need to save the old output pointer
|
|
|
|
var oldOutput = this.output;
|
|
|
|
this.output = output;
|
|
|
|
// Get the first matches for the rule and terminator RegExps
|
|
|
|
terminatorRegExp.lastIndex = this.nextMatch;
|
|
|
|
var terminatorMatch = terminatorRegExp.exec(this.source);
|
2012-01-05 11:08:05 +00:00
|
|
|
this.rulesRegExp.lastIndex = this.nextMatch;
|
|
|
|
var ruleMatch = this.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
2011-12-05 16:50:25 +00:00
|
|
|
while(terminatorMatch || ruleMatch) {
|
|
|
|
// Check for a terminator match before the next rule match
|
|
|
|
if(terminatorMatch && (!ruleMatch || terminatorMatch.index <= ruleMatch.index)) {
|
|
|
|
// Output any text before the match
|
|
|
|
if(terminatorMatch.index > this.nextMatch)
|
|
|
|
this.outputText(this.output,this.nextMatch,terminatorMatch.index);
|
|
|
|
// Set the match parameters
|
|
|
|
this.matchText = terminatorMatch[1];
|
|
|
|
this.matchLength = terminatorMatch[1].length;
|
|
|
|
this.matchStart = terminatorMatch.index;
|
|
|
|
this.nextMatch = this.matchStart + this.matchLength;
|
|
|
|
// Restore the output pointer
|
|
|
|
this.output = oldOutput;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// It must be a rule match; output any text before the match
|
|
|
|
if(ruleMatch.index > this.nextMatch)
|
|
|
|
this.outputText(this.output,this.nextMatch,ruleMatch.index);
|
|
|
|
// Set the match parameters
|
|
|
|
this.matchStart = ruleMatch.index;
|
|
|
|
this.matchLength = ruleMatch[0].length;
|
|
|
|
this.matchText = ruleMatch[0];
|
2012-01-05 11:08:05 +00:00
|
|
|
this.nextMatch = this.rulesRegExp.lastIndex;
|
2011-12-05 16:50:25 +00:00
|
|
|
// Figure out which rule matched and call its handler
|
|
|
|
var t;
|
|
|
|
for(t=1; t<ruleMatch.length; t++) {
|
|
|
|
if(ruleMatch[t]) {
|
2012-01-05 11:08:05 +00:00
|
|
|
this.rules[t-1].handler(this);
|
|
|
|
this.rulesRegExp.lastIndex = this.nextMatch;
|
2011-12-05 16:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get the next match
|
|
|
|
terminatorRegExp.lastIndex = this.nextMatch;
|
|
|
|
terminatorMatch = terminatorRegExp.exec(this.source);
|
2012-01-05 11:08:05 +00:00
|
|
|
ruleMatch = this.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
2011-12-05 16:50:25 +00:00
|
|
|
}
|
|
|
|
// Output any text after the last match
|
|
|
|
if(this.nextMatch < this.source.length) {
|
|
|
|
this.outputText(this.output,this.nextMatch,this.source.length);
|
|
|
|
this.nextMatch = this.source.length;
|
|
|
|
}
|
|
|
|
// Restore the output pointer
|
|
|
|
this.output = oldOutput;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.WikiTextParser = WikiTextParser;
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|