Adding table generation from simple textfile
authorFredrik Unger <fred@tree.se>
Thu, 22 Nov 2012 11:38:06 +0000 (12:38 +0100)
committerFredrik Unger <fred@tree.se>
Thu, 22 Nov 2012 11:38:06 +0000 (12:38 +0100)
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.

xinclude/table.py [new file with mode: 0755]

diff --git a/xinclude/table.py b/xinclude/table.py
new file mode 100755 (executable)
index 0000000..f338661
--- /dev/null
@@ -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"(?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))