1 """
   2    MoinMoin - Key-value pair parser
   3  
   4    Copyright: 2006 by Matt Cooper <macooper@vt.edu>
   5    License: GNU GPL
   6    Version: 1.0
   7    
   8    Usage:
   9    #FORMAT keyval
  10    #FORMAT keyval delim='delim_char' (defaults to ':')
  11 """
  12 
  13 from MoinMoin.parser import wiki
  14 from MoinMoin import wikiutil
  15 
  16 class Parser(wiki.Parser):
  17     """
  18         Key-value pairs parser
  19         "Key" is anything before the delimiter,
  20         "Value" is everything after (including more delimiters)
  21         If a delimiter isn't found, the line is not turned into a key-value pair
  22     """
  23     
  24     parsername = "KeyValueParser"
  25     
  26     def __init__(self, raw, request, **kw):
  27         wiki.Parser.__init__(self, raw, request)
  28         self.request = request
  29         self.form = request.form
  30         self.raw = raw
  31         self._ = request.getText
  32         self.args = kw.get('format_args', '')
  33     
  34         attrs, msg = wikiutil.parseAttributes(request, self.args)
  35         self.delim = attrs.get('delim', '')[1:-1]
  36 
  37 
  38     def format(self, formatter):
  39         """
  40             Look through the text line-by-line for the delimiter character.
  41             When it's found, turn that line into a table.  Otherwise,
  42             leave the line as-is.  When that's all done, we let the wiki
  43             formatter do the hard work.
  44         """
  45         linesep   = "\n"
  46         delimiter = self.delim or ":"
  47         cellsep   = "||"
  48         
  49         # split the raw input into lines
  50         lines = self.raw.split("\n")
  51         
  52         # for each line, see if there's a delimiter
  53         # if there is, split the line; if not, just keep it as-is
  54         result = []
  55         while lines:
  56             line = lines.pop(0)
  57             if line.find(delimiter) > -1:
  58 
  59                 (k,v) = line.split(delimiter, 1)
  60                 result.append("%s %s %s %s %s" % \
  61                            (cellsep, k, cellsep, v, cellsep))
  62                 
  63             # if there is no delimiter...
  64             else:
  65                 result.append(line)
  66         
  67         # now send to the wiki parser
  68         self.raw = linesep.join(result)
  69         wiki.Parser.format(self, formatter)

MoinMoin: parser/keyval.py (last edited 2007-10-29 19:08:08 by localhost)