mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-30 23:23:02 +00:00 
			
		
		
		
	Code tyding-up
This commit is contained in:
		| @@ -1968,9 +1968,12 @@ $tw.deferredDirSpecs = []; | |||||||
| /* | /* | ||||||
| Load all the tiddlers defined by a `tiddlywiki.files` specification file | Load all the tiddlers defined by a `tiddlywiki.files` specification file | ||||||
| filepath: pathname of the directory containing the 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) { | $tw.loadTiddlersFromSpecification = function(filepath,excludeRegExp,options) { | ||||||
| 	var loadDeferred = loadDeferred || false; | 	options = options || {}; | ||||||
|  | 	var loadDeferred = options.loadDeferred || false; | ||||||
| 	var tiddlers = []; | 	var tiddlers = []; | ||||||
| 	// Read the specification | 	// Read the specification | ||||||
| 	var filesInfo = $tw.utils.parseJSONSafe(fs.readFileSync(filepath + path.sep + "tiddlywiki.files","utf8")); | 	var filesInfo = $tw.utils.parseJSONSafe(fs.readFileSync(filepath + path.sep + "tiddlywiki.files","utf8")); | ||||||
|   | |||||||
| @@ -5,24 +5,22 @@ module-type: startup | |||||||
|  |  | ||||||
| Register tiddlerloader plugins and load deferred tiddlers. | Register tiddlerloader plugins and load deferred tiddlers. | ||||||
| \*/ | \*/ | ||||||
| (function(){ | /*jslint node: true, browser: true */ | ||||||
|     ospath = require("path"); | /*global $tw: false */ | ||||||
|  | "use strict"; | ||||||
|  |  | ||||||
|     /*jslint node: true, browser: true */ | // Export name and synchronous status | ||||||
|     /*global $tw: false */ | exports.name = "load-defer"; | ||||||
|     "use strict"; | exports.platforms = ["node"]; | ||||||
|  | exports.after = ["plugins"]; | ||||||
|  | exports.synchronous = true; | ||||||
|  |  | ||||||
|     // Export name and synchronous status | var parsers = {}; | ||||||
|     exports.name = "load-defer"; |  | ||||||
|     exports.platforms = ["node"]; |  | ||||||
|     exports.after = ["plugins"]; |  | ||||||
|     exports.synchronous = true; |  | ||||||
|  |  | ||||||
|     var parsers = {}; | $tw.deserializerParsers = parsers; | ||||||
|  |  | ||||||
|     $tw.deserializerParsers = parsers; | exports.startup = function(callback) { | ||||||
|      | 	var path = require("path"); | ||||||
|     exports.startup = function(callback) { |  | ||||||
| 	// First, exec all tiddlerloaders | 	// First, exec all tiddlerloaders | ||||||
| 	$tw.modules.forEachModuleOfType("tiddlerLoader",function(title,module) { | 	$tw.modules.forEachModuleOfType("tiddlerLoader",function(title,module) { | ||||||
| 		for(var f in module) { | 		for(var f in module) { | ||||||
| @@ -35,11 +33,11 @@ Register tiddlerloader plugins and load deferred tiddlers. | |||||||
| 	var specs = $tw.deferredDirSpecs; | 	var specs = $tw.deferredDirSpecs; | ||||||
| 	$tw.utils.each(specs, function(spec){ | 	$tw.utils.each(specs, function(spec){ | ||||||
|  |  | ||||||
|             var path = spec.filepath; | 		var fpath = spec.filepath; | ||||||
|             var tiddlers = $tw.loadTiddlersFromSpecification(path, undefined, true) | 		var tiddlers = $tw.loadTiddlersFromSpecification(fpath, undefined, true) | ||||||
| 		$tw.utils.each(tiddlers,function(tiddlerFile) { | 		$tw.utils.each(tiddlers,function(tiddlerFile) { | ||||||
| 			$tw.utils.each(tiddlerFile.tiddlers,function(tiddler) { | 			$tw.utils.each(tiddlerFile.tiddlers,function(tiddler) { | ||||||
|                     relativePath = ospath.relative($tw.boot.wikiTiddlersPath,tiddlerFile.filepath); | 				relativePath = path.relative($tw.boot.wikiTiddlersPath,tiddlerFile.filepath); | ||||||
| 				// Keep track of our file tiddlers, so add them to boot.files | 				// Keep track of our file tiddlers, so add them to boot.files | ||||||
| 				$tw.boot.files[tiddler.title] = { | 				$tw.boot.files[tiddler.title] = { | ||||||
| 					filepath: tiddlerFile.filepath, | 					filepath: tiddlerFile.filepath, | ||||||
| @@ -52,7 +50,4 @@ Register tiddlerloader plugins and load deferred tiddlers. | |||||||
| 			$tw.wiki.addTiddlers(tiddlerFile.tiddlers); | 			$tw.wiki.addTiddlers(tiddlerFile.tiddlers); | ||||||
| 		}); | 		}); | ||||||
| 	}); | 	}); | ||||||
|     }; | }; | ||||||
|      |  | ||||||
|     })(); |  | ||||||
|      |  | ||||||
|   | |||||||
| @@ -459,7 +459,9 @@ exports.saveTiddlerToFile = function(tiddler,fileInfo,callback) { | |||||||
| 			}); | 			}); | ||||||
| 		} else { | 		} else { | ||||||
| 			// If we have a deferred filetype, call parser first and then save. | 			// 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) { | 			if(fileInfo.deferredFiletype) { | ||||||
| 				var loader = new $tw.deserializerParsers[fileInfo.deferredFiletype](); | 				var loader = new $tw.deserializerParsers[fileInfo.deferredFiletype](); | ||||||
| 				var binContent = loader.save(fileInfo.filepath, tiddler); | 				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" | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 CyberFoxar
					CyberFoxar