From 55dd392fe4507c7654349f54f5aef68ba8412007 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 5 Jun 2012 14:43:31 +0100 Subject: [PATCH] Added run parser for emphasis like bold and italics --- .../newwikitextparser/rules/emphasis.js | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 core/modules/parsers/newwikitextparser/rules/emphasis.js diff --git a/core/modules/parsers/newwikitextparser/rules/emphasis.js b/core/modules/parsers/newwikitextparser/rules/emphasis.js new file mode 100644 index 000000000..c62dc41d9 --- /dev/null +++ b/core/modules/parsers/newwikitextparser/rules/emphasis.js @@ -0,0 +1,64 @@ +/*\ +title: $:/core/modules/parsers/newwikitextparser/rules/emphasis.js +type: application/javascript +module-type: wikitextrule + +Wiki text run rule for emphasis + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.name = "emphasis"; + +exports.runParser = true; + +exports.regExpString = "''|\/\/|__|\\^\\^|~~|--(?!\\s|$)"; + +exports.parse = function(match,isBlock) { + this.pos = match.index + match[0].length; + var el,regExp; + switch(match[0]) { + case "''": // Bold + el = "strong"; + regExp = /('')|(\r?\n)/mg; + break; + case "//": // Italics + el = "em"; + regExp = /(\/\/)|(\r?\n)/mg; + break; + case "__": // Underline + el = "u"; + regExp = /(__)|(\r?\n)/mg; + break; + case "^^": + el = "sup"; + regExp = /(\^\^)|(\r?\n)/mg; + break; + case "~~": + el = "sub"; + regExp = /(~~)|(\r?\n)/mg; + break; + case "--": + el = "strike"; + regExp = /(--)|(\r?\n)/mg; + break; + } + // Parse the run up to the terminator + var tree = this.parseRun(regExp,{leaveTerminator: true}); + // Check for the terminator + regExp.lastIndex = this.pos; + match = regExp.exec(this.source); + if(match && match.index === this.pos) { + // Only consume the terminator if it isn't a line break + if(match[1]) { + this.pos = match.index + match[0].length; + } + } + return [$tw.Tree.Element(el,{},tree)]; +}; + +})();