instead of over and over. Typical speedup is ~4x. Max possible speedup:
[ikiwiki] / doc / plugins / write.mdwn
1 ikiwiki [[plugins]] are written in perl. Each plugin is a perl module, in
2 the `IkiWiki::Plugin` namespace. The name of the plugin is typically in
3 lowercase, such as `IkiWiki::Plugin::inline`. Ikiwiki includes a
4 `IkiWiki::Plugin::skeleton` that can be fleshed out to make a useful
5 plugin. `IkiWiki::Plugin::pagecount` is another simple example.
6
7 [[toc levels=2]]
8
9 ## Considerations
10
11 One thing to keep in mind when writing a plugin is that ikiwiki is a wiki
12 *compiler*. So plugins influence pages when they are built, not when they
13 are loaded. A plugin that inserts the current time into a page, for
14 example, will insert the build time. Also, as a compiler, ikiwiki avoids
15 rebuilding pages unless they have changed, so a plugin that prints some
16 random or changing thing on a page will generate a static page that won't
17 change until ikiwiki rebuilds the page for some other reason, like the page
18 being edited.
19
20 ## Registering plugins
21
22 All plugins should `use IkiWiki` to import the ikiwiki plugin interface.
23
24 Plugins should, when imported, call `hook()` to hook into ikiwiki's
25 processing. The function uses named parameters, and use varies depending on
26 the type of hook being registered -- see below. Note that a plugin can call
27 the function more than once to register multiple hooks. All calls to
28 `hook()` should be passed a "type" parameter, which gives the type of
29 hook, a "id" paramter, which should be a unique string for this plugin, and
30 a "call" parameter, which is a reference to a function to call for the
31 hook.
32
33 An optional "scan" parameter, if set to a true value, makes the hook be
34 called during the preliminary scan that ikiwiki makes of updated pages,
35 before begining to render pages. This parameter should be set to true if
36 the hook modifies data in `%links`. Note that doing so will make the hook
37 be run twice per page build, so avoid doing it for expensive hooks.
38
39 ## Types of hooks
40
41 In roughly the order they are called.
42
43 ### getopt
44
45         hook(type => "getopt", id => "foo", call => \&getopt);
46
47 This allows for plugins to perform their own processing of command-line
48 options and so add options to the ikiwiki command line. It's called during
49 command line processing, with @ARGV full of any options that ikiwiki was
50 not able to process on its own. The function should process any options it
51 can, removing them from @ARGV, and probably recording the configuration
52 settings in %config. It should take care not to abort if it sees
53 an option it cannot process, and should just skip over those options and
54 leave them in @ARGV.
55
56 ### checkconfig
57
58         hook(type => "checkconfig", id => "foo", call => \&checkconfig);
59
60 This is useful if the plugin needs to check for or modify ikiwiki's
61 configuration. It's called early in the startup process. The
62 function is passed no values. It's ok for the function to call
63 `error()` if something isn't configured right.
64
65 ### filter
66
67         hook(type => "filter", id => "foo", call => \&filter);
68
69 Runs on the raw source of a page, before anything else touches it, and can
70 make arbitrary changes. The function is passed named parameters `page` and
71 `content` and should return the filtered content.
72
73 ### scan
74
75         hook(type => "scan", id => "foo", call => \&scan);
76
77 This is identical to a preprocess hook (see below), except that it is
78 called in the initial pass that scans pages for data that will be used in
79 later passes. Scan hooks are the only hook that should modify 
80
81 ### preprocess
82
83 Adding a [[PreProcessorDirective]] is probably the most common use of a
84 plugin.
85
86         hook(type => "preprocess", id => "foo", call => \&preprocess);
87
88 Replace "foo" with the command name that will be used inside brackets for
89 the preprocessor directive.
90
91 Each time the directive is processed, the referenced function (`preprocess`
92 in the example above) is called, and is passed named parameters. A "page"
93 parameter gives the name of the page that embedded the preprocessor
94 directive, while a "destpage" parameter gives the name of the page the
95 content is going to (different for inlined pages). All parameters included
96 in the directive are included as named parameters as well. Whatever the
97 function returns goes onto the page in place of the directive.
98
99 Note that if the [[htmlscrubber]] is enabled, html in
100 [[PreProcessorDirective]] output is sanitised, which may limit what your
101 plugin can do. Also, the rest of the page content is not in html format at
102 preprocessor time. Text output by a preprocessor directive will be
103 linkified and passed through markdown (or whatever engine is used to htmlize
104 the page) along with the rest of the page.
105
106 ### htmlize
107
108         hook(type => "htmlize", id => "ext", call => \&htmlize);
109
110 Runs on the raw source of a page and turns it into html. The id parameter
111 specifies the filename extension that a file must have to be htmlized using
112 this plugin. This is how you can add support for new and exciting markup
113 languages to ikiwiki.
114
115 The function is passed named parameters: "page" and "content" and should
116 return the htmlized content.
117
118 ### pagetemplate
119
120         hook(type => "pagetemplate", id => "foo", call => \&pagetemplate);
121
122 [[Templates]] are filled out for many different things in ikiwiki, 
123 like generating a page, or part of a blog page, or an rss feed, or a cgi.
124 This hook allows modifying those templates. The function is passed named
125 parameters. The "page" and "destpage" parameters are the same as for a
126 preprocess hook. The "template" parameter is a `HTML::Template` object that
127 is the template that will be used to generate the page. The function can
128 manipulate that template object.
129
130 The most common thing to do is probably to call $template->param() to add
131 a new custom parameter to the template.
132
133 ### sanitize
134
135         hook(type => "sanitize", id => "foo", call => \&sanitize);
136
137 Use this to implement html sanitization or anything else that needs to
138 modify the body of a page after it has been fully converted to html.
139
140 The function is passed named parameters: "page" and "content", and 
141 should return the sanitized content.
142
143 ### format
144
145         hook(type => "format", id => "foo", call => \&format);
146
147 The difference between format and sanitize is that sanitize only acts on
148 the page body, while format can modify the entire html page including the
149 header and footer inserted by ikiwiki, the html document type, etc.
150
151 The function is passed named parameters: "page" and "content", and 
152 should return the formatted content.
153
154 ### delete
155
156         hook(type => "delete", id => "foo", call => \&delete);
157
158 Each time a page or pages is removed from the wiki, the referenced function
159 is called, and passed the names of the source files that were removed.
160
161 ### change
162
163         hook(type => "change", id => "foo", call => \&render);
164
165 Each time ikiwiki renders a change or addition (but not deletion) to the
166 wiki, the referenced function is called, and passed the names of the
167 source files that were rendered.
168
169 ### cgi
170
171         hook(type => "cgi", id => "foo", call => \&cgi);
172
173 Use this to hook into ikiwiki's cgi script. Each registered cgi hook is
174 called in turn, and passed a CGI object. The hook should examine the
175 parameters, and if it will handle this CGI request, output a page and
176 terminate the program.
177
178 ### savestate
179
180         hook(type => "savestate", id => "foo", call => \&savestate);
181
182 This hook is called wheneven ikiwiki normally saves its state, just before
183 the state is saved. The function can save other state, modify values before
184 they're saved, etc.
185
186 ## Plugin interface
187
188 To import the ikiwiki plugin interface:
189
190         use IkiWiki '1.00';
191
192 This will import several variables and functions into your plugin's
193 namespace. These variables and functions are the ones most plugins need,
194 and a special effort will be made to avoid changing them in incompatible
195 ways, and to document any changes that have to be made in the future.
196
197 Note that IkiWiki also provides other variables functions that are not
198 exported by default. No guarantee is made about these in the future, so if
199 it's not exported, the wise choice is to not use it.
200
201 ### %config
202
203 A plugin can access the wiki's configuration via the `%config`
204 hash. The best way to understand the contents of the hash is to look at
205 [[ikiwiki.setup]], which sets the hash content to configure the wiki.
206
207 ### Other variables
208
209 If your plugin needs to access data about other pages in the wiki. It can
210 use the following hashes, using a page name as the key:
211
212 * `%links` lists the names of each page that a page links to, in an array
213   reference.
214 * `%renderedfiles` lists names of the files rendered by a page, in an array
215   reference.
216 * `%pagesources` contains the name of the source file for a page.
217
218 Also, the %IkiWiki::version variable contains the version number for the
219 ikiwiki program.
220
221 ### Library functions
222
223 #### `hook(@)`
224
225 Hook into ikiwiki's processing. See the discussion of hooks above.
226
227 Note that in addition to the named parameters described above, a parameter
228 named no_override is supported, If it's set to a true value, then this hook
229 will not override any existing hook with the same id. This is useful if
230 the id can be controled by the user.
231
232 #### `debug($)`
233
234 Logs a debugging message. These are supressed unless verbose mode is turned
235 on.
236
237 #### `error($)`
238
239 Aborts with an error message.
240
241 Note that while any plugin can use this for a fatal error, plugins should
242 try to avoid dying on bad input, as that will halt the entire wiki build
243 and make the wiki unusable. So for example, if a [[PreProcessorDirective]]
244 is passed bad parameters, it's better to return an error message, which can
245 appear on the wiki page, rather than calling error().
246
247 #### `template($;@)`
248
249 Creates and returns a HTML::Template object. The first parameter is the
250 name of the file in the template directory. The optional remaining
251 parameters are passed to HTML::Template->new.
252
253 #### `htmlpage($)`
254
255 Passed a page name, returns the base name that will be used for a the html
256 page created from it. (Ie, it appends ".html".)
257
258 #### `add_depends($$)`
259
260 Makes the specified page depend on the specified [[PageSpec]].
261
262 #### `pagespec_match($$)`
263
264 Passed a page name, and a [[PageSpec]], returns true if the [[PageSpec]]
265 matches the page.
266
267 #### `bestlink($$)`
268
269 Given a page and the text of a link on the page, determine which
270 existing page that link best points to. Prefers pages under a
271 subdirectory with the same name as the source page, failing that
272 goes down the directory tree to the base looking for matching
273 pages, as described in [[SubPage/LinkingRules]].
274
275 #### `htmllink($$$;$$$)`
276
277 Many plugins need to generate html links and add them to a page. This is
278 done by using the `htmllink` function. The usual way to call
279 `htmlllink` is:
280
281         htmllink($page, $page, $link)
282
283 Why is `$page` repeated? Because if a page is inlined inside another, and a
284 link is placed on it, the right way to make that link is actually:
285
286         htmllink($page, $destpage, $link)
287
288 Here `$destpage` is the inlining page. A `destpage` parameter is passed to
289 some of the hook functions above; the ones that are not passed it are not used
290 during inlining and don't need to worry about this issue.
291
292 The remaining three optional parameters to `htmllink` are:
293
294 1. noimageinline - set to true to avoid turning links into inline html images
295 1. forcesubpage  - set to force a link to a subpage
296 1. linktext - set to force the link text to something
297
298 #### `readfile($;$)`
299
300 Given a filename, reads and returns the entire file.
301
302 The optional second parameter, if set to a true value, makes the file be read
303 in binary mode.
304
305 A failure to read the file will result in it dying with an error.
306
307 #### `writefile($$$;$)`
308
309 Given a filename, a directory to put it in, and the file's content,
310 writes a file. 
311
312 The optional second parameter, if set to a true value, makes the file be
313 written in binary mode.
314
315 A failure to write the file will result in it dying with an error.
316
317 If the destination directory doesn't exist, it will first be created.
318
319 ### `will_render($$)`
320
321 Given a page name and a destination file name (not including the base
322 destination directory), register that the page will result in that file
323 being rendered. It's important to call this before writing to any file in
324 the destination directory.
325
326 #### `pagetype($)`
327
328 Given the name of a source file, returns the type of page it is, if it's
329 a type that ikiwiki knowns how to htmlize. Otherwise, returns undef.
330
331 #### `pagename($)`
332
333 Given the name of a source file, returns the name of the wiki page
334 that corresponds to that file.
335
336 #### `srcfile($)`
337
338 Given the name of a source file in the wiki, searches for the file in
339 the source directory and the underlay directory, and returns the full
340 path to the first file found.
341
342 #### `displaytime($)`
343
344 Given a time, formats it for display.
345
346 ## RCS plugins
347
348 ikiwiki's support for revision control systems also uses pluggable perl
349 modules. These are in the `IkiWiki::RCS` namespace, for example
350 `IkiWiki::RCS::svn`. 
351
352 Each RCS plugin must support all the IkiWiki::rcs\_* functions.
353 See IkiWiki::RCS::Stub for the full list of functions. It's ok if
354 rcs\_getctime does nothing except for throwing an error.
355
356 See [[about_RCS_backends]] for some more info.