1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-27 02:20:28 +00:00
TiddlyWiki5/js/TiddlyTextParser.js
Jeremy Ruston 9ec268cd2f Add a parser for TiddlyText
TiddlyText being plain text with macros and transclusions. It's the
equivalent of getRecursiveTiddlerText in classic TW
2012-04-19 10:15:11 +02:00

59 lines
1.4 KiB
JavaScript

/*\
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;
})();