#format python """ MoinMoin wiki - dumb parser for tables. Hopefully this format is easier to tell which column you are editing than with the '||' format. However if you want to change the name of your columns later on, you have to change the name for every row in the table, and that is kind of a pain so FYI. Installation: Copy this file into your data/plugin/parser directory and restart your MoinMoin. Use: You have a bunch of entries. Each entry represents a row in the table. Each line represent a cell in the row. The column name comes first, then a ':' thingy, then the data for the cell. Example 1: {{{#!DumbTable racer name: bob time: 3m50s home state: ok racer name: lucy time: 3m48s home state: ca racer name: jiu time: 3m52s home state: ri }}} result is a table like this: racer name time home state bob 3m50s ok lucy 3m48s ca jiu 3m52s ri Example 2: {{{#!DumbTable tree name: white oak scientific name: querqus alba height: 80-100 feet seeds: acorns bark color: gray tree name: ponderosa pine scientific name: pinus ponderosa height: 80 feet bark color: orange/black seeds: pine cones tree name: red mulberry scientific name: morus rubra height: 50 feet bark color: brown seeds: fruity mess }}} result is big, try it yourself. Peace Out """ class Parser: def __init__(self, raw, request, **kw): """ save incoming data """ self.raw = raw self.request = request self.kw = kw def format(self, formatter): """ write html to the request object. use the formatter.table functions and write it out to html. if each column doesnt have the proper amount of rows due to typos and whatnot, it will fill that cell with an error message""" lines = self.raw.replace('\r','').split('\n') # build table in memory first matrix = {} # each dictionary entry is a column in the table. # the dictionary keys are the names of the columns. columnsinorder = [] # keeps names of columns in their original order for line in lines: if line.strip()!='': columnname = line.split(":")[0].strip() value = line.split(":")[1].strip() if matrix.has_key(columnname): matrix[columnname]+=[value] else: matrix[columnname]=[value] columnsinorder+=[columnname] # put table into html self.request.write(formatter.table(1)) # write names of columns self.request.write(formatter.table_row(1)) for columnname in columnsinorder: self.request.write(formatter.table_cell(1)) self.request.write(formatter.text(columnname)) self.request.write(formatter.table_cell(0)) # write rest of data # assume number of rows = number of rows in column 1 for rownumber in range(len(matrix.items()[0][1])): self.request.write(formatter.table_row(1)) for columnname in columnsinorder: self.request.write(formatter.table_cell(1)) try: cellvalue=matrix[columnname][rownumber] except IndexError: cellvalue='error-missing a row. check yr typing' self.request.write(formatter.text(cellvalue)) self.request.write(formatter.table_cell(0)) self.request.write(formatter.table_row(0)) self.request.write(formatter.table(0)) if __name__=='__main__': # this is used to test this parser # fake the request object import StringIO class FakeReq(StringIO.StringIO): getText,user,cfg,current_lang,content_lang='','','','','' fakereq=FakeReq() # get a formatter object import MoinMoin.formatter.text_html testformatter = MoinMoin.formatter.text_html.Formatter(fakereq) # fake some tables # table 1 - a bunch of trees testtable1=' \ tree name: white oak \n \ scientific name: querqus alba \n\ height: 80-100 feet \n\ seeds: acorns \n\ bark color: gray \n\ \n\ tree name: ponderosa pine \n\ scientific name: pinus ponderosa \n\ height: 80 feet \n\ bark color: orange/black \n\ seeds: pine cones \n\ \n\ tree name: red mulberry \n\ scientific name: morus rubra \n\ height: 50 feet \n\ bark color: brown \n\ seeds: fruity mess \n\ ' # table 2 - deliberately screw up.. someone accidentally typed # 'd' instead of 'c' into their column name. testtable2='a: 1\r\n b:2\r\n c:3\r\n ' + \ 'a: 5\r\n b:4\r\n d:6\r\n ' testtables = [testtable1]+[testtable2] for table in testtables: # try out the parser test = Parser(table,fakereq) test.format(testformatter) # print out result print test.request.getvalue() fakereq.truncate(0)