- Is the Cisco MARS mission going to abort?
- First iPhone worm spreads Rick Astley wallpaper
- 10 stunning 3D buildings made with Google SketchUp
- Open source software ready for big business
- Four reasons to buy (and one reason to avoid) the Droid
In this, the penultimate installment on the wonders of TiddlyWiki, the free, open source, personal, portable wiki system, we'll look at two of the three topics promised last week, TiddlyWiki's macros and plugins.
Macros and plugins allow you to change the behavior of TiddlyWiki without having to change the source code. Both macros and plugins are JavaScript code stored in tiddlers (TiddlyWiki's basic unit of content) that are labeled with the tag "systemConfig". This allows the TiddlyWiki system to identify them as code.
TiddlyWiki includes a number of macros, such as "newTiddler" which is shown as a link in the right hand menu of the standard distribution and, as you might guess, creates a new tiddler.
There's also a "sparklines" macro that creates sparkline graphics; "tabs", which creates a tabbed presentation inside a tiddler, and "slider", which creates a button that, when clicked, slides out text.
The difference between a macro and a plugin is that plugins are executed at load time (when the TiddlyWiki file is loaded into the browser and rendered) while macros are called when individual tiddlers are opened or other events occur, such as buttons being clicked. Also, after a plugin has executed at load time, it can also provide code to be invoked by a macro, making the distinction between plugins and macros a little loose.
Here's a demonstration of a very simple plugin. It consists of a code fragment in a tiddler that is tagged with "systemConfig":
//{{{
alert("Hello world");
//}}}
At start-up the code fragment will be executed and display the message "Hello world" (yawn). This technique is frequently used for making global changes to the TiddlyWiki architecture before the user gets involved.
To create a macro we need to modify the code and add the macro's name to the global object "config.macros" and then declare a handler for that name. We now have a macro, actually a JavaScript function, which can be executed on demand. Here's what a macro looks like:
//{{{
config.macros.helloWorld = {
handler: function (place, macroName, params, wikifier, paramString, tiddler)
{
// this will run when macro is called from a tiddler
var who = params[0] || "world";
alert("Hello " + who);
}
};
//}}}
Like the plugin, in the code is the content of a tiddler and it is tagged with systemConfig.
Comment