From f50e025f7c919b6c22bc2130862e1048034525f1 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 5 Jun 2012 14:41:30 +0100 Subject: [PATCH] Refactored run parser so that it can optionally not consume the terminator --- .../newwikitextparser/newwikitextparser.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/core/modules/parsers/newwikitextparser/newwikitextparser.js b/core/modules/parsers/newwikitextparser/newwikitextparser.js index 6397f607f..ed9837c86 100644 --- a/core/modules/parsers/newwikitextparser/newwikitextparser.js +++ b/core/modules/parsers/newwikitextparser/newwikitextparser.js @@ -77,17 +77,22 @@ WikiTextRenderer.prototype.skipWhitespace = function() { /* Parse a run of text at the current position terminatorRegExp: a regexp at which to stop the run + options: see below + +Options are: + leaveTerminator: true if the terminator shouldn't be consumed Returns an array of tree nodes */ -WikiTextRenderer.prototype.parseRun = function(terminatorRegExp) { +WikiTextRenderer.prototype.parseRun = function(terminatorRegExp,options) { if(terminatorRegExp === null) { - return this.parseRunUnterminated(); + return this.parseRunUnterminated(options); } else { - return this.parseRunTerminated(terminatorRegExp); + return this.parseRunTerminated(terminatorRegExp,options); } }; -WikiTextRenderer.prototype.parseRunUnterminated = function() { +WikiTextRenderer.prototype.parseRunUnterminated = function(options) { + options = options || {}; var tree = []; // Find the next occurrence of a runrule this.parser.runRegExp.lastIndex = this.pos; @@ -121,7 +126,8 @@ WikiTextRenderer.prototype.parseRunUnterminated = function() { return tree; }; -WikiTextRenderer.prototype.parseRunTerminated = function(terminatorRegExp) { +WikiTextRenderer.prototype.parseRunTerminated = function(terminatorRegExp,options) { + options = options || {}; var tree = []; // Find the next occurrence of the terminator terminatorRegExp = terminatorRegExp || /(\r?\n\r?\n)/mg; @@ -138,7 +144,10 @@ WikiTextRenderer.prototype.parseRunTerminated = function(terminatorRegExp) { if(terminatorMatch.index > this.pos) { tree.push($tw.Tree.Text(this.source.substring(this.pos,terminatorMatch.index))); } - this.pos = terminatorMatch.index + terminatorMatch[0].length; + this.pos = terminatorMatch.index; + if(!options.leaveTerminator) { + this.pos += terminatorMatch[0].length; + } return tree; } }