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