2012-05-26 17:30:32 +00:00
|
|
|
/*\
|
2012-11-15 12:53:10 +00:00
|
|
|
title: $:/core/modules/parsers/wikitextparser/wikitextparser.js
|
2012-05-26 17:30:32 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: parser
|
|
|
|
|
|
|
|
A new-school wikitext parser
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Define the wikitext renderer constructor
|
|
|
|
*/
|
|
|
|
var WikiTextRenderer = function(text,options) {
|
|
|
|
this.source = text || "";
|
|
|
|
this.sourceLength = this.source.length;
|
|
|
|
this.pos = 0;
|
|
|
|
this.wiki = options.wiki;
|
|
|
|
this.parser = options.parser;
|
|
|
|
this.tree = [];
|
|
|
|
this.dependencies = new $tw.Dependencies();
|
2012-06-10 16:26:54 +00:00
|
|
|
// Parse the text into runs or blocks
|
2012-06-04 12:38:33 +00:00
|
|
|
if(options.isRun) {
|
2012-06-10 16:26:54 +00:00
|
|
|
this.tree.push.apply(this.tree,this.parseRun());
|
2012-06-04 12:38:33 +00:00
|
|
|
} else {
|
2012-06-10 16:26:54 +00:00
|
|
|
this.tree.push.apply(this.tree,this.parseBlocks());
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Now make WikiTextRenderer inherit from the default Renderer class
|
|
|
|
*/
|
|
|
|
var Renderer = require("$:/core/modules/renderer.js").Renderer;
|
|
|
|
WikiTextRenderer.prototype = new Renderer();
|
|
|
|
WikiTextRenderer.constructor = WikiTextRenderer;
|
|
|
|
|
|
|
|
/*
|
2012-06-10 16:26:54 +00:00
|
|
|
Parse a block from the current position
|
|
|
|
terminatorRegExpString: optional regular expression string that identifies the end of plain paragraphs. Must not include capturing parenthesis
|
|
|
|
options: see below
|
|
|
|
Options are:
|
|
|
|
leaveTerminator: True if the terminator shouldn't be consumed
|
2012-05-26 17:30:32 +00:00
|
|
|
*/
|
2012-06-10 16:26:54 +00:00
|
|
|
WikiTextRenderer.prototype.parseBlock = function(terminatorRegExpString,options) {
|
|
|
|
var terminatorRegExp = terminatorRegExpString ? new RegExp("(" + terminatorRegExpString + "|\\r?\\n\\r?\\n)","mg") : /(\r?\n\r?\n)/mg;
|
2012-05-26 17:30:32 +00:00
|
|
|
this.skipWhitespace();
|
2012-05-26 22:42:48 +00:00
|
|
|
if(this.pos >= this.sourceLength) {
|
|
|
|
return [];
|
|
|
|
}
|
2012-05-26 17:30:32 +00:00
|
|
|
// Look for a block rule
|
2012-05-26 23:22:58 +00:00
|
|
|
this.parser.blockRegExp.lastIndex = this.pos;
|
|
|
|
var match = this.parser.blockRegExp.exec(this.source);
|
|
|
|
if(this.parser.blockRules.length && match && match.index === this.pos) {
|
2012-05-26 17:30:32 +00:00
|
|
|
var rule;
|
2012-05-26 23:22:58 +00:00
|
|
|
for(var t=0; t<this.parser.blockRules.length; t++) {
|
2012-05-26 17:30:32 +00:00
|
|
|
if(match[t+1]) {
|
2012-05-26 23:22:58 +00:00
|
|
|
rule = this.parser.blockRules[t];
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:51:52 +00:00
|
|
|
return rule ? rule.parse.call(this,match,true) : [];
|
2012-05-26 17:30:32 +00:00
|
|
|
} else {
|
|
|
|
// Treat it as a paragraph if we didn't find a block rule
|
2012-06-10 16:26:54 +00:00
|
|
|
return [$tw.Tree.Element("p",{},this.parseRun(terminatorRegExp,options))];
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-07 12:08:50 +00:00
|
|
|
/*
|
2012-06-10 16:26:54 +00:00
|
|
|
Parse blocks of text until a terminating regexp is encountered or the end of the text
|
|
|
|
terminatorRegExpString: terminating regular expression
|
2012-06-14 16:40:27 +00:00
|
|
|
options: none at present
|
2012-06-07 12:08:50 +00:00
|
|
|
*/
|
2012-06-10 16:26:54 +00:00
|
|
|
WikiTextRenderer.prototype.parseBlocks = function(terminatorRegExpString,options) {
|
|
|
|
if(terminatorRegExpString) {
|
|
|
|
return this.parseBlocksTerminated(terminatorRegExpString,options);
|
|
|
|
} else {
|
|
|
|
return this.parseBlocksUnterminated(options);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse a block from the current position to the end of the text
|
|
|
|
*/
|
|
|
|
WikiTextRenderer.prototype.parseBlocksUnterminated = function(options) {
|
2012-06-07 12:08:50 +00:00
|
|
|
var tree = [];
|
2012-06-10 16:26:54 +00:00
|
|
|
while(this.pos < this.sourceLength) {
|
|
|
|
tree.push.apply(tree,this.parseBlock());
|
|
|
|
}
|
|
|
|
return tree;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse blocks of text until a terminating regexp is encountered. See parseBlocks() for details
|
|
|
|
*/
|
|
|
|
WikiTextRenderer.prototype.parseBlocksTerminated = function(terminatorRegExpString,options) {
|
|
|
|
options = options || {};
|
|
|
|
var terminatorRegExp = new RegExp("(" + terminatorRegExpString + ")","mg"),
|
|
|
|
tree = [];
|
2012-06-07 12:08:50 +00:00
|
|
|
// Skip any whitespace
|
|
|
|
this.skipWhitespace();
|
|
|
|
// Check if we've got the end marker
|
|
|
|
terminatorRegExp.lastIndex = this.pos;
|
|
|
|
var match = terminatorRegExp.exec(this.source);
|
|
|
|
// Parse the text into blocks
|
|
|
|
while(this.pos < this.sourceLength && !(match && match.index === this.pos)) {
|
2012-06-10 16:26:54 +00:00
|
|
|
var blocks = this.parseBlock(terminatorRegExpString,{leaveTerminator: true});
|
2012-06-14 16:40:27 +00:00
|
|
|
tree.push.apply(tree,blocks);
|
2012-06-07 12:08:50 +00:00
|
|
|
// Skip any whitespace
|
|
|
|
this.skipWhitespace();
|
|
|
|
// Check if we've got the end marker
|
|
|
|
terminatorRegExp.lastIndex = this.pos;
|
|
|
|
match = terminatorRegExp.exec(this.source);
|
|
|
|
}
|
|
|
|
if(match && match.index === this.pos) {
|
|
|
|
this.pos = match.index + match[0].length;
|
|
|
|
}
|
|
|
|
return tree;
|
|
|
|
};
|
|
|
|
|
2012-05-26 17:30:32 +00:00
|
|
|
WikiTextRenderer.prototype.skipWhitespace = function() {
|
|
|
|
var whitespaceRegExp = /(\s+)/mg;
|
|
|
|
whitespaceRegExp.lastIndex = this.pos;
|
|
|
|
var whitespaceMatch = whitespaceRegExp.exec(this.source);
|
|
|
|
if(whitespaceMatch && whitespaceMatch.index === this.pos) {
|
|
|
|
this.pos = whitespaceRegExp.lastIndex;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse a run of text at the current position
|
|
|
|
terminatorRegExp: a regexp at which to stop the run
|
2012-06-05 13:41:30 +00:00
|
|
|
options: see below
|
|
|
|
|
|
|
|
Options are:
|
|
|
|
leaveTerminator: true if the terminator shouldn't be consumed
|
2012-05-26 17:30:32 +00:00
|
|
|
Returns an array of tree nodes
|
|
|
|
*/
|
2012-06-05 13:41:30 +00:00
|
|
|
WikiTextRenderer.prototype.parseRun = function(terminatorRegExp,options) {
|
2012-06-10 16:26:54 +00:00
|
|
|
if(terminatorRegExp) {
|
2012-06-05 13:41:30 +00:00
|
|
|
return this.parseRunTerminated(terminatorRegExp,options);
|
2012-06-10 16:26:54 +00:00
|
|
|
} else {
|
|
|
|
return this.parseRunUnterminated(options);
|
2012-06-04 12:38:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-05 13:41:30 +00:00
|
|
|
WikiTextRenderer.prototype.parseRunUnterminated = function(options) {
|
|
|
|
options = options || {};
|
2012-06-04 12:38:33 +00:00
|
|
|
var tree = [];
|
|
|
|
// Find the next occurrence of a runrule
|
|
|
|
this.parser.runRegExp.lastIndex = this.pos;
|
|
|
|
var runRuleMatch = this.parser.runRegExp.exec(this.source);
|
|
|
|
// Loop around until we've reached the end of the text
|
|
|
|
while(this.pos < this.sourceLength && runRuleMatch) {
|
|
|
|
// Process the text preceding the run rule
|
|
|
|
if(runRuleMatch.index > this.pos) {
|
|
|
|
tree.push($tw.Tree.Text(this.source.substring(this.pos,runRuleMatch.index)));
|
|
|
|
this.pos = runRuleMatch.index;
|
|
|
|
}
|
|
|
|
// Process the run rule
|
|
|
|
var rule;
|
|
|
|
for(var t=0; t<this.parser.runRules.length; t++) {
|
|
|
|
if(runRuleMatch[t+1]) {
|
|
|
|
rule = this.parser.runRules[t];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(rule) {
|
|
|
|
tree.push.apply(tree,rule.parse.call(this,runRuleMatch,false));
|
|
|
|
}
|
|
|
|
// Look for the next run rule
|
|
|
|
this.parser.runRegExp.lastIndex = this.pos;
|
|
|
|
runRuleMatch = this.parser.runRegExp.exec(this.source);
|
|
|
|
}
|
|
|
|
// Process the remaining text
|
|
|
|
if(this.pos < this.sourceLength) {
|
|
|
|
tree.push($tw.Tree.Text(this.source.substr(this.pos)));
|
|
|
|
}
|
|
|
|
this.pos = this.sourceLength;
|
|
|
|
return tree;
|
|
|
|
};
|
|
|
|
|
2012-06-05 13:41:30 +00:00
|
|
|
WikiTextRenderer.prototype.parseRunTerminated = function(terminatorRegExp,options) {
|
|
|
|
options = options || {};
|
2012-05-26 17:30:32 +00:00
|
|
|
var tree = [];
|
|
|
|
// Find the next occurrence of the terminator
|
|
|
|
terminatorRegExp.lastIndex = this.pos;
|
|
|
|
var terminatorMatch = terminatorRegExp.exec(this.source);
|
|
|
|
// Find the next occurrence of a runrule
|
2012-05-26 23:22:58 +00:00
|
|
|
this.parser.runRegExp.lastIndex = this.pos;
|
|
|
|
var runRuleMatch = this.parser.runRegExp.exec(this.source);
|
2012-05-26 17:30:32 +00:00
|
|
|
// Loop around until we've reached the end of the text
|
|
|
|
while(this.pos < this.sourceLength && (terminatorMatch || runRuleMatch)) {
|
|
|
|
// Return if we've found the terminator, and it precedes any run rule match
|
|
|
|
if(terminatorMatch) {
|
2012-06-02 09:15:41 +00:00
|
|
|
if(!runRuleMatch || runRuleMatch.index >= terminatorMatch.index) {
|
2012-05-26 17:30:32 +00:00
|
|
|
if(terminatorMatch.index > this.pos) {
|
|
|
|
tree.push($tw.Tree.Text(this.source.substring(this.pos,terminatorMatch.index)));
|
|
|
|
}
|
2012-06-05 13:41:30 +00:00
|
|
|
this.pos = terminatorMatch.index;
|
|
|
|
if(!options.leaveTerminator) {
|
|
|
|
this.pos += terminatorMatch[0].length;
|
|
|
|
}
|
2012-05-26 17:30:32 +00:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Process any run rule, along with the text preceding it
|
|
|
|
if(runRuleMatch) {
|
|
|
|
// Preceding text
|
|
|
|
if(runRuleMatch.index > this.pos) {
|
|
|
|
tree.push($tw.Tree.Text(this.source.substring(this.pos,runRuleMatch.index)));
|
|
|
|
this.pos = runRuleMatch.index;
|
|
|
|
}
|
|
|
|
// Process the run rule
|
|
|
|
var rule;
|
2012-05-26 23:22:58 +00:00
|
|
|
for(var t=0; t<this.parser.runRules.length; t++) {
|
2012-05-26 17:30:32 +00:00
|
|
|
if(runRuleMatch[t+1]) {
|
2012-05-26 23:22:58 +00:00
|
|
|
rule = this.parser.runRules[t];
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(rule) {
|
2012-05-28 14:51:52 +00:00
|
|
|
tree.push.apply(tree,rule.parse.call(this,runRuleMatch,false));
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
2012-06-05 21:01:06 +00:00
|
|
|
// Look for the next run rule and the next terminator match
|
2012-05-26 23:22:58 +00:00
|
|
|
this.parser.runRegExp.lastIndex = this.pos;
|
|
|
|
runRuleMatch = this.parser.runRegExp.exec(this.source);
|
2012-06-05 21:01:06 +00:00
|
|
|
terminatorRegExp.lastIndex = this.pos;
|
|
|
|
terminatorMatch = terminatorRegExp.exec(this.source);
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Process the remaining text
|
|
|
|
if(this.pos < this.sourceLength) {
|
2012-06-02 08:41:03 +00:00
|
|
|
tree.push($tw.Tree.Text(this.source.substr(this.pos)));
|
2012-05-26 17:30:32 +00:00
|
|
|
}
|
|
|
|
this.pos = this.sourceLength;
|
|
|
|
return tree;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse a run of text preceded by an optional class specifier `{{class}}`
|
|
|
|
*/
|
|
|
|
WikiTextRenderer.prototype.parseClassedRun = function(terminatorRegExp) {
|
|
|
|
var classRegExp = /\{\{([^\}]*)\}\}/mg,
|
|
|
|
className;
|
|
|
|
classRegExp.lastIndex = this.pos;
|
|
|
|
var match = classRegExp.exec(this.source);
|
|
|
|
if(match && match.index === this.pos) {
|
|
|
|
className = match[1];
|
|
|
|
this.pos = match.index + match[0].length;
|
|
|
|
}
|
|
|
|
var tree = this.parseRun(terminatorRegExp);
|
|
|
|
return {
|
|
|
|
"class": className,
|
|
|
|
tree: tree
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
The wikitext parser assembles the rules and uses the wikitext renderer to do the parsing
|
|
|
|
*/
|
|
|
|
var WikiTextParser = function(options) {
|
2012-05-26 23:22:58 +00:00
|
|
|
this.wiki = options.wiki;
|
|
|
|
// Assemble the rule regexps
|
|
|
|
this.blockRules = [];
|
|
|
|
this.runRules = [];
|
|
|
|
var blockRegExpStrings = [],
|
|
|
|
runRegExpStrings = [],
|
2012-11-14 11:23:43 +00:00
|
|
|
self = this;
|
|
|
|
$tw.modules.forEachModuleOfType("wikitextrule",function(title,module) {
|
|
|
|
if(module.blockParser) {
|
|
|
|
self.blockRules.push(module);
|
|
|
|
blockRegExpStrings.push("(" + module.regExpString + ")");
|
2012-05-26 23:22:58 +00:00
|
|
|
}
|
2012-11-14 11:23:43 +00:00
|
|
|
if(module.runParser) {
|
|
|
|
self.runRules.push(module);
|
|
|
|
runRegExpStrings.push("(" + module.regExpString + ")");
|
2012-05-26 23:22:58 +00:00
|
|
|
}
|
2012-11-14 11:23:43 +00:00
|
|
|
});
|
2012-05-26 23:22:58 +00:00
|
|
|
this.blockRegExp = new RegExp(blockRegExpStrings.join("|"),"mg");
|
|
|
|
this.runRegExp = new RegExp(runRegExpStrings.join("|"),"mg");
|
2012-05-26 17:30:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
The wikitext parser constructs a wikitext renderer to do the work
|
|
|
|
*/
|
|
|
|
WikiTextParser.prototype.parse = function(type,text) {
|
|
|
|
return new WikiTextRenderer(text,{
|
|
|
|
wiki: this.wiki,
|
2012-06-04 12:38:33 +00:00
|
|
|
parser: this,
|
|
|
|
isRun: type === "text/x-tiddlywiki-run"
|
2012-05-26 17:30:32 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-07-14 17:08:00 +00:00
|
|
|
exports["text/x-tiddlywiki"] = WikiTextParser;
|
2012-05-26 17:30:32 +00:00
|
|
|
|
2012-06-04 12:38:33 +00:00
|
|
|
exports["text/x-tiddlywiki-run"] = WikiTextParser;
|
|
|
|
|
2012-05-26 17:30:32 +00:00
|
|
|
})();
|