From 9ec268cd2f40d001327614a718a78929693fdc4e Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Thu, 19 Apr 2012 10:15:11 +0200 Subject: [PATCH] Add a parser for TiddlyText TiddlyText being plain text with macros and transclusions. It's the equivalent of getRecursiveTiddlerText in classic TW --- js/TiddlyTextParser.js | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 js/TiddlyTextParser.js diff --git a/js/TiddlyTextParser.js b/js/TiddlyTextParser.js new file mode 100644 index 000000000..b4752c643 --- /dev/null +++ b/js/TiddlyTextParser.js @@ -0,0 +1,58 @@ +/*\ +title: js/TiddlyTextParser.js + +Parses a plain text block that can also contain macros and transclusions. + +The syntax for transclusions is: + + [[tiddlerTitle]] + +The syntax for macros is: + + <> + +\*/ +(function(){ + +/*jslint node: true */ +"use strict"; + +var Dependencies = require("./Dependencies.js").Dependencies, + Renderer = require("./Renderer.js").Renderer, + utils = require("./Utils.js"); + +var TiddlyTextParser = function(options) { + this.store = options.store; +}; + +TiddlyTextParser.prototype.parse = function(type,text) { + var output = [], + dependencies = new Dependencies(), + macroRegExp = /(?:\[\[([^\]]+)\]\])|(?:<<([^>\s]+)(?:\s*)((?:[^>]|(?:>(?!>)))*)>>)/mg, + lastMatchPos = 0, + match, + macroNode; + do { + match = macroRegExp.exec(text); + if(match) { + output.push(Renderer.TextNode(text.substring(lastMatchPos,match.index))); + if(match[1]) { // Transclusion + macroNode = Renderer.MacroNode("tiddler",{ + target: match[1] + },[],this.store); + } else if(match[2]) { // Macro call + macroNode = Renderer.MacroNode(match[2],match[3],[],this.store); + } + output.push(macroNode); + dependencies.mergeDependencies(macroNode.dependencies); + lastMatchPos = match.index + match[0].length; + } else { + output.push(Renderer.TextNode(text.substr(lastMatchPos))); + } + } while(match); + return new Renderer(output,dependencies,this.store); +}; + +exports.TiddlyTextParser = TiddlyTextParser; + +})();