2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/TiddlerOutput.js
|
2011-12-12 10:52:04 +00:00
|
|
|
|
2011-11-22 17:42:03 +00:00
|
|
|
Functions concerned with parsing representations of tiddlers
|
2011-12-13 12:30:09 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
2011-11-22 17:42:03 +00:00
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-11-30 17:27:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2011-12-11 14:51:48 +00:00
|
|
|
var utils = require("./Utils.js"),
|
|
|
|
util = require("util");
|
2011-11-22 17:42:03 +00:00
|
|
|
|
|
|
|
var tiddlerOutput = exports;
|
|
|
|
|
2011-12-11 14:51:48 +00:00
|
|
|
// Utility function to convert a tags string array into a TiddlyWiki-style quoted tags string
|
|
|
|
var stringifyTags = function(tags) {
|
|
|
|
var results = [];
|
|
|
|
for(var t=0; t<tags.length; t++) {
|
|
|
|
if(tags[t].indexOf(" ") !== -1) {
|
|
|
|
results.push("[[" + tags[t] + "]]");
|
|
|
|
} else {
|
|
|
|
results.push(tags[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results.join(" ");
|
|
|
|
};
|
|
|
|
|
2011-11-25 19:20:35 +00:00
|
|
|
/*
|
|
|
|
Output a tiddler as a .tid file
|
|
|
|
*/
|
2011-12-11 14:51:48 +00:00
|
|
|
var outputTiddler = function(tid) {
|
2011-11-28 17:04:39 +00:00
|
|
|
var result = [],
|
|
|
|
outputAttribute = function(name,value) {
|
2011-11-25 19:20:35 +00:00
|
|
|
result.push(name + ": " + value + "\n");
|
|
|
|
};
|
|
|
|
for(var t in tid.fields) {
|
|
|
|
switch(t) {
|
|
|
|
case "text":
|
|
|
|
// Ignore the text field
|
|
|
|
break;
|
|
|
|
case "tags":
|
|
|
|
// Output tags as a list
|
|
|
|
outputAttribute(t,tiddlerOutput.stringifyTags(tid.fields.tags));
|
|
|
|
break;
|
|
|
|
case "modified":
|
|
|
|
case "created":
|
|
|
|
// Output dates in YYYYMMDDHHMM
|
|
|
|
outputAttribute(t,utils.convertToYYYYMMDDHHMM(tid.fields[t]));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Output other attributes raw
|
|
|
|
outputAttribute(t,tid.fields[t]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push("\n");
|
|
|
|
result.push(tid.fields.text);
|
|
|
|
return result.join("");
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-25 19:20:35 +00:00
|
|
|
|
2011-11-22 17:42:03 +00:00
|
|
|
/*
|
|
|
|
Output a tiddler as an HTML <DIV>
|
|
|
|
out - array to push the output strings
|
|
|
|
tid - the tiddler to be output
|
2011-12-03 13:01:24 +00:00
|
|
|
The fields are in the order title, creator, modifier, created, modified, tags, followed by any others
|
2011-11-22 17:42:03 +00:00
|
|
|
*/
|
2011-12-11 14:51:48 +00:00
|
|
|
var outputTiddlerDiv = function(tid) {
|
2011-11-28 17:04:39 +00:00
|
|
|
var result = [],
|
2011-12-03 13:01:24 +00:00
|
|
|
attributes = {},
|
|
|
|
outputAttribute = function(name,transform,dontDelete) {
|
|
|
|
if(name in attributes) {
|
|
|
|
var value = attributes[name];
|
|
|
|
if(transform)
|
|
|
|
value = transform(value);
|
|
|
|
result.push(" " + name + "=\"" + value + "\"");
|
|
|
|
if(!dontDelete) {
|
|
|
|
delete attributes[name];
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 17:04:39 +00:00
|
|
|
};
|
2011-11-22 17:42:03 +00:00
|
|
|
for(var t in tid.fields) {
|
2011-12-03 13:01:24 +00:00
|
|
|
attributes[t] = tid.fields[t];
|
|
|
|
}
|
|
|
|
if(attributes.text) {
|
|
|
|
delete attributes.text;
|
|
|
|
}
|
|
|
|
result.push("<div");
|
|
|
|
// Output the standard attributes in the correct order
|
|
|
|
outputAttribute("title");
|
|
|
|
outputAttribute("creator");
|
|
|
|
outputAttribute("modifier");
|
2011-12-05 16:50:25 +00:00
|
|
|
outputAttribute("created", function(v) {return utils.convertToYYYYMMDDHHMM(v);});
|
|
|
|
outputAttribute("modified", function(v) {return utils.convertToYYYYMMDDHHMM(v);});
|
2011-12-11 14:51:48 +00:00
|
|
|
outputAttribute("tags", function(v) {return stringifyTags(v);});
|
2011-12-03 13:01:24 +00:00
|
|
|
// Output any other attributes
|
|
|
|
for(t in attributes) {
|
|
|
|
outputAttribute(t,null,true);
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
2011-12-03 12:01:08 +00:00
|
|
|
result.push(">\n<pre>");
|
2011-11-22 17:42:03 +00:00
|
|
|
result.push(utils.htmlEncode(tid.fields.text));
|
|
|
|
result.push("</pre>\n</div>");
|
2011-11-25 13:27:40 +00:00
|
|
|
return result.join("");
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 17:42:03 +00:00
|
|
|
|
2011-12-11 14:51:48 +00:00
|
|
|
tiddlerOutput.register = function(tiddlerConverters) {
|
|
|
|
tiddlerConverters.registerSerializer(".tid","application/x-tiddler",outputTiddler);
|
|
|
|
tiddlerConverters.registerSerializer(".tiddler","application/x-tiddler-html-div",outputTiddlerDiv);
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|