From: Fredrik Unger Date: Thu, 22 Nov 2012 11:38:06 +0000 (+0100) Subject: Adding table generation from simple textfile X-Git-Url: https://source.tree.se/git?p=treecutter.git;a=commitdiff_plain;h=276a427181ea94be0e39e2a042e0845dce06a4eb Adding table generation from simple textfile Original idea stems from dependencies.py in a site which organized a table from a formated textfile. At the time just from the body of the table. This version can have a labeled header row in the begining and entries that are links, with or without title. Title is denoted httplink|title. For extra text more work is needed. Can only do one word titles for now. --- diff --git a/xinclude/table.py b/xinclude/table.py new file mode 100755 index 0000000..f338661 --- /dev/null +++ b/xinclude/table.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import sys +import re + +from urlparse import urlparse +from lxml import etree +from lxml.builder import ElementMaker +from treecutter import constants as const + +def linkify(text): + db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP) + r = re.search(r"(?Phttps?://[^ ]+)\|(?P[\w\-\.]+)", text) + if r: + rep = r.groups(r.group(1)) + text = db.link(rep[1],**{const.XLINK+"href": rep[0]}) + return text + +class Table(object): + def __init__(self, tablefile, title): + self.tablefile = tablefile + self.title = title + self.cols = [] + + def parse(self): + f = file(self.tablefile, 'r') + for line in f: + c = line.split() + self.cols.append(c) + + def db_xml(self): + db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP) + cols = self.cols + nrcol = str(len(cols[0])) + if cols[0][0][0] == '*': + cols[0][0] = cols[0][0][1:] + h = cols.pop(0) + row = db.row() + for e in h: + row.append(db.entry(linkify(e), align="center")) + head = db.thead(row) + body = db.tbody() + for r in cols: + row = db.row() + body.append(row) + for e in r: + row.append(db.entry(linkify(e))) + tab = db.table(db.title(self.title), + db.tgroup(head,body,cols=nrcol, + colsep='1',rowsep='1',align='left'), + frame='all') + return tab + +if __name__ == "__main__": + for arg in sys.argv[1:]: + al = arg.split("=") + if al[0] == "lang": + lang = al[1] + if al[0] == "xptr": + argument = al[1].decode('utf-8') + + (tablefile, title) = argument.split('|') + tab = Table(tablefile,title) + tab.parse() + txml = tab.db_xml() + + sys.stdout.write(etree.tostring(txml,encoding='UTF-8',pretty_print=False))