diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..e43b0f988 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/js/ArgParser.js b/js/ArgParser.js old mode 100644 new mode 100755 diff --git a/js/Recipe.js b/js/Recipe.js old mode 100644 new mode 100755 index d0c2e1368..7c1a0a767 --- a/js/Recipe.js +++ b/js/Recipe.js @@ -28,7 +28,9 @@ this.ingredients = { */ var tiddler = require("./Tiddler.js"), - tiddlerUtils = require("./TiddlerUtils.js"), + tiddlerInput = require("./TiddlerInput.js"), + tiddlerOutput = require("./TiddlerOutput.js"), + utils = require("./Utils.js"), tiddlywiki = require("./TiddlyWiki.js"), fs = require("fs"), path = require("path"), @@ -102,11 +104,11 @@ Recipe.prototype.readIngredient = function(dirname,filepath) { title: basename }; // Read the tiddler file - fields = tiddlerUtils.parseTiddler(fs.readFileSync(fullpath,"utf8"),extname,fields); + fields = tiddlerInput.parseTiddler(fs.readFileSync(fullpath,"utf8"),extname,fields); // Check for the .meta file var metafile = fullpath + ".meta"; if(path.existsSync(metafile)) { - fields = tiddlerUtils.parseMetaDataBlock(fs.readFileSync(metafile,"utf8"),fields); + fields = tiddlerInput.parseMetaDataBlock(fs.readFileSync(metafile,"utf8"),fields); } return fields; } @@ -164,7 +166,7 @@ Recipe.ingredientOutputter = { // Ordinary tiddlers are output as a
The text of the tiddler (without the expected HTML encoding). ++
tag is not. +*/ +tiddlerInput.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 thetag + 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] = tiddlerInput.parseMetaDataItem(name,value); + } + } while(attrMatch); + } + return fields; +} + +/* +Parse a single metadata field/value pair and return the value as the appropriate data type +*/ +tiddlerInput.parseMetaDataItem = function(field,value) { + var result; + switch(field) { + case "modified": + case "created": + result = utils.convertFromYYYYMMDDHHMMSS(value); + break; + case "tags": + var parser = new argParser.ArgParser(value,{noNames: true}); + result = parser.getValuesByName("",""); + break; + default: + result = value; + break; + } + return result; +} diff --git a/js/TiddlerOutput.js b/js/TiddlerOutput.js new file mode 100755 index 000000000..aa4b52a33 --- /dev/null +++ b/js/TiddlerOutput.js @@ -0,0 +1,61 @@ +/* +Functions concerned with parsing representations of tiddlers +*/ + +var argParser = require("./ArgParser.js"), + utils = require("./Utils.js"); + +var tiddlerOutput = exports; + +/* +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- -Note that the field attributes are HTML encoded, but that the body of thetag and the text +*/ +tiddlerOutput.outputTiddlerDiv = function(out,tid) { + var result = []; + var outputAttribute = function(name,value) { + result.push(" " + name + "=\"" + value + "\""); + }; + result.push("\n"); + out.push(result.join("")); +} + +tiddlerOutput.stringifyTags = function(tags) { + var results = []; + for(var t=0; t\n"); + result.push(utils.htmlEncode(tid.fields.text)); + result.push("\n- The text of the tiddler (without the expected HTML encoding). --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 thetag - 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 thetag and the text -tiddlerUtils.outputTiddlerDiv = function(out,tid,options) { - var result = []; - var outputAttribute = function(name,value) { - result.push(" " + name + "=\"" + value + "\""); - }; - result.push("\n"); - out.push(result.join("")); -} - -tiddlerUtils.stringifyTags = function(tags) { - var results = []; - for(var t=0; t"); - if(!(options && options.omitPrecedingLineFeed)) - result.push("\n"); - result.push(tiddlerUtils.htmlEncode(tid.fields.text)); - result.push("\nto ">" 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,"&"); -}; - diff --git a/js/TiddlyWiki.js b/js/TiddlyWiki.js old mode 100644 new mode 100755 diff --git a/js/Utils.js b/js/Utils.js new file mode 100755 index 000000000..c3c39b7ab --- /dev/null +++ b/js/Utils.js @@ -0,0 +1,74 @@ +/* +Various static utility functions. + +This file is a bit of a dumping ground; the expectation is that most of these functions will be refactored. +*/ + +var utils = exports; + +// Pad a string to a certain length with zeros +utils.zeroPad = function(n,d) +{ + var s = n.toString(); + if(s.length < d) + s = "000000000000000000000000000".substr(0,d-s.length) + s; + return s; +}; + +// Convert a date to local YYYYMMDDHHMM string format +utils.convertToLocalYYYYMMDDHHMM = function(date) +{ + return date.getFullYear() + utils.zeroPad(date.getMonth()+1,2) + utils.zeroPad(date.getDate(),2) + utils.zeroPad(date.getHours(),2) + utils.zeroPad(date.getMinutes(),2); +}; + +// Convert a date to UTC YYYYMMDDHHMM string format +utils.convertToYYYYMMDDHHMM = function(date) +{ + return date.getUTCFullYear() + utils.zeroPad(date.getUTCMonth()+1,2) + utils.zeroPad(date.getUTCDate(),2) + utils.zeroPad(date.getUTCHours(),2) + utils.zeroPad(date.getUTCMinutes(),2); +}; + +// Convert a date to UTC YYYYMMDD.HHMMSSMMM string format +utils.convertToYYYYMMDDHHMMSSMMM = function(date) +{ + return date.getUTCFullYear() + utils.zeroPad(date.getUTCMonth()+1,2) + utils.zeroPad(date.getUTCDate(),2) + "." + utils.zeroPad(date.getUTCHours(),2) + utils.zeroPad(date.getUTCMinutes(),2) + utils.zeroPad(date.getUTCSeconds(),2) + utils.zeroPad(date.getUTCMilliseconds(),3) +"0"; +}; + +// Create a date from a UTC YYYYMMDDHHMM format string +utils.convertFromYYYYMMDDHHMM = function(d) +{ + d = d?d.replace(/[^0-9]/g, ""):""; + return utils.convertFromYYYYMMDDHHMMSSMMM(d.substr(0,12)); +}; + +// Create a date from a UTC YYYYMMDDHHMMSS format string +utils.convertFromYYYYMMDDHHMMSS = function(d) +{ + d = d?d.replace(/[^0-9]/g, ""):""; + return utils.convertFromYYYYMMDDHHMMSSMMM(d.substr(0,14)); +}; + +// Create a date from a UTC YYYYMMDDHHMMSSMMM format string +utils.convertFromYYYYMMDDHHMMSSMMM = function(d) +{ + d = d ? d.replace(/[^0-9]/g, "") : ""; + return new Date(Date.UTC(parseInt(d.substr(0,4),10), + parseInt(d.substr(4,2),10)-1, + parseInt(d.substr(6,2),10), + parseInt(d.substr(8,2)||"00",10), + parseInt(d.substr(10,2)||"00",10), + parseInt(d.substr(12,2)||"00",10), + parseInt(d.substr(14,3)||"000",10))); +}; + +// Convert & to "&", < to "<", > to ">" and " to """ +utils.htmlEncode = function(s) +{ + return s.replace(/&/mg,"&").replace(//mg,">").replace(/\"/mg,"""); +}; + +// Convert "&" to &, "<" to <, ">" to > and """ to " +utils.htmlDecode = function(s) +{ + return s.replace(/</mg,"<").replace(/>/mg,">").replace(/"/mg,"\"").replace(/&/mg,"&"); +}; +