--- /dev/null
+#!/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"(?P<url>https?://[^ ]+)\|(?P<title>[\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))