mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-04-27 21:23:14 +00:00
Code tyding-up
This commit is contained in:
parent
e345096a32
commit
387611e41a
@ -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"));
|
||||
|
@ -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 = {};
|
||||
|
||||
var parsers = {};
|
||||
$tw.deserializerParsers = parsers;
|
||||
|
||||
$tw.deserializerParsers = parsers;
|
||||
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
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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 specs = $tw.deferredDirSpecs;
|
||||
$tw.utils.each(specs, function(spec){
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
})();
|
||||
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);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
})()
|
@ -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"
|
||||
}
|
88
plugins/cyberfoxar/frontmatter-tiddler/fmloader.js
Normal file
88
plugins/cyberfoxar/frontmatter-tiddler/fmloader.js
Normal file
@ -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
|
||||
})()
|
6
plugins/cyberfoxar/frontmatter-tiddler/plugin.info
Normal file
6
plugins/cyberfoxar/frontmatter-tiddler/plugin.info
Normal file
@ -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"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user