/*\ title: $:/core/modules/parsers/newwikitextparser/newwikitextparser.js 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(); if(options.isRun) { this.tree.push.apply(this.tree,this.parseRun(null)); } else { // Parse the text into blocks while(this.pos < this.sourceLength) { this.tree.push.apply(this.tree,this.parseBlock()); } } }; /* Now make WikiTextRenderer inherit from the default Renderer class */ var Renderer = require("$:/core/modules/renderer.js").Renderer; WikiTextRenderer.prototype = new Renderer(); WikiTextRenderer.constructor = WikiTextRenderer; /* Parse a block of text at the current position */ WikiTextRenderer.prototype.parseBlock = function() { this.skipWhitespace(); if(this.pos >= this.sourceLength) { return []; } // Look for a block rule this.parser.blockRegExp.lastIndex = this.pos; var match = this.parser.blockRegExp.exec(this.source); if(this.parser.blockRules.length && match && match.index === this.pos) { var rule; for(var t=0; t 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= terminatorMatch.index) { if(terminatorMatch.index > this.pos) { tree.push($tw.Tree.Text(this.source.substring(this.pos,terminatorMatch.index))); } this.pos = terminatorMatch.index + terminatorMatch[0].length; 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; for(var t=0; t