]> sipb.mit.edu Git - ikiwiki.git/blob - doc/plugins/write.mdwn
cleanup
[ikiwiki.git] / 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 ### cgi
179
180         hook(type => "cgi", id => "foo", call => \&auth);
181
182 This hook can be used to implement a different authentication method than
183 the standard web form. When a user needs to be authenticated, each registered
184 auth hook is called in turn, and passed a CGI object and a session object. 
185
186 If the hook is able to authenticate the user, it should set the session
187 object's "name" parameter to the authenticated user's name. Note that
188 if the name is set to the name of a user who is not registered,
189 a basic registration of the user will be automatically performed.
190
191 ### savestate
192
193         hook(type => "savestate", id => "foo", call => \&savestate);
194
195 This hook is called wheneven ikiwiki normally saves its state, just before
196 the state is saved. The function can save other state, modify values before
197 they're saved, etc.
198
199 ## Plugin interface
200
201 To import the ikiwiki plugin interface:
202
203         use IkiWiki '1.00';
204
205 This will import several variables and functions into your plugin's
206 namespace. These variables and functions are the ones most plugins need,
207 and a special effort will be made to avoid changing them in incompatible
208 ways, and to document any changes that have to be made in the future.
209
210 Note that IkiWiki also provides other variables functions that are not
211 exported by default. No guarantee is made about these in the future, so if
212 it's not exported, the wise choice is to not use it.
213
214 ### %config
215
216 A plugin can access the wiki's configuration via the `%config`
217 hash. The best way to understand the contents of the hash is to look at
218 [[ikiwiki.setup]], which sets the hash content to configure the wiki.
219
220 ### Other variables
221
222 If your plugin needs to access data about other pages in the wiki. It can
223 use the following hashes, using a page name as the key:
224
225 * `%links` lists the names of each page that a page links to, in an array
226   reference.
227 * `%renderedfiles` lists names of the files rendered by a page, in an array
228   reference.
229 * `%pagesources` contains the name of the source file for a page.
230
231 Also, the %IkiWiki::version variable contains the version number for the
232 ikiwiki program.
233
234 ### Library functions
235
236 #### `hook(@)`
237
238 Hook into ikiwiki's processing. See the discussion of hooks above.
239
240 Note that in addition to the named parameters described above, a parameter
241 named no_override is supported, If it's set to a true value, then this hook
242 will not override any existing hook with the same id. This is useful if
243 the id can be controled by the user.
244
245 #### `debug($)`
246
247 Logs a debugging message. These are supressed unless verbose mode is turned
248 on.
249
250 #### `error($)`
251
252 Aborts with an error message.
253
254 Note that while any plugin can use this for a fatal error, plugins should
255 try to avoid dying on bad input, as that will halt the entire wiki build
256 and make the wiki unusable. So for example, if a [[PreProcessorDirective]]
257 is passed bad parameters, it's better to return an error message, which can
258 appear on the wiki page, rather than calling error().
259
260 #### `template($;@)`
261
262 Creates and returns a HTML::Template object. The first parameter is the
263 name of the file in the template directory. The optional remaining
264 parameters are passed to HTML::Template->new.
265
266 #### `htmlpage($)`
267
268 Passed a page name, returns the base name that will be used for a the html
269 page created from it. (Ie, it appends ".html".)
270
271 #### `add_depends($$)`
272
273 Makes the specified page depend on the specified [[PageSpec]].
274
275 #### `pagespec_match($$)`
276
277 Passed a page name, and a [[PageSpec]], returns true if the [[PageSpec]]
278 matches the page.
279
280 #### `bestlink($$)`
281
282 Given a page and the text of a link on the page, determine which
283 existing page that link best points to. Prefers pages under a
284 subdirectory with the same name as the source page, failing that
285 goes down the directory tree to the base looking for matching
286 pages, as described in [[SubPage/LinkingRules]].
287
288 #### `htmllink($$$;$$$)`
289
290 Many plugins need to generate html links and add them to a page. This is
291 done by using the `htmllink` function. The usual way to call
292 `htmlllink` is:
293
294         htmllink($page, $page, $link)
295
296 Why is `$page` repeated? Because if a page is inlined inside another, and a
297 link is placed on it, the right way to make that link is actually:
298
299         htmllink($page, $destpage, $link)
300
301 Here `$destpage` is the inlining page. A `destpage` parameter is passed to
302 some of the hook functions above; the ones that are not passed it are not used
303 during inlining and don't need to worry about this issue.
304
305 The remaining three optional parameters to `htmllink` are:
306
307 1. noimageinline - set to true to avoid turning links into inline html images
308 1. forcesubpage  - set to force a link to a subpage
309 1. linktext - set to force the link text to something
310
311 #### `readfile($;$)`
312
313 Given a filename, reads and returns the entire file.
314
315 The optional second parameter, if set to a true value, makes the file be read
316 in binary mode.
317
318 A failure to read the file will result in it dying with an error.
319
320 #### `writefile($$$;$)`
321
322 Given a filename, a directory to put it in, and the file's content,
323 writes a file. 
324
325 The optional second parameter, if set to a true value, makes the file be
326 written in binary mode.
327
328 A failure to write the file will result in it dying with an error.
329
330 If the destination directory doesn't exist, it will first be created.
331
332 ### `will_render($$)`
333
334 Given a page name and a destination file name (not including the base
335 destination directory), register that the page will result in that file
336 being rendered. It's important to call this before writing to any file in
337 the destination directory.
338
339 #### `pagetype($)`
340
341 Given the name of a source file, returns the type of page it is, if it's
342 a type that ikiwiki knowns how to htmlize. Otherwise, returns undef.
343
344 #### `pagename($)`
345
346 Given the name of a source file, returns the name of the wiki page
347 that corresponds to that file.
348
349 #### `srcfile($)`
350
351 Given the name of a source file in the wiki, searches for the file in
352 the source directory and the underlay directory, and returns the full
353 path to the first file found.
354
355 #### `displaytime($)`
356
357 Given a time, formats it for display.
358
359 ## RCS plugins
360
361 ikiwiki's support for revision control systems also uses pluggable perl
362 modules. These are in the `IkiWiki::RCS` namespace, for example
363 `IkiWiki::RCS::svn`. 
364
365 Each RCS plugin must support all the IkiWiki::rcs\_* functions.
366 See IkiWiki::RCS::Stub for the full list of functions. It's ok if
367 rcs\_getctime does nothing except for throwing an error.
368
369 See [[about_RCS_backends]] for some more info.