1 """
   2 
   3     NEEDS REVIEW - refactor into Parser page
   4 
   5 Hello World parser for MoinMoin wiki.
   6 
   7 This hopefully gives a basic idea of what a parser looks like and what
   8 the parts of it mean. 
   9 
  10 Parsers are kind of in flux. The whole moinmoin thing is being refactored
  11 a lot. But for right now, 1.3, Parsers sort of work like this:
  12 
  13 A parser gets created by a Page object, is given the text its supposed
  14 to format and an HTTPRequest object. It mucks around with the text,
  15 and writes the result to the HTTPRequest object it was given. 
  16 
  17 To install:
  18 
  19 Copy this file into data/plugins/parser in your moinmoin wiki instance.
  20 Restart the wiki if necessary.
  21 Edit a page and type in
  22 
  23 {{{#!HelloWorld 1 2 3
  24 rabbit
  25 }}}
  26 
  27 The result should be something like this inserted in your page: 
  28 
  29 hello world begin
  30 raw: rabbit
  31 kw: 1 2 3
  32 hello world end
  33 
  34 
  35 You can get more ideas by looking in the
  36 'python\Lib\site-packages\MoinMoin\parser' directory on your computer,
  37 or by looking at http://moinmoin.wikiwikiweb.de/ParserMarket
  38 at the parsers that others have written. 
  39 """
  40 
  41 
  42 #from MoinMoin import wikiutil
  43 #from MoinMoin.parser import wiki
  44 
  45 class Parser:
  46     """ Hello World parser for MoinMoin wiki """
  47     
  48     def __init__(self, raw, request, **kw):
  49         # print "init"
  50         # 'init' is called once for each !# command but it doesnt do much.
  51         # Most of the work usually happens in the 'format' method.
  52 
  53         self.raw = raw
  54         # raw is the text inbetween the {{{ and }}} thingies. 
  55         # most parsers generally save it for use in the 'format'
  56         # method. 
  57         
  58         self.request = request
  59         # request is the HTTPRequest object
  60         # parsers generally save this during '__init__' for use later
  61         # on in 'format'. They have to write to it in fact to get
  62         # any results. 
  63 
  64         self.kw=kw
  65         # kw is is a dictionary with 'arguments' to the !# command.
  66         # for example: {{{!# HelloWorld a b c }}}
  67         # would give the following value for kw:
  68         # {'format_args': 'a b c '}
  69 
  70     def format(self, formatter):
  71         # print "format"
  72         # format is also called for each !# command. its called after __init__
  73         # is called. this is where parsers do most of their work.
  74         # they write their results into the Httprequest object
  75         # which is usually stored from __init__ in self.request. 
  76         
  77         # print "formatter",dir(formatter)
  78         # formatter is a special object in MoinMoin that
  79         # is supposed to help people who write extensions to have
  80         # sort of a uniform looking thing going on.
  81         # see http://moinmoin.wikiwikiweb.de/ApplyingFormatters?highlight=%28formatter%29
  82                 
  83         # but formatter is not documented well. you have to look at
  84         # moinmoin/formatter/base.py. And if you do, you will see that half of
  85         # the methods raise a 'not implemented' error.
  86         # formatter is also being refactored alot so dont get used to it. 
  87         # if all else fails just use formatter.rawHTML which will
  88         # screw up XML output but at least it will work. 
  89         
  90         self.request.write(formatter.text("hello world begin"))
  91         self.request.write(formatter.paragraph(1))
  92         self.request.write(formatter.text('raw: ' + self.raw))
  93         self.request.write(formatter.paragraph(0))
  94         self.request.write(formatter.paragraph(1))
  95         self.request.write(formatter.text('kw:' + str(self.kw)))
  96         self.request.write(formatter.paragraph(0))
  97         self.request.write(formatter.text("hello world end"))
  98         self.request.write(formatter.paragraph(1))
  99         self.request.write(formatter.paragraph(0))
 100 
 101         # the end

MoinMoin: parser/HelloWorld.py (last edited 2007-10-29 19:13:39 by localhost)