2 # -*- coding: utf-8 -*-
4 # pythondemo — demo Python ikiwiki plugin
6 # Copyright © martin f. krafft <madduck@madduck.net>
7 # Released under the terms of the GNU GPL version 2
9 __name__ = 'pythondemo'
10 __description__ = 'demo Python ikiwiki plugin'
12 __author__ = 'martin f. krafft <madduck@madduck.net>'
13 __copyright__ = 'Copyright © ' + __author__
16 from proxy import IkiWikiProcedureProxy
20 sys.stderr.write(__name__ + ':DEBUG:%s\n' % s)
23 proxy = IkiWikiProcedureProxy(__name__, debug_fn=None)
25 def _arglist_to_dict(args):
26 if len(args) % 2 != 0:
27 raise ValueError, 'odd number of arguments, cannot convert to dict'
28 return dict([args[i:i+2] for i in xrange(0, len(args), 2)])
30 def getopt_demo(*args):
31 # This allows for plugins to perform their own processing of command-line
32 # options and so add options to the ikiwiki command line. It's called
33 # during command line processing, with @ARGV full of any options that
34 # ikiwiki was not able to process on its own. The function should process
35 # any options it can, removing them from @ARGV, and probably recording the
36 # configuration settings in %config. It should take care not to abort if
37 # it sees an option it cannot process, and should just skip over those
38 # options and leave them in @ARGV.
41 # http://ikiwiki.info/bugs/external_plugins_cannot_access_ARGV_needed_for_getopt
42 debug("hook `getopt' called with arguments %s" % str(args))
43 raise NotImplementedError
44 #proxy.hook('getopt', getopt_demo)
46 def checkconfig_demo(*args):
47 # This is useful if the plugin needs to check for or modify ikiwiki's
48 # configuration. It's called early in the startup process. The function is
49 # passed no values. It's ok for the function to call error() if something
50 # isn't configured right.
51 # TODO: how do we tell ikiwiki about errors?
52 debug("hook `checkconfig' called with arguments %s" % str(args))
53 raise NotImplementedError
54 #proxy.hook('checkconfig', checkconfig_demo)
56 def refresh_demo(*args):
57 # This hook is called just before ikiwiki scans the wiki for changed
58 # files. It's useful for plugins that need to create or modify a source
59 # page. The function is passed no values.
60 debug("hook `refresh' called with arguments %s" % str(args))
61 proxy.hook('refresh', refresh_demo)
63 def needsbuild_demo(*args):
64 # This allows a plugin to manipulate the list of files that need to be
65 # built when the wiki is refreshed. The function is passed a reference to
66 # an array of pages that will be rebuilt, and can modify the array, either
67 # adding or removing files from it.
68 # TODO: how do we modify the array?
69 debug("hook `needsbuild' called with arguments %s" % str(args))
70 raise NotImplementedError
71 #proxy.hook('needsbuild', needsbuild_demo)
73 def filter_demo(*args):
74 # Runs on the raw source of a page, before anything else touches it, and
75 # can make arbitrary changes. The function is passed named parameters
76 # "page", "destpage", and "content". It should return the filtered
78 kwargs = _arglist_to_dict(args)
79 debug("hook `filter' called with arguments %s" % kwargs);
80 return kwargs['content']
81 proxy.hook('filter', filter_demo)
83 def preprocess_demo(*args):
84 # Each time the directive is processed, the referenced function
85 # (preprocess in the example above) is called, and is passed named
86 # parameters. A "page" parameter gives the name of the page that embedded
87 # the preprocessor directive, while a "destpage" parameter gives the name
88 # of the page the content is going to (different for inlined pages), and
89 # a "preview" parameter is set to a true value if the page is being
90 # previewed. All parameters included in the directive are included as
91 # named parameters as well. Whatever the function returns goes onto the
92 # page in place of the directive.
94 # An optional "scan" parameter, if set to a true value, makes the hook be
95 # called during the preliminary scan that ikiwiki makes of updated pages,
96 # before begining to render pages. This parameter should be set to true if
97 # the hook modifies data in %links. Note that doing so will make the hook
98 # be run twice per page build, so avoid doing it for expensive hooks. (As
99 # an optimisation, if your preprocessor hook is called in a void contets,
100 # you can assume it's being run in scan mode.)
102 # Note that if the htmlscrubber is enabled, html in PreProcessorDirective
103 # output is sanitised, which may limit what your plugin can do. Also, the
104 # rest of the page content is not in html format at preprocessor time.
105 # Text output by a preprocessor directive will be linkified and passed
106 # through markdown (or whatever engine is used to htmlize the page) along
107 # with the rest of the page.
109 # TODO: needs to be handled differently, the ID cannot be the plugin name.
110 kwargs = _arglist_to_dict(args)
111 debug("hook `preprocess' called with arguments %s" % kwargs)
112 raise NotImplementedError
113 return kwargs['content']
114 #proxy.hook('preprocess', preprocess_demo)
116 def linkify_demo(*args):
117 # This hook is called to convert WikiLinks on the page into html links.
118 # The function is passed named parameters "page", "destpage", and
119 # "content". It should return the linkified content.
121 # Plugins that implement linkify must also implement a scan hook, that
122 # scans for the links on the page and adds them to %links.
123 kwargs = _arglist_to_dict(args)
124 debug("hook `linkify' called with arguments %s" % kwargs)
125 return kwargs['content']
126 proxy.hook('linkify', linkify_demo)
128 def scan_demo(*args):
129 # This hook is called early in the process of building the wiki, and is
130 # used as a first pass scan of the page, to collect metadata about the
131 # page. It's mostly used to scan the page for WikiLinks, and add them to
134 # The function is passed named parameters "page" and "content". Its return
137 # TODO: how do we add to %links?
138 kwargs = _arglist_to_dict(args)
139 debug("hook `scan' called with arguments %s" % kwargs)
140 raise NotImplementedError
141 #proxy.hook('scan', scan_demo)
143 def htmlize_demo(*args):
144 # Runs on the raw source of a page and turns it into html. The id
145 # parameter specifies the filename extension that a file must have to be
146 # htmlized using this plugin. This is how you can add support for new and
147 # exciting markup languages to ikiwiki.
149 # The function is passed named parameters: "page" and "content" and should
150 # return the htmlized content.
151 kwargs = _arglist_to_dict(args)
152 debug("hook `htmlize' called with arguments %s" % kwargs)
153 return kwargs['content']
154 #proxy.hook('htmlize', htmlize_demo)
156 def pagetemplate_demo(*args):
157 # Templates are filled out for many different things in ikiwiki, like
158 # generating a page, or part of a blog page, or an rss feed, or a cgi.
159 # This hook allows modifying the variables available on those templates.
160 # The function is passed named parameters. The "page" and "destpage"
161 # parameters are the same as for a preprocess hook. The "template"
162 # parameter is a HTML::Template object that is the template that will be
163 # used to generate the page. The function can manipulate that template
166 # The most common thing to do is probably to call $template->param() to
167 # add a new custom parameter to the template.
168 # TODO: how do we call $template->param()
169 kwargs = _arglist_to_dict(args)
170 debug("hook `pagetemplate' called with arguments %s" % kwargs)
171 raise NotImplementedError
172 #proxy.hook('pagetemplate', pagetemplate_demo)
174 def templatefile_demo(*args):
175 # This hook allows plugins to change the template that is used for a page
176 # in the wiki. The hook is passed a "page" parameter, and should return
177 # the name of the template file to use, or undef if it doesn't want to
178 # change the default ("page.tmpl"). Template files are looked for in
179 # /usr/share/ikiwiki/templates by default.
181 kwargs = _arglist_to_dict(args)
182 debug("hook `templatefile' called with arguments %s" % kwargs)
184 proxy.hook('templatefile', templatefile_demo)
186 def sanitize_demo(*args):
187 # Use this to implement html sanitization or anything else that needs to
188 # modify the body of a page after it has been fully converted to html.
190 # The function is passed named parameters: "page" and "content", and
191 # should return the sanitized content.
192 kwargs = _arglist_to_dict(args)
193 debug("hook `sanitize' called with arguments %s" % kwargs)
194 return kwargs['content']
195 proxy.hook('sanitize', sanitize_demo)
197 def format_demo(*args):
198 # The difference between format and sanitize is that sanitize only acts on
199 # the page body, while format can modify the entire html page including
200 # the header and footer inserted by ikiwiki, the html document type, etc.
202 # The function is passed named parameters: "page" and "content", and
203 # should return the formatted content.
204 kwargs = _arglist_to_dict(args)
205 debug("hook `format' called with arguments %s" % kwargs)
206 return kwargs['content']
207 proxy.hook('format', format_demo)
209 def delete_demo(*args):
210 # Each time a page or pages is removed from the wiki, the referenced
211 # function is called, and passed the names of the source files that were
213 debug("hook `delete' called with arguments %s" % str(args))
214 proxy.hook('delete', delete_demo)
216 def change_demo(*args):
217 # Each time ikiwiki renders a change or addition (but not deletion) to the
218 # wiki, the referenced function is called, and passed the names of the
219 # source files that were rendered.
220 debug("hook `change' called with arguments %s" % str(args))
221 proxy.hook('change', change_demo)
224 # Use this to hook into ikiwiki's cgi script. Each registered cgi hook is
225 # called in turn, and passed a CGI object. The hook should examine the
226 # parameters, and if it will handle this CGI request, output a page
227 # (including the http headers) and terminate the program.
229 # Note that cgi hooks are called as early as possible, before any ikiwiki
230 # state is loaded, and with no session information.
231 debug("hook `cgi' called with arguments %s" % str(args))
232 raise NotImplementedError
233 #proxy.hook('cgi', cgi_demo)
235 def auth_demo(*args):
236 # This hook can be used to implement a different authentication method
237 # than the standard web form. When a user needs to be authenticated, each
238 # registered auth hook is called in turn, and passed a CGI object and
241 # If the hook is able to authenticate the user, it should set the session
242 # object's "name" parameter to the authenticated user's name. Note that if
243 # the name is set to the name of a user who is not registered, a basic
244 # registration of the user will be automatically performed.
246 # TODO: how do we set the session parameter?
247 debug("hook `auth' called with arguments %s" % str(args))
248 raise NotImplementedError
249 #proxy.hook('auth', auth_demo)
251 def sessioncgi_demo(*args):
252 # Unlike the cgi hook, which is run as soon as possible, the sessioncgi
253 # hook is only run once a session object is available. It is passed both
254 # a CGI object and a session object. To check if the user is in fact
255 # signed in, you can check if the session object has a "name" parameter
257 debug("hook `sessioncgi' called with arguments %s" % str(args))
258 raise NotImplementedError
259 #proxy.hook('sessioncgi', sessioncgi_demo)
261 def canedit_demo(*args):
262 # This hook can be used to implement arbitrary access methods to control
263 # when a page can be edited using the web interface (commits from revision
264 # control bypass it). When a page is edited, each registered canedit hook
265 # is called in turn, and passed the page name, a CGI object, and a session
268 # If the hook has no opinion about whether the edit can proceed, return
269 # undef, and the next plugin will be asked to decide. If edit can proceed,
270 # the hook should return "". If the edit is not allowed by this hook, the
271 # hook should return an error message for the user to see, or a function
272 # that can be run to log the user in or perform other action necessary for
273 # them to be able to edit the page.
275 # This hook should avoid directly redirecting the user to a signin page,
276 # since it's sometimes used to test to see which pages in a set of pages
279 # TODO: we cannot return undef/None, see above.
280 # TODO: how do we return a function?
281 debug("hook `canedit' called with arguments %s" % str(args))
282 raise NotImplementedError
283 #proxy.hook('canedit', canedit_demo)
285 def editcontent_demo(*args):
286 # This hook is called when a page is saved (or previewed) using the web
287 # interface. It is passed named parameters: content, page, cgi, and
288 # session. These are, respectively, the new page content as entered by the
289 # user, the page name, a CGI object, and the user's CGI::Session.
291 # It can modify the content as desired, and should return the content.
292 kwargs = _arglist_to_dict(args)
293 debug("hook `editcontent' called with arguments %s" % kwargs)
294 return kwargs['content']
295 proxy.hook('editcontent', editcontent_demo)
297 def formbuilder_setup_demo(*args):
298 # These hooks allow tapping into the parts of ikiwiki that use
299 # CGI::FormBuilder to generate web forms. These hooks are passed named
300 # parameters: cgi, session, form, and buttons. These are, respectively,
301 # the CGI object, the user's CGI::Session, a CGI::FormBuilder, and
302 # a reference to an array of names of buttons to go on the form.
304 # Each time a form is set up, the formbuilder_setup hook is called.
305 # Typically the formbuilder_setup hook will check the form's title, and if
306 # it's a form that it needs to modify, will call various methods to
307 # add/remove/change fields, tweak the validation code for the fields, etc.
308 # It will not validate or display the form.
310 # Just before a form is displayed to the user, the formbuilder hook is
311 # called. It can be used to validate the form, but should not display it.
313 # TODO: how do we modify the form?
314 kwargs = _arglist_to_dict(args)
315 debug("hook `formbuilder_setup' called with arguments %s" % kwargs)
316 raise NotImplementedError
317 return kwargs['content']
318 #proxy.hook('formbuilder_setup', formbuilder_setup_demo)
320 def formbuilder_demo(*args):
321 # These hooks allow tapping into the parts of ikiwiki that use
322 # CGI::FormBuilder to generate web forms. These hooks are passed named
323 # parameters: cgi, session, form, and buttons. These are, respectively,
324 # the CGI object, the user's CGI::Session, a CGI::FormBuilder, and
325 # a reference to an array of names of buttons to go on the form.
327 # Each time a form is set up, the formbuilder_setup hook is called.
328 # Typically the formbuilder_setup hook will check the form's title, and if
329 # it's a form that it needs to modify, will call various methods to
330 # add/remove/change fields, tweak the validation code for the fields, etc.
331 # It will not validate or display the form.
333 # Just before a form is displayed to the user, the formbuilder hook is
334 # called. It can be used to validate the form, but should not display it.
335 # TODO: how do we modify the form?
336 kwargs = _arglist_to_dict(args)
337 debug("hook `formbuilder' called with arguments %s" % kwargs)
338 raise NotImplementedError
339 return kwargs['content']
340 #proxy.hook('formbuilder', formbuilder_demo)
342 def savestate_demo(*args):
343 # This hook is called wheneven ikiwiki normally saves its state, just
344 # before the state is saved. The function can save other state, modify
345 # values before they're saved, etc.
348 debug("hook `savestate' called with arguments %s" % str(args))
349 raise NotImplementedError
350 #proxy.hook('savestate', savestate_demo)