mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-30 23:23:02 +00:00 
			
		
		
		
	Add support for macro modules
Now JavaScript macros can be defined in "macro" modules
This commit is contained in:
		
							
								
								
									
										44
									
								
								core/modules/macros/makedatauri.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								core/modules/macros/makedatauri.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | /*\ | ||||||
|  | title: $:/core/modules/macros/makedatauri.js | ||||||
|  | type: application/javascript | ||||||
|  | module-type: macro | ||||||
|  |  | ||||||
|  | Macro to convert the content of a tiddler to a data URI | ||||||
|  |  | ||||||
|  | <<makedatauri text:"Text to be converted" type:"text/vnd.tiddlywiki">> | ||||||
|  |  | ||||||
|  | \*/ | ||||||
|  | (function(){ | ||||||
|  |  | ||||||
|  | /*jslint node: true, browser: true */ | ||||||
|  | /*global $tw: false */ | ||||||
|  | "use strict"; | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | Information about this macro | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | exports.name = "makedatauri"; | ||||||
|  |  | ||||||
|  | exports.params = [ | ||||||
|  | 	{name: "text"}, | ||||||
|  | 	{name: "type"} | ||||||
|  | ]; | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | Run the macro | ||||||
|  | */ | ||||||
|  | exports.run = function(text,type) { | ||||||
|  | 	type = type || "text/vnd.tiddlywiki"; | ||||||
|  | 	var typeInfo = $tw.config.contentTypeInfo[type] || $tw.config.contentTypeInfo["text/plain"], | ||||||
|  | 		isBase64 = typeInfo.encoding === "base64", | ||||||
|  | 		parts = []; | ||||||
|  | 	parts.push("data:"); | ||||||
|  | 	parts.push(type); | ||||||
|  | 	parts.push(isBase64 ? ";base64" : ""); | ||||||
|  | 	parts.push(","); | ||||||
|  | 	parts.push(isBase64 ? text : encodeURIComponent(text)); | ||||||
|  | 	return parts.join(""); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | })(); | ||||||
							
								
								
									
										30
									
								
								core/modules/macros/version.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								core/modules/macros/version.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | /*\ | ||||||
|  | title: $:/core/modules/macros/version.js | ||||||
|  | type: application/javascript | ||||||
|  | module-type: macro | ||||||
|  |  | ||||||
|  | Macro to return the TiddlyWiki core version number | ||||||
|  |  | ||||||
|  | \*/ | ||||||
|  | (function(){ | ||||||
|  |  | ||||||
|  | /*jslint node: true, browser: true */ | ||||||
|  | /*global $tw: false */ | ||||||
|  | "use strict"; | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | Information about this macro | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | exports.name = "version"; | ||||||
|  |  | ||||||
|  | exports.params = []; | ||||||
|  |  | ||||||
|  | /* | ||||||
|  | Run the macro | ||||||
|  | */ | ||||||
|  | exports.run = function() { | ||||||
|  | 	return $tw.version; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | })(); | ||||||
| @@ -81,8 +81,9 @@ Widget.prototype.getVariable = function(name,actualParams,defaultValue) { | |||||||
| 	while(node && !$tw.utils.hop(node.variables,name)) { | 	while(node && !$tw.utils.hop(node.variables,name)) { | ||||||
| 		node = node.parentWidget; | 		node = node.parentWidget; | ||||||
| 	} | 	} | ||||||
|  | 	// If we get to the root then look for a macro module | ||||||
| 	if(!node) { | 	if(!node) { | ||||||
| 		return defaultValue; | 		return this.evaluateMacroModule(name,actualParams,defaultValue); | ||||||
| 	} | 	} | ||||||
| 	// Get the value | 	// Get the value | ||||||
| 	var value = node.variables[name].value; | 	var value = node.variables[name].value; | ||||||
| @@ -129,6 +130,40 @@ Widget.prototype.substituteVariableReferences = function(text) { | |||||||
| 	}); | 	}); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | Widget.prototype.evaluateMacroModule = function(name,actualParams,defaultValue) { | ||||||
|  | 	if($tw.utils.hop($tw.macros,name)) { | ||||||
|  | 		var macro = $tw.macros[name], | ||||||
|  | 			args = []; | ||||||
|  | 		var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call | ||||||
|  | 			paramInfo, paramValue; | ||||||
|  | 		// Step through each of the parameters in the macro definition | ||||||
|  | 		for(var p=0; p<macro.params.length; p++) { | ||||||
|  | 			// Check if we've got a macro call parameter with the same name | ||||||
|  | 			paramInfo = macro.params[p]; | ||||||
|  | 			paramValue = undefined; | ||||||
|  | 			for(var m=0; m<actualParams.length; m++) { | ||||||
|  | 				if(actualParams[m].name === paramInfo.name) { | ||||||
|  | 					paramValue = actualParams[m].value; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			// If not, use the next available anonymous macro call parameter | ||||||
|  | 			while(nextAnonParameter < actualParams.length && actualParams[nextAnonParameter].name) { | ||||||
|  | 				nextAnonParameter++; | ||||||
|  | 			} | ||||||
|  | 			if(paramValue === undefined && nextAnonParameter < actualParams.length) { | ||||||
|  | 				paramValue = actualParams[nextAnonParameter++].value; | ||||||
|  | 			} | ||||||
|  | 			// If we've still not got a value, use the default, if any | ||||||
|  | 			paramValue = paramValue || paramInfo["default"] || ""; | ||||||
|  | 			// Save the parameter | ||||||
|  | 			args.push(paramValue); | ||||||
|  | 		} | ||||||
|  | 		return macro.run.apply(null,args) | ||||||
|  | 	} else { | ||||||
|  | 		return defaultValue; | ||||||
|  | 	} | ||||||
|  | }; | ||||||
|  |  | ||||||
| /* | /* | ||||||
| Set the value of a context variable | Set the value of a context variable | ||||||
| name: name of the variable | name: name of the variable | ||||||
|   | |||||||
| @@ -28,6 +28,7 @@ exports.startup = function() { | |||||||
| 	$tw.modules.applyMethods("tiddlermethod",$tw.Tiddler.prototype); | 	$tw.modules.applyMethods("tiddlermethod",$tw.Tiddler.prototype); | ||||||
| 	$tw.modules.applyMethods("wikimethod",$tw.Wiki.prototype); | 	$tw.modules.applyMethods("wikimethod",$tw.Wiki.prototype); | ||||||
| 	$tw.modules.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerModules); | 	$tw.modules.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerModules); | ||||||
|  | 	$tw.macros = $tw.modules.getModulesByTypeAsHashmap("macro"); | ||||||
| 	// Set up the parsers | 	// Set up the parsers | ||||||
| 	$tw.wiki.initParsers(); | 	$tw.wiki.initParsers(); | ||||||
| 	// Set up the syncer object | 	// Set up the syncer object | ||||||
|   | |||||||
| @@ -282,6 +282,21 @@ describe("Widget module", function() { | |||||||
| 		expect(wrapper.innerHTML).toBe("<p>\n<div class='My something something,  or other thing'>\nContent</div></p>"); | 		expect(wrapper.innerHTML).toBe("<p>\n<div class='My something something,  or other thing'>\nContent</div></p>"); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|  | 	it("should deal with built-in macros", function() { | ||||||
|  | 		var wiki = new $tw.Wiki(); | ||||||
|  | 		// Add some tiddlers | ||||||
|  | 		wiki.addTiddlers([ | ||||||
|  | 			{title: "TiddlerOne", text: "Jolly Old World", type: "text/vnd.tiddlywiki"} | ||||||
|  | 		]); | ||||||
|  | 		// Construct the widget node | ||||||
|  | 		var text = "\\define makelink(text,type)\n<a href=<<makedatauri text:\"$text$\" type:\"$type$\">>>My linky link</a>\n\\end\n\n<$macrocall $name=\"makelink\" text={{TiddlerOne}} type={{TiddlerOne!!type}}/>"; | ||||||
|  | 		var widgetNode = createWidgetNode(parseText(text,wiki),wiki); | ||||||
|  | 		// Render the widget node to the DOM | ||||||
|  | 		var wrapper = renderWidgetNode(widgetNode); | ||||||
|  | 		// Test the rendering | ||||||
|  | 		expect(wrapper.innerHTML).toBe("<p>\n<a href='data:text/vnd.tiddlywiki,Jolly%20Old%20World'>\nMy linky link</a></p>"); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
| 	it("should deal with the list widget", function() { | 	it("should deal with the list widget", function() { | ||||||
| 		var wiki = new $tw.Wiki(); | 		var wiki = new $tw.Wiki(); | ||||||
| 		// Add some tiddlers | 		// Add some tiddlers | ||||||
|   | |||||||
							
								
								
									
										40
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								readme.md
									
									
									
									
									
								
							| @@ -128,8 +128,7 @@ The following commands are available:</p><div class='tw-list-frame'> | |||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/LoadCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/LoadCommand.html'> | ||||||
| LoadCommand</a></span></h3><div> | LoadCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| Load tiddlers from 2.x.x <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki.html'> | Load tiddlers from 2.x.x <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki.html'> | ||||||
| TiddlyWiki</a> files (<code> | TiddlyWiki</a> files (<code> | ||||||
| @@ -137,22 +136,20 @@ TiddlyWiki</a> files (<code> | |||||||
| .tiddler</code>, <code> | .tiddler</code>, <code> | ||||||
| .tid</code>, <code> | .tid</code>, <code> | ||||||
| .json</code> or other files </p><pre> | .json</code> or other files </p><pre> | ||||||
| --load <filepath></pre></div></div></div></div><div class='tw-list-element'> | --load <filepath></pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/PasswordCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/PasswordCommand.html'> | ||||||
| PasswordCommand</a></span></h3><div> | PasswordCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| Set a password for subsequent crypto operations</p><pre> | Set a password for subsequent crypto operations</p><pre> | ||||||
| --password <password></pre></div></div></div></div><div class='tw-list-element'> | --password <password></pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/PrintCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/PrintCommand.html'> | ||||||
| PrintCommand</a></span></h3><div> | PrintCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| The <code> | The <code> | ||||||
| print</code> command outputs specified information. </p><h3 class=''> | print</code> command outputs specified information. </p><h3 class=''> | ||||||
| @@ -167,24 +164,22 @@ Print the titles of the system tiddlers in the wiki store </p><pre> | |||||||
| --print system</pre><h3 class=''> | --print system</pre><h3 class=''> | ||||||
| print config</h3><p> | print config</h3><p> | ||||||
| Print the current core configuration </p><pre> | Print the current core configuration </p><pre> | ||||||
| --print config</pre></div></div></div></div><div class='tw-list-element'> | --print config</pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/RenderTiddlerCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/RenderTiddlerCommand.html'> | ||||||
| RenderTiddlerCommand</a></span></h3><div> | RenderTiddlerCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| Render an individual tiddler as a specified <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/ContentType.html'> | Render an individual tiddler as a specified <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/ContentType.html'> | ||||||
| ContentType</a>, defaults to <code> | ContentType</a>, defaults to <code> | ||||||
| text/html</code> and save it to the specified filename </p><pre> | text/html</code> and save it to the specified filename </p><pre> | ||||||
| --rendertiddler <title> <filename> [<type>]</pre></div></div></div></div><div class='tw-list-element'> | --rendertiddler <title> <filename> [<type>]</pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/RenderTiddlersCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/RenderTiddlersCommand.html'> | ||||||
| RenderTiddlersCommand</a></span></h3><div> | RenderTiddlersCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| Render a set of tiddlers matching a filter to separate files of a specified <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/ContentType.html'> | Render a set of tiddlers matching a filter to separate files of a specified <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/ContentType.html'> | ||||||
| ContentType</a> (defaults to <code> | ContentType</a> (defaults to <code> | ||||||
| @@ -192,13 +187,12 @@ text/html</code>) and extension (defaults to <code> | |||||||
| .html</code>).</p><pre> | .html</code>).</p><pre> | ||||||
| --rendertiddlers <filter> <template> <pathname> [<type>] [<extension>]</pre><p> | --rendertiddlers <filter> <template> <pathname> [<type>] [<extension>]</pre><p> | ||||||
| For example:</p><pre> | For example:</p><pre> | ||||||
| --rendertiddlers [!is[system]] $:/core/templates/static.tiddler.html ./static text/plain</pre></div></div></div></div><div class='tw-list-element'> | --rendertiddlers [!is[system]] $:/core/templates/static.tiddler.html ./static text/plain</pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/ServerCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/ServerCommand.html'> | ||||||
| ServerCommand</a></span></h3><div> | ServerCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| The server built in to <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki5.html'> | The server built in to <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki5.html'> | ||||||
| TiddlyWiki5</a> is very simple. Although compatible with <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWeb.html'> | TiddlyWiki5</a> is very simple. Although compatible with <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWeb.html'> | ||||||
| @@ -222,26 +216,24 @@ servetype</strong> - the content type with which the root tiddler should be serv | |||||||
| <strong> | <strong> | ||||||
| username</strong> - the default username for signing edits</li></ul><p> | username</strong> - the default username for signing edits</li></ul><p> | ||||||
| For example:</p><pre> | For example:</p><pre> | ||||||
| --server 8080 $:/core/tiddlywiki5.template.html text/plain text/html MyUserName</pre></div></div></div></div><div class='tw-list-element'> | --server 8080 $:/core/tiddlywiki5.template.html text/plain text/html MyUserName</pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/VerboseCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/VerboseCommand.html'> | ||||||
| VerboseCommand</a></span></h3><div> | VerboseCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| Triggers verbose output, useful for debugging </p><pre> | Triggers verbose output, useful for debugging </p><pre> | ||||||
| --verbose</pre></div></div></div></div><div class='tw-list-element'> | --verbose</pre></div></div></div><div class='tw-list-element'> | ||||||
| <div class='tw-tiddler'> | <div class='tw-tiddler'> | ||||||
| <h3 class=''> | <h3 class=''> | ||||||
| <span class='tw-view-link'> | <span class='tw-view-link'> | ||||||
| <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/VersionCommand.html'> | <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/VersionCommand.html'> | ||||||
| VersionCommand</a></span></h3><div> | VersionCommand</a></span></h3><div class='tw-transclude'> | ||||||
| <div class='tw-transclude'> |  | ||||||
| <p> | <p> | ||||||
| Displays the version number of <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki.html'> | Displays the version number of <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki.html'> | ||||||
| TiddlyWiki</a>.</p><pre> | TiddlyWiki</a>.</p><pre> | ||||||
| --version</pre></div></div></div></div></div></div></div><p> | --version</pre></div></div></div></div></div></div><p> | ||||||
| <em> | <em> | ||||||
| This readme file was automatically generated by <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki5.html'> | This readme file was automatically generated by <a class='tw-tiddlylink tw-tiddlylink-internal tw-tiddlylink-resolves' href='http://five.tiddlywiki.com/static/TiddlyWiki5.html'> | ||||||
| TiddlyWiki5</a></em> | TiddlyWiki5</a></em> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jeremy Ruston
					Jeremy Ruston