diff --git a/boot/boot.js b/boot/boot.js index a9c84a25c..7b1129cbd 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -1968,9 +1968,12 @@ $tw.deferredDirSpecs = []; /* Load all the tiddlers defined by a `tiddlywiki.files` specification file filepath: pathname of the directory containing the specification file +options: + loadDeferred {bool|undefined}: wheter or not to load the tiddlers marked as "deferred" in the specification. */ -$tw.loadTiddlersFromSpecification = function(filepath,excludeRegExp,loadDeferred) { - var loadDeferred = loadDeferred || false; +$tw.loadTiddlersFromSpecification = function(filepath,excludeRegExp,options) { + options = options || {}; + var loadDeferred = options.loadDeferred || false; var tiddlers = []; // Read the specification var filesInfo = $tw.utils.parseJSONSafe(fs.readFileSync(filepath + path.sep + "tiddlywiki.files","utf8")); diff --git a/core/modules/startup/load-defer.js b/core/modules/startup/load-defer.js index ab4c8b7bb..dba9d1e3a 100644 --- a/core/modules/startup/load-defer.js +++ b/core/modules/startup/load-defer.js @@ -5,54 +5,49 @@ module-type: startup Register tiddlerloader plugins and load deferred tiddlers. \*/ -(function(){ - ospath = require("path"); +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; - /*jslint node: true, browser: true */ - /*global $tw: false */ - "use strict"; - - // Export name and synchronous status - exports.name = "load-defer"; - exports.platforms = ["node"]; - exports.after = ["plugins"]; - exports.synchronous = true; +// Export name and synchronous status +exports.name = "load-defer"; +exports.platforms = ["node"]; +exports.after = ["plugins"]; +exports.synchronous = true; - var parsers = {}; - - $tw.deserializerParsers = parsers; - - exports.startup = function(callback) { - // First, exec all tiddlerloaders - $tw.modules.forEachModuleOfType("tiddlerLoader",function(title,module) { - for(var f in module) { - if($tw.utils.hop(module,f)) { - parsers[f] = module[f]; // Store the parser class - } - } - }); +var parsers = {}; - var specs = $tw.deferredDirSpecs; - $tw.utils.each(specs, function(spec){ +$tw.deserializerParsers = parsers; - var path = spec.filepath; - var tiddlers = $tw.loadTiddlersFromSpecification(path, undefined, true) - $tw.utils.each(tiddlers,function(tiddlerFile) { - $tw.utils.each(tiddlerFile.tiddlers,function(tiddler) { - relativePath = ospath.relative($tw.boot.wikiTiddlersPath,tiddlerFile.filepath); - // Keep track of our file tiddlers, so add them to boot.files - $tw.boot.files[tiddler.title] = { - filepath: tiddlerFile.filepath, - type: tiddlerFile.type, - hasMetaFile: tiddlerFile.hasMetaFile, - isEditableFile: tiddlerFile.isEditableFile || tiddlerFile.filepath.indexOf($tw.boot.wikiTiddlersPath) !== 0, - originalpath: relativePath - }; - }); - $tw.wiki.addTiddlers(tiddlerFile.tiddlers); - }); - }); - }; - - })(); - \ No newline at end of file +exports.startup = function(callback) { + var path = require("path"); + // First, exec all tiddlerloaders + $tw.modules.forEachModuleOfType("tiddlerLoader",function(title,module) { + for(var f in module) { + if($tw.utils.hop(module,f)) { + parsers[f] = module[f]; // Store the parser class + } + } + }); + + var specs = $tw.deferredDirSpecs; + $tw.utils.each(specs, function(spec){ + + var fpath = spec.filepath; + var tiddlers = $tw.loadTiddlersFromSpecification(fpath, undefined, true) + $tw.utils.each(tiddlers,function(tiddlerFile) { + $tw.utils.each(tiddlerFile.tiddlers,function(tiddler) { + relativePath = path.relative($tw.boot.wikiTiddlersPath,tiddlerFile.filepath); + // Keep track of our file tiddlers, so add them to boot.files + $tw.boot.files[tiddler.title] = { + filepath: tiddlerFile.filepath, + type: tiddlerFile.type, + hasMetaFile: tiddlerFile.hasMetaFile, + isEditableFile: tiddlerFile.isEditableFile || tiddlerFile.filepath.indexOf($tw.boot.wikiTiddlersPath) !== 0, + originalpath: relativePath + }; + }); + $tw.wiki.addTiddlers(tiddlerFile.tiddlers); + }); + }); +}; diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index 33a7f6231..d35f96174 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -459,7 +459,9 @@ exports.saveTiddlerToFile = function(tiddler,fileInfo,callback) { }); } else { // If we have a deferred filetype, call parser first and then save. - var content, fileOptions = "utf8", filepath = fileInfo.filepath; + var content, + fileOptions = "utf8", + filepath = fileInfo.filepath; if(fileInfo.deferredFiletype) { var loader = new $tw.deserializerParsers[fileInfo.deferredFiletype](); var binContent = loader.save(fileInfo.filepath, tiddler); diff --git a/plugins/cyberfoxar/fm-md/fmloader.js b/plugins/cyberfoxar/fm-md/fmloader.js deleted file mode 100644 index 2f9d08625..000000000 --- a/plugins/cyberfoxar/fm-md/fmloader.js +++ /dev/null @@ -1,101 +0,0 @@ -/*\ -title: $:/plugins/cyberfoxar/fm-md/fmloader.js -type: application/javascript -module-type: tiddlerLoader - -Proof-of-concept plugin for a tiddlerloader. -\*/ - -(function(){ - /** - * One thing I might be able to do to 'parse' dendron: - * use `$tw.utils.parseFields` which parses plaintext fields. - * Now, I just need to find how to properly handle the fences. - * And filenames-as-tags. - * - * Fences might be as simple as: - * open file, find first line with `---` - * then read each line and stop when you find another empty line with `---`. - * Everything read should go to parseFields, then fields - * Everything else of the file should go to field.text - */ - - - /** - * When given binary data, tries to deserialize it into a tiddler. - * - * @param {string} filename - original filename, does not include path - * @param {Buffer} fileBuffer - file as read by NodeJS - * @param {Object} fileSpec - Context/Directory specification-like object - * @returns {$tw.Tiddler} - decoded Tiddler object to be added to the store - */ - function loadTiddlerFromBinary(filename, fileBuffer, fileSpec){ - var lines = fileBuffer.toString().split(/(?:\r\n|\r|\n)/g) - var fm, text - - for (let index = 0; index < lines.length; index++) { - const line = lines[index]; - console.log('read line:', line) - if (line.startsWith('---') && line.trim().length < 4) { - if (fm === undefined){ - fm = [] - // start fm - } else { - // end fm - text = lines.slice(index+1).join('\n') - break - } - } else if (fm !== undefined) { - // fm has started - fm = fm.concat(line) - } - } - fm = fm ? fm.join('\n') : "" - var myfields = $tw.utils.parseFields(fm) - - myfields.text = text - myfields.type = 'text/markdown' - myfields.deferredFiletype = 'bin/test-tiddler' - - return [myfields] - } - - /** - * When given a Tiddler, binarize it however we like and gives - * back a temporary object holding the data. - * - * @param {string} filePath - * @param {$tw.Tiddler} tiddler - tiddler to be binarized - * @returns { - * { - * filePath: string - * buffer: Buffer, - * fileOptions: {fs.WriteFileOptions | undefined} - * } - * } - */ - function makeBinaryFromTiddler(filePath, tiddler){ - // This is a very naive implementation of fences-in-markdown style. - // It works, though. - - var fm = tiddler.getFieldStringBlock({exclude: ["text","bag"]}); - var content = "---\n" + (fm) + "\n---\n" + (!!tiddler.fields.text ? tiddler.fields.text : "") - - return { - filePath: filePath, - buffer: content, - fileOptions: { - encoding: "utf8" - } - } - } - - TiddlerLoaderPlugin.prototype.load = loadTiddlerFromBinary - TiddlerLoaderPlugin.prototype.save = makeBinaryFromTiddler - - function TiddlerLoaderPlugin(){ - // console.log("TiddlerLoaderPlugin init called") - } - - exports["bin/test-tiddler"] = TiddlerLoaderPlugin -})() \ No newline at end of file diff --git a/plugins/cyberfoxar/fm-md/plugin.info b/plugins/cyberfoxar/fm-md/plugin.info deleted file mode 100644 index 58cb7528c..000000000 --- a/plugins/cyberfoxar/fm-md/plugin.info +++ /dev/null @@ -1,6 +0,0 @@ -{ - "title": "$:/plugins/cyberfoxar/fm-md", - "description": "A PoC plugin for frontmatter loading and showing off deferred loading", - "author": "CyberFoxar", - "version": "0.0.1" -} \ No newline at end of file diff --git a/plugins/cyberfoxar/frontmatter-tiddler/fmloader.js b/plugins/cyberfoxar/frontmatter-tiddler/fmloader.js new file mode 100644 index 000000000..bb87148b3 --- /dev/null +++ b/plugins/cyberfoxar/frontmatter-tiddler/fmloader.js @@ -0,0 +1,88 @@ +/*\ +title: $:/plugins/cyberfoxar/frontmatter-tiddler/fmloader.js +type: application/javascript +module-type: tiddlerLoader + +Proof-of-concept plugin for a tiddlerloader. +Tries to find a fenced frontmatter block and parse the content like a tiddler metafile. +\*/ + +(function(){ + /** + * When given binary data, tries to deserialize it into a tiddler. + * + * @param {string} filename - original filename, does not include path + * @param {Buffer} fileBuffer - file as read by NodeJS + * @param {Object} fileSpec - Context/Directory specification-like object + * @returns {Array[Object]} - decoded Tiddler fields to be added to the wiki + */ + function loadTiddlerFromBinary(filename, fileBuffer, fileSpec){ + var lines = fileBuffer.toString().split(/(?:\r\n|\r|\n)/g) + var fm, text + + for (let index = 0; index < lines.length; index++) { + const line = lines[index]; + console.log('read line:', line) + if (line.startsWith('---') && line.trim().length < 4) { + if (fm === undefined){ + fm = [] + // start fm + } else { + // end fm + text = lines.slice(index+1).join('\n') + break + } + } else if (fm !== undefined) { + // fm has started + fm = fm.concat(line) + } + } + fm = fm ? fm.join('\n') : "" + var myfields = $tw.utils.parseFields(fm) + + myfields.text = text + myfields.type = 'text/markdown' + myfields.deferredFiletype = 'bin/test-tiddler' + + return [myfields] + } + + /** + * When given a Tiddler, binarize it however we like and gives + * back a temporary object holding the data. + * + * @param {string} filePath + * @param {$tw.Tiddler} tiddler - tiddler to be binarized + * @returns { + * { + * filePath: string + * buffer: Buffer, + * fileOptions: {fs.WriteFileOptions | undefined} + * } + * } + */ + function makeBinaryFromTiddler(filePath, tiddler){ + // This is a very naive implementation of fences-in-text style. + // It works, though. + + var fm = tiddler.getFieldStringBlock({exclude: ["text","bag"]}); + var content = "---\n" + (fm) + "\n---\n" + (!!tiddler.fields.text ? tiddler.fields.text : "") + + return { + filePath: filePath, + buffer: content, + fileOptions: { + encoding: "utf8" + } + } + } + + TiddlerLoaderPlugin.prototype.load = loadTiddlerFromBinary + TiddlerLoaderPlugin.prototype.save = makeBinaryFromTiddler + + function TiddlerLoaderPlugin(){ + // console.log("TiddlerLoaderPlugin init called") + } + + exports["bin/test-tiddler"] = TiddlerLoaderPlugin +})() \ No newline at end of file diff --git a/plugins/cyberfoxar/frontmatter-tiddler/plugin.info b/plugins/cyberfoxar/frontmatter-tiddler/plugin.info new file mode 100644 index 000000000..a2760def3 --- /dev/null +++ b/plugins/cyberfoxar/frontmatter-tiddler/plugin.info @@ -0,0 +1,6 @@ +{ + "title": "$:/plugins/cyberfoxar/frontmatter-tiddler", + "description": "A PoC plugin for loading and saving a text-based tiddlers as a unified file with frontmatter meta and showing off deferred loading", + "author": "CyberFoxar", + "version": "0.0.1" +} \ No newline at end of file