/*\ title: $:/core/modules/parsers/wikitextparser/wikitextparser.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(); // Parse the text into runs or blocks if(options.isRun) { this.tree.push.apply(this.tree,this.parseRun()); } else { this.tree.push.apply(this.tree,this.parseBlocks()); } }; /* 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 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 */ WikiTextRenderer.prototype.parseBlock = function(terminatorRegExpString,options) { var terminatorRegExp = terminatorRegExpString ? new RegExp("(" + terminatorRegExpString + "|\\r?\\n\\r?\\n)","mg") : /(\r?\n\r?\n)/mg; 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; if(!options.leaveTerminator) { this.pos += 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