mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-11-10 12:33:00 +00:00
152
boot/boot.js
152
boot/boot.js
@@ -1456,17 +1456,26 @@ $tw.loadTiddlersFromFile = function(filepath,fields) {
|
||||
typeInfo = type ? $tw.config.contentTypeInfo[type] : null,
|
||||
data = fs.readFileSync(filepath,typeInfo ? typeInfo.encoding : "utf8"),
|
||||
tiddlers = $tw.wiki.deserializeTiddlers(ext,data,fields),
|
||||
metafile = filepath + ".meta",
|
||||
metadata;
|
||||
if(ext !== ".json" && tiddlers.length === 1 && fs.existsSync(metafile)) {
|
||||
metadata = fs.readFileSync(metafile,"utf8");
|
||||
if(metadata) {
|
||||
tiddlers = [$tw.utils.parseFields(metadata,tiddlers[0])];
|
||||
}
|
||||
if(ext !== ".json" && tiddlers.length === 1) {
|
||||
metadata = $tw.loadMetadataForFile(filepath);
|
||||
tiddlers = [$tw.utils.extend({},metadata,tiddlers[0])];
|
||||
}
|
||||
return {filepath: filepath, type: type, tiddlers: tiddlers, hasMetaFile: !!metadata};
|
||||
};
|
||||
|
||||
/*
|
||||
Load the metadata fields in the .meta file corresponding to a particular file
|
||||
*/
|
||||
$tw.loadMetadataForFile = function(filepath) {
|
||||
var metafilename = filepath + ".meta";
|
||||
if(fs.existsSync(metafilename)) {
|
||||
return $tw.utils.parseFields(fs.readFileSync(metafilename,"utf8") || "");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
A default set of files for TiddlyWiki to ignore during load.
|
||||
This matches what NPM ignores, and adds "*.meta" to ignore tiddler
|
||||
@@ -1486,44 +1495,7 @@ $tw.loadTiddlersFromPath = function(filepath,excludeRegExp) {
|
||||
var files = fs.readdirSync(filepath);
|
||||
// Look for a tiddlywiki.files file
|
||||
if(files.indexOf("tiddlywiki.files") !== -1) {
|
||||
// If so, process the files it describes
|
||||
var filesInfo = JSON.parse(fs.readFileSync(filepath + path.sep + "tiddlywiki.files","utf8"));
|
||||
// First the tiddlers
|
||||
$tw.utils.each(filesInfo.tiddlers,function(tidInfo) {
|
||||
var type = tidInfo.fields.type || "text/plain",
|
||||
typeInfo = $tw.config.contentTypeInfo[type],
|
||||
pathname = path.resolve(filepath,tidInfo.file),
|
||||
text = fs.readFileSync(pathname,typeInfo ? typeInfo.encoding : "utf8");
|
||||
if(tidInfo.isTiddlerFile) {
|
||||
var fileTiddlers = $tw.wiki.deserializeTiddlers(path.extname(pathname),text) || [];
|
||||
$tw.utils.each(fileTiddlers,function(tiddler) {
|
||||
$tw.utils.extend(tiddler,tidInfo.fields);
|
||||
if(tidInfo.prefix) {
|
||||
tiddler.text = tidInfo.prefix + tiddler.text;
|
||||
}
|
||||
if(tidInfo.suffix) {
|
||||
tiddler.text = tiddler.text + tidInfo.suffix;
|
||||
}
|
||||
});
|
||||
tiddlers.push({tiddlers: fileTiddlers});
|
||||
} else {
|
||||
if(tidInfo.prefix) {
|
||||
text = tidInfo.prefix + text;
|
||||
}
|
||||
if(tidInfo.suffix) {
|
||||
text = text + tidInfo.suffix;
|
||||
}
|
||||
tidInfo.fields.text = text;
|
||||
tiddlers.push({tiddlers: [tidInfo.fields]});
|
||||
}
|
||||
});
|
||||
// Then any recursive directories
|
||||
$tw.utils.each(filesInfo.directories,function(dirPath) {
|
||||
var pathname = path.resolve(filepath,dirPath);
|
||||
if(fs.existsSync(pathname) && fs.statSync(pathname).isDirectory()) {
|
||||
tiddlers.push.apply(tiddlers,$tw.loadTiddlersFromPath(pathname,excludeRegExp));
|
||||
}
|
||||
});
|
||||
Array.prototype.push.apply(tiddlers,$tw.loadTiddlersFromSpecification(filepath));
|
||||
} else {
|
||||
// If not, read all the files in the directory
|
||||
$tw.utils.each(files,function(file) {
|
||||
@@ -1539,6 +1511,98 @@ $tw.loadTiddlersFromPath = function(filepath,excludeRegExp) {
|
||||
return tiddlers;
|
||||
};
|
||||
|
||||
/*
|
||||
Load all the tiddlers defined by a `tiddlywiki.files` specification file
|
||||
filepath: pathname of the directory containing the specification file
|
||||
*/
|
||||
$tw.loadTiddlersFromSpecification = function(filepath) {
|
||||
var tiddlers = [];
|
||||
// Read the specification
|
||||
var filesInfo = JSON.parse(fs.readFileSync(filepath + path.sep + "tiddlywiki.files","utf8"));
|
||||
// Helper to process a file
|
||||
var processFile = function(filename,isTiddlerFile,fields) {
|
||||
var extInfo = $tw.config.fileExtensionInfo[path.extname(filename)],
|
||||
type = (extInfo || {}).type || fields.type || "text/plain",
|
||||
typeInfo = $tw.config.contentTypeInfo[type] || {},
|
||||
pathname = path.resolve(filepath,filename),
|
||||
text = fs.readFileSync(pathname,typeInfo.encoding || "utf8"),
|
||||
metadata = $tw.loadMetadataForFile(pathname) || {},
|
||||
fileTiddlers;
|
||||
if(isTiddlerFile) {
|
||||
fileTiddlers = $tw.wiki.deserializeTiddlers(path.extname(pathname),text,metadata) || [];
|
||||
} else {
|
||||
fileTiddlers = [$tw.utils.extend({text: text},metadata)];
|
||||
}
|
||||
var combinedFields = $tw.utils.extend({},fields,metadata);
|
||||
$tw.utils.each(fileTiddlers,function(tiddler) {
|
||||
$tw.utils.each(combinedFields,function(fieldInfo,name) {
|
||||
if(typeof fieldInfo === "string" || $tw.utils.isArray(fieldInfo)) {
|
||||
tiddler[name] = fieldInfo;
|
||||
} else {
|
||||
var value = tiddler[name];
|
||||
switch(fieldInfo.source) {
|
||||
case "filename":
|
||||
value = path.basename(filename);
|
||||
break;
|
||||
case "basename":
|
||||
value = path.basename(filename,path.extname(filename));
|
||||
break;
|
||||
case "extname":
|
||||
value = path.extname(filename);
|
||||
break;
|
||||
case "created":
|
||||
value = new Date(fs.statSync(pathname).birthtime);
|
||||
break;
|
||||
case "modified":
|
||||
value = new Date(fs.statSync(pathname).mtime);
|
||||
break;
|
||||
}
|
||||
if(fieldInfo.prefix) {
|
||||
value = fieldInfo.prefix + value;
|
||||
}
|
||||
if(fieldInfo.suffix) {
|
||||
value = value + fieldInfo.suffix;
|
||||
}
|
||||
tiddler[name] = value;
|
||||
}
|
||||
});
|
||||
});
|
||||
tiddlers.push({tiddlers: fileTiddlers});
|
||||
};
|
||||
// Process the listed tiddlers
|
||||
$tw.utils.each(filesInfo.tiddlers,function(tidInfo) {
|
||||
if(tidInfo.prefix && tidInfo.suffix) {
|
||||
tidInfo.fields.text = {prefix: tidInfo.prefix,suffix: tidInfo.suffix};
|
||||
} else if(tidInfo.prefix) {
|
||||
tidInfo.fields.text = {prefix: tidInfo.prefix};
|
||||
} else if(tidInfo.suffix) {
|
||||
tidInfo.fields.text = {suffix: tidInfo.suffix};
|
||||
}
|
||||
processFile(tidInfo.file,tidInfo.isTiddlerFile,tidInfo.fields);
|
||||
});
|
||||
// Process any listed directories
|
||||
$tw.utils.each(filesInfo.directories,function(dirSpec) {
|
||||
// Read literal directories directly
|
||||
if(typeof dirSpec === "string") {
|
||||
var pathname = path.resolve(filepath,dirSpec);
|
||||
if(fs.existsSync(pathname) && fs.statSync(pathname).isDirectory()) {
|
||||
tiddlers.push.apply(tiddlers,$tw.loadTiddlersFromPath(pathname,excludeRegExp));
|
||||
}
|
||||
} else {
|
||||
// Process directory specifier
|
||||
var dirPath = path.resolve(filepath,dirSpec.path),
|
||||
files = fs.readdirSync(dirPath),
|
||||
fileRegExp = new RegExp(dirSpec.filesRegExp || "^.*$");
|
||||
for(var t=0; t<files.length; t++) {
|
||||
if(files[t] !== "tiddlywiki.files" && fileRegExp.test(files[t])) {
|
||||
processFile(dirPath + path.sep + files[t],dirSpec.isTiddlerFile,dirSpec.fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return tiddlers;
|
||||
};
|
||||
|
||||
/*
|
||||
Load the tiddlers from a plugin folder, and package them up into a proper JSON plugin tiddler
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user