1 """
   2     MoinMoin wiki - dumb parser for tables. Hopefully this format is easier to tell 
   3     which column you are editing than with the '||' format. 
   4 
   5     However if you want to change the name of your columns later on, 
   6     you have to change the name for every row in the table, and that 
   7     is kind of a pain so FYI. 
   8 
   9 Installation:
  10 
  11 Copy this file into your data/plugin/parser directory and
  12 restart your MoinMoin. 
  13 
  14 Use:
  15 
  16 You have a bunch of entries. Each entry represents a row in the table. 
  17 Each line represent a cell in the row. The column name comes first, then a ':' thingy, 
  18 then the data for the cell. 
  19 
  20 Example 1:
  21 
  22 {{{#!DumbTable
  23 
  24 racer name: bob
  25 time: 3m50s
  26 home state: ok
  27 
  28 racer name: lucy
  29 time: 3m48s
  30 home state: ca
  31 
  32 racer name: jiu
  33 time: 3m52s
  34 home state: ri
  35 
  36 }}}
  37 
  38 result is a table like this:
  39 
  40 racer name  time    home state
  41 bob         3m50s   ok
  42 lucy        3m48s   ca
  43 jiu         3m52s   ri
  44 
  45 
  46 Example 2:
  47 
  48 {{{#!DumbTable
  49 
  50 tree name: white oak
  51 scientific name: querqus alba
  52 height: 80-100 feet
  53 seeds: acorns
  54 bark color: gray
  55 
  56 tree name: ponderosa pine
  57 scientific name: pinus ponderosa
  58 height: 80 feet
  59 bark color: orange/black
  60 seeds: pine cones
  61 
  62 tree name: red mulberry
  63 scientific name: morus rubra
  64 height: 50 feet
  65 bark color: brown
  66 seeds: fruity mess
  67 
  68 }}}
  69 
  70 
  71 result is big, try it yourself. 
  72 
  73 
  74 Peace Out
  75 
  76 """
  77 
  78 class Parser:
  79 
  80     def __init__(self, raw, request, **kw):
  81         """ save incoming data """
  82         self.raw = raw
  83         self.request = request
  84         self.kw = kw
  85 
  86     def format(self, formatter):
  87         """ write html to the request object. 
  88 
  89         use the formatter.table functions and
  90         write it out to html.
  91 
  92         if each column doesnt have the proper amount of rows
  93         due to typos and whatnot, it will fill that cell with an
  94         error message"""
  95 
  96         lines = self.raw.replace('\r','').split('\n')
  97 
  98         # build table in memory first
  99         matrix = {} # each dictionary entry is a column in the table.
 100                     # the dictionary keys are the names of the columns. 
 101         columnsinorder = [] # keeps names of columns in their original order
 102         for line in lines:
 103             if line.strip()!='':
 104                 columnname = line.split(":")[0].strip()
 105                 value = line.split(":")[1].strip()
 106                 if matrix.has_key(columnname):
 107                     matrix[columnname]+=[value]
 108                 else:
 109                     matrix[columnname]=[value]
 110                     columnsinorder+=[columnname]
 111                 
 112         # put table into html
 113         self.request.write(formatter.table(1))        
 114 
 115         # write names of columns 
 116         self.request.write(formatter.table_row(1))
 117         for columnname in columnsinorder:
 118             self.request.write(formatter.table_cell(1))
 119             self.request.write(formatter.text(columnname))
 120             self.request.write(formatter.table_cell(0))
 121 
 122         # write rest of data
 123         # assume number of rows = number of rows in column 1
 124         for rownumber in range(len(matrix.items()[0][1])):
 125             self.request.write(formatter.table_row(1))
 126             for columnname in columnsinorder:
 127                 self.request.write(formatter.table_cell(1))
 128                 try:
 129                     cellvalue=matrix[columnname][rownumber]
 130                 except IndexError:
 131                     cellvalue='error-missing a row. check yr typing'
 132                 self.request.write(formatter.text(cellvalue))
 133                 self.request.write(formatter.table_cell(0))
 134             self.request.write(formatter.table_row(0))
 135         self.request.write(formatter.table(0))
 136         
 137 if __name__=='__main__':
 138     # this is used to test this parser
 139 
 140     # fake the request object
 141     import StringIO
 142     class FakeReq(StringIO.StringIO):
 143         getText,user,cfg,current_lang,content_lang='','','','',''
 144     fakereq=FakeReq()
 145     
 146     # get a formatter object
 147     import MoinMoin.formatter.text_html
 148     testformatter = MoinMoin.formatter.text_html.Formatter(fakereq)
 149 
 150     # fake some tables 
 151 
 152     # table 1 - a bunch of trees
 153     testtable1=' \
 154     tree name: white oak \n \
 155     scientific name: querqus alba \n\
 156     height: 80-100 feet \n\
 157     seeds: acorns \n\
 158     bark color: gray \n\
 159     \n\
 160     tree name: ponderosa pine \n\
 161     scientific name: pinus ponderosa \n\
 162     height: 80 feet \n\
 163     bark color: orange/black \n\
 164     seeds: pine cones \n\
 165     \n\
 166     tree name: red mulberry \n\
 167     scientific name: morus rubra \n\
 168     height: 50 feet \n\
 169     bark color: brown \n\
 170     seeds: fruity mess   \n\
 171     '
 172 
 173     # table 2 - deliberately screw up.. someone accidentally typed
 174     # 'd' instead of 'c' into their column name. 
 175     testtable2='a: 1\r\n b:2\r\n c:3\r\n ' + \
 176                'a: 5\r\n b:4\r\n d:6\r\n '
 177 
 178     testtables = [testtable1]+[testtable2]
 179 
 180     for table in testtables:
 181         # try out the parser
 182         test = Parser(table,fakereq)
 183         test.format(testformatter)
 184         # print out result
 185         print test.request.getvalue()
 186         fakereq.truncate(0)

MoinMoin: parser/DumbTable.py (last edited 2008-10-21 03:35:30 by BradeyHonsinger)