2011-11-22 17:42:03 +00:00
|
|
|
/*
|
|
|
|
Functions concerned with parsing representations of tiddlers
|
|
|
|
*/
|
|
|
|
|
2011-11-30 17:27:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2011-11-30 19:35:01 +00:00
|
|
|
var utils = require("./Utils.js"),
|
2011-11-30 16:29:49 +00:00
|
|
|
util = require("util");
|
2011-11-22 17:42:03 +00:00
|
|
|
|
|
|
|
var tiddlerInput = 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-30 16:06:34 +00:00
|
|
|
tiddlerInput.parseTiddlerFile = function(text,type,fields) {
|
2011-11-22 17:42:03 +00:00
|
|
|
if(fields === undefined) {
|
|
|
|
var fields = {};
|
|
|
|
}
|
|
|
|
// Map extensions to mimetpyes
|
|
|
|
var fileExtensionMapping = tiddlerInput.fileExtensionMappings[type];
|
|
|
|
if(fileExtensionMapping)
|
|
|
|
type = fileExtensionMapping;
|
|
|
|
// Invoke the parser for the specified mimetype
|
2011-11-30 16:06:34 +00:00
|
|
|
var parser = tiddlerInput.parseTiddlerFileByMimeType[type];
|
2011-11-22 17:42:03 +00:00
|
|
|
if(parser) {
|
|
|
|
return parser(text,fields);
|
2011-11-30 11:41:45 +00:00
|
|
|
} else {
|
2011-11-30 16:06:34 +00:00
|
|
|
throw new Error("Unknown tiddler type in tiddlerInput.parseTiddlerFile: " + type);
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tiddlerInput.fileExtensionMappings = {
|
|
|
|
".txt": "text/plain",
|
|
|
|
".html": "text/html",
|
|
|
|
".tiddler": "application/x-tiddler-html-div",
|
|
|
|
".tid": "application/x-tiddler",
|
2011-11-30 16:06:34 +00:00
|
|
|
".js": "application/javascript",
|
|
|
|
".json": "application/json"
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 16:06:34 +00:00
|
|
|
tiddlerInput.parseTiddlerFileByMimeType = {
|
2011-11-22 17:42:03 +00:00
|
|
|
"text/plain": function(text,fields) {
|
|
|
|
fields.text = text;
|
2011-11-30 16:06:34 +00:00
|
|
|
return [fields];
|
2011-11-22 17:42:03 +00:00
|
|
|
},
|
|
|
|
"text/html": function(text,fields) {
|
|
|
|
fields.text = text;
|
2011-11-30 16:06:34 +00:00
|
|
|
return [fields];
|
2011-11-22 17:42:03 +00:00
|
|
|
},
|
|
|
|
"application/x-tiddler-html-div": function(text,fields) {
|
2011-11-30 16:06:34 +00:00
|
|
|
return [tiddlerInput.parseTiddlerDiv(text,fields)];
|
2011-11-22 17:42:03 +00:00
|
|
|
},
|
|
|
|
"application/x-tiddler": function(text,fields) {
|
|
|
|
var split = text.indexOf("\n\n");
|
|
|
|
if(split === -1) {
|
|
|
|
split = text.length;
|
|
|
|
}
|
|
|
|
fields = tiddlerInput.parseMetaDataBlock(text.substr(0,split),fields);
|
|
|
|
fields.text = text.substr(split + 2);
|
2011-11-30 16:06:34 +00:00
|
|
|
return [fields];
|
2011-11-22 17:42:03 +00:00
|
|
|
},
|
|
|
|
"application/javascript": function(text,fields) {
|
|
|
|
fields.text = text;
|
2011-11-30 16:06:34 +00:00
|
|
|
return [fields];
|
|
|
|
},
|
|
|
|
"application/json": function(text,fields) {
|
2011-11-30 19:35:01 +00:00
|
|
|
var tiddlers = JSON.parse(text),
|
|
|
|
result = [],
|
|
|
|
getKnownFields = function(tid) {
|
|
|
|
var fields = {};
|
|
|
|
"title text created creator modified modifier type tags".split(" ").forEach(function(value) {
|
|
|
|
fields[value] = tid[value];
|
|
|
|
});
|
|
|
|
return fields;
|
|
|
|
};
|
2011-11-30 16:29:49 +00:00
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
2011-11-30 19:35:01 +00:00
|
|
|
result.push(getKnownFields(tiddlers[t]));
|
2011-11-30 16:29:49 +00:00
|
|
|
}
|
|
|
|
return result;
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
tiddlerInput.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();
|
2011-11-30 18:53:43 +00:00
|
|
|
fields[field] = value;
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse an old-style tiddler DIV. It looks like this:
|
|
|
|
|
|
|
|
<div title="Title" creator="JoeBloggs" modifier="JoeBloggs" created="201102111106" modified="201102111310" tags="myTag [[my long tag]]">
|
|
|
|
<pre>The text of the tiddler (without the expected HTML encoding).
|
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
Note that the field attributes are HTML encoded, but that the body of the <PRE> tag is not.
|
|
|
|
*/
|
|
|
|
tiddlerInput.parseTiddlerDiv = function(text,fields) {
|
|
|
|
if(fields === undefined) {
|
|
|
|
var fields = {};
|
|
|
|
}
|
2011-11-28 17:04:39 +00:00
|
|
|
var divRegExp = /^\s*<div\s+([^>]*)>((?:.|\n)*)<\/div>\s*$/gi,
|
|
|
|
subDivRegExp = /^\s*<pre>((?:.|\n)*)<\/pre>\s*$/gi,
|
|
|
|
attrRegExp = /\s*([^=\s]+)\s*=\s*"([^"]*)"/gi,
|
|
|
|
match = divRegExp.exec(text);
|
2011-11-22 17:42:03 +00:00
|
|
|
if(match) {
|
|
|
|
var subMatch = subDivRegExp.exec(match[2]); // Body of the <DIV> tag
|
|
|
|
if(subMatch) {
|
|
|
|
fields.text = subMatch[1];
|
|
|
|
} else {
|
|
|
|
fields.text = match[2];
|
|
|
|
}
|
2011-11-30 16:06:34 +00:00
|
|
|
var attrMatch;
|
2011-11-22 17:42:03 +00:00
|
|
|
do {
|
2011-11-30 16:06:34 +00:00
|
|
|
attrMatch = attrRegExp.exec(match[1]);
|
2011-11-22 17:42:03 +00:00
|
|
|
if(attrMatch) {
|
|
|
|
var name = attrMatch[1];
|
|
|
|
var value = attrMatch[2];
|
2011-11-30 18:53:43 +00:00
|
|
|
fields[name] = value;
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
|
|
|
} while(attrMatch);
|
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|