/* Various static utility functions concerned with parsing and generating representations of tiddlers and other objects. This file is a bit of a dumping ground; the expectation is that most of these functions will be refactored. */ var argParser = require("./ArgParser.js"); var tiddlerUtils = exports; /* Parse a tiddler given its mimetype, and merge the results into a hashmap of tiddler fields. A file extension can be passed as a shortcut for the mimetype, as shown in tiddlerUtils.fileExtensionMappings. For example ".txt" file extension is mapped to the "text/plain" mimetype. Special processing to extract embedded metadata is applied to some mimetypes. */ tiddlerUtils.parseTiddler = function(text,type,fields) { if(fields === undefined) { var fields = {}; } // Map extensions to mimetpyes var fileExtensionMapping = tiddlerUtils.fileExtensionMappings[type]; if(fileExtensionMapping) type = fileExtensionMapping; // Invoke the parser for the specified mimetype var parser = tiddlerUtils.parseTiddlerByMimeType[type]; if(parser) { return parser(text,fields); } return fields; } tiddlerUtils.fileExtensionMappings = { ".txt": "text/plain", ".html": "text/html", ".tiddler": "application/x-tiddler-html-div", ".tid": "application/x-tiddler", ".js": "application/javascript" } tiddlerUtils.parseTiddlerByMimeType = { "text/plain": function(text,fields) { fields.text = text; return fields; }, "text/html": function(text,fields) { fields.text = text; return fields; }, "application/x-tiddler-html-div": function(text,fields) { fields = tiddlerUtils.parseTiddlerDiv(text,fields); return fields; }, "application/x-tiddler": function(text,fields) { var split = text.indexOf("\n\n"); if(split === -1) { split = text.length; } fields = tiddlerUtils.parseMetaDataBlock(text.substr(0,split),fields); fields.text = text.substr(split + 2); return fields; }, "application/javascript": function(text,fields) { fields.text = text; return fields; } } /* Parse a block of metadata and merge the results into a hashmap of tiddler fields. The block consists of newline delimited lines consisting of the field name, a colon, and then the value. For example: title: Safari modifier: blaine created: 20110211110700 modified: 20110211131020 tags: browsers issues creator: psd */ tiddlerUtils.parseMetaDataBlock = function(metaData,fields) { if(fields === undefined) { var fields = {}; } metaData.split("\n").forEach(function(line) { var p = line.indexOf(":"); if(p !== -1) { var field = line.substr(0, p).trim(); var value = line.substr(p+1).trim(); fields[field] = tiddlerUtils.parseMetaDataItem(field,value); } }); return fields; } /* Parse an old-style tiddler DIV. It looks like this:
The text of the tiddler (without the expected HTML encoding).
Note that the field attributes are HTML encoded, but that the body of the
 tag is not.
*/
tiddlerUtils.parseTiddlerDiv = function(text,fields) {
	if(fields === undefined) {
		var fields = {};
	}
	var divRegExp = /^\s*]*)>((?:.|\n)*)<\/div>\s*$/gi;
	var subDivRegExp = /^(?:\s*
)((?:.|\n)*)(?:<\/pre>\s*)$/gi;
	var attrRegExp = /\s*([^=\s]+)\s*=\s*"([^"]*)"/gi;
	var match = divRegExp.exec(text);
	if(match) {
		var subMatch = subDivRegExp.exec(match[2]); // Body of the 
tag if(subMatch) { fields.text = subMatch[1]; } else { fields.text = match[2]; } do { var attrMatch = attrRegExp.exec(match[1]); if(attrMatch) { var name = attrMatch[1]; var value = attrMatch[2]; fields[name] = tiddlerUtils.parseMetaDataItem(name,value); } } while(attrMatch); } return fields; } // Output a tiddler as an HTML
// out - array to push the output strings // tid - the tiddler to be output // options - options: // omitPrecedingLineFeed - determines if a linefeed is inserted between the
 tag and the text
tiddlerUtils.outputTiddlerDiv = function(out,tid,options) {
	var result = [];
	var outputAttribute = function(name,value) {
		result.push(" " + name + "=\"" + value + "\"");
	};
	result.push("\n
");
	if(!(options && options.omitPrecedingLineFeed))
		result.push("\n");
	result.push(tiddlerUtils.htmlEncode(tid.fields.text));
	result.push("
\n
"); out.push(result.join("")); } tiddlerUtils.stringifyTags = function(tags) { var results = []; for(var t=0; t to ">" and " to """ tiddlerUtils.htmlEncode = function(s) { return s.replace(/&/mg,"&").replace(//mg,">").replace(/\"/mg,"""); }; // Convert "&" to &, "<" to <, ">" to > and """ to " tiddlerUtils.htmlDecode = function(s) { return s.replace(/</mg,"<").replace(/>/mg,">").replace(/"/mg,"\"").replace(/&/mg,"&"); };