1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-03 10:43:16 +00:00

Add a parser for TiddlyText

TiddlyText being plain text with macros and transclusions. It's the
equivalent of getRecursiveTiddlerText in classic TW
This commit is contained in:
Jeremy Ruston 2012-04-19 10:15:11 +02:00
parent 4197358281
commit 9ec268cd2f

58
js/TiddlyTextParser.js Normal file
View File

@ -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:
<<macroName params>>
\*/
(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;
})();