From 3f151ba70e96db70a7158316e0cee9ea3ce8b517 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Thu, 17 Oct 2013 16:57:07 +0100 Subject: [PATCH] Add support for macro modules Now JavaScript macros can be defined in "macro" modules --- core/modules/macros/makedatauri.js | 44 +++++++++++++++++++++ core/modules/macros/version.js | 30 ++++++++++++++ core/modules/new_widgets/widget.js | 37 ++++++++++++++++- core/modules/startup.js | 1 + editions/test/tiddlers/tests/test-widget.js | 15 +++++++ readme.md | 40 ++++++++----------- 6 files changed, 142 insertions(+), 25 deletions(-) create mode 100644 core/modules/macros/makedatauri.js create mode 100644 core/modules/macros/version.js diff --git a/core/modules/macros/makedatauri.js b/core/modules/macros/makedatauri.js new file mode 100644 index 000000000..418a95f6b --- /dev/null +++ b/core/modules/macros/makedatauri.js @@ -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 + +<> + +\*/ +(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(""); +}; + +})(); diff --git a/core/modules/macros/version.js b/core/modules/macros/version.js new file mode 100644 index 000000000..b0dd0b0d5 --- /dev/null +++ b/core/modules/macros/version.js @@ -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; +}; + +})(); diff --git a/core/modules/new_widgets/widget.js b/core/modules/new_widgets/widget.js index 3ffff98dc..984efe512 100755 --- a/core/modules/new_widgets/widget.js +++ b/core/modules/new_widgets/widget.js @@ -81,8 +81,9 @@ Widget.prototype.getVariable = function(name,actualParams,defaultValue) { while(node && !$tw.utils.hop(node.variables,name)) { node = node.parentWidget; } + // If we get to the root then look for a macro module if(!node) { - return defaultValue; + return this.evaluateMacroModule(name,actualParams,defaultValue); } // Get the 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\n
\nContent

"); }); + 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>>My linky link\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("

\n\nMy linky link

"); + }); + it("should deal with the list widget", function() { var wiki = new $tw.Wiki(); // Add some tiddlers diff --git a/readme.md b/readme.md index eeff45223..2b9b1ab5a 100644 --- a/readme.md +++ b/readme.md @@ -128,8 +128,7 @@ The following commands are available:

-LoadCommand

-
+LoadCommand

Load tiddlers from 2.x.x TiddlyWiki files ( @@ -137,22 +136,20 @@ TiddlyWiki files ( .tiddler, .tid, .json or other files

---load <filepath>
+--load <filepath>

-PasswordCommand

-
+PasswordCommand

Set a password for subsequent crypto operations

---password <password>
+--password <password>

-PrintCommand

-
+PrintCommand

The print command outputs specified information.

@@ -167,24 +164,22 @@ Print the titles of the system tiddlers in the wiki store

 --print system

print config

Print the current core configuration

---print config
+--print config

-RenderTiddlerCommand

-
+RenderTiddlerCommand

Render an individual tiddler as a specified ContentType, defaults to text/html and save it to the specified filename

---rendertiddler <title> <filename> [<type>]
+--rendertiddler <title> <filename> [<type>]

-RenderTiddlersCommand

-
+RenderTiddlersCommand

Render a set of tiddlers matching a filter to separate files of a specified ContentType (defaults to @@ -192,13 +187,12 @@ text/html) and extension (defaults to .html).

 --rendertiddlers <filter> <template> <pathname> [<type>] [<extension>]

For example:

---rendertiddlers [!is[system]] $:/core/templates/static.tiddler.html ./static text/plain
+--rendertiddlers [!is[system]] $:/core/templates/static.tiddler.html ./static text/plain

-VerboseCommand

-
+VerboseCommand

Triggers verbose output, useful for debugging

---verbose
+--verbose

-VersionCommand

-
+VersionCommand

Displays the version number of TiddlyWiki.

---version

+--version

This readme file was automatically generated by TiddlyWiki5