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";
|
|
|
|
|
2011-12-11 18:28:09 +00:00
|
|
|
var WikiTextRenderer = require("./WikiTextRenderer.js").WikiTextRenderer,
|
2011-12-05 16:50:25 +00:00
|
|
|
utils = require("./Utils.js"),
|
|
|
|
util = require("util");
|
|
|
|
|
2011-12-11 18:28:09 +00:00
|
|
|
var WikiTextParser = function(text,processor) {
|
|
|
|
this.processor = processor;
|
2011-12-05 16:50:25 +00:00
|
|
|
this.autoLinkWikiWords = true;
|
|
|
|
this.source = text;
|
|
|
|
this.nextMatch = 0;
|
2011-12-11 18:28:09 +00:00
|
|
|
this.children = [];
|
2011-12-05 16:50:25 +00:00
|
|
|
this.output = null;
|
2011-12-11 18:28:09 +00:00
|
|
|
this.subWikify(this.children);
|
2011-12-05 16:50:25 +00:00
|
|
|
};
|
|
|
|
|
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
|
2011-12-11 18:28:09 +00:00
|
|
|
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
|
|
|
var ruleMatch = this.processor.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];
|
2011-12-11 18:28:09 +00:00
|
|
|
this.nextMatch = this.processor.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]) {
|
2011-12-11 18:28:09 +00:00
|
|
|
this.processor.rules[t-1].handler(this);
|
|
|
|
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
2011-12-05 16:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get the next match
|
2011-12-11 18:28:09 +00:00
|
|
|
ruleMatch = this.processor.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);
|
2011-12-11 18:28:09 +00:00
|
|
|
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
|
|
|
var ruleMatch = this.processor.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];
|
2011-12-11 18:28:09 +00:00
|
|
|
this.nextMatch = this.processor.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]) {
|
2011-12-11 18:28:09 +00:00
|
|
|
this.processor.rules[t-1].handler(this);
|
|
|
|
this.processor.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);
|
2011-12-11 18:28:09 +00:00
|
|
|
ruleMatch = this.processor.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;
|
|
|
|
};
|
|
|
|
|
2011-12-11 18:28:09 +00:00
|
|
|
WikiTextParser.prototype.render = function(type,treenode,store,title) {
|
|
|
|
var renderer = new WikiTextRenderer(store,title,this);
|
|
|
|
return renderer.render(type,treenode);
|
|
|
|
};
|
|
|
|
|
2011-12-05 16:50:25 +00:00
|
|
|
exports.WikiTextParser = WikiTextParser;
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|