table: adding link support in simple tables
[treecutter.git] / xinclude / table.py
index f3386617060b15fe1006a00a1472c624da79b895..033ac63c74d7d8775f20444789de0a8fed161a09 100755 (executable)
@@ -3,19 +3,47 @@
 
 import sys
 import re
+import codecs
 
 from urlparse import urlparse
+from email.utils import parseaddr
 from lxml import etree
 from lxml.builder import ElementMaker
 from treecutter import constants as const
 
+def append_text(tree, text):
+    children = tree.getchildren()
+    if children:
+        if children[-1].tail is None:
+            children[-1].tail = text
+        else:
+            children[-1].tail += text
+    else:
+        if tree.text is None:
+            tree.text = text
+        else:
+            tree.text += text
+    return tree
+
 def linkify(text):
     db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
+    ent = db.entry(align="center")
     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
+        ent.append(db.link(rep[1],**{const.XLINK+"href": rep[0]}))
+    ts = text.split(',')
+    c = 0
+    for t in ts:
+        c = c + 1
+        n = parseaddr(t)
+        if n[0] != '' and n[1] != '':
+            ent.append(db.address(db.personname(db.firstname(n[0].split(' ')[0]), db.surname(n[0].split(' ')[1])),db.email(n[1])))
+        else:
+            append_text(ent,t)
+        if c<len(ts):
+            append_text(ent,',')
+    return ent
 
 class Table(object):
     def __init__(self, tablefile, title):
@@ -24,9 +52,9 @@ class Table(object):
         self.cols = []
 
     def parse(self):
-        f = file(self.tablefile, 'r')
+        f = codecs.open(self.tablefile, encoding='utf-8')
         for line in f:
-            c = line.split()
+            c = re.split(r'\t+', line.rstrip())
             self.cols.append(c)
 
     def db_xml(self):
@@ -38,14 +66,14 @@ class Table(object):
             h = cols.pop(0)
             row = db.row()
             for e in h:
-                row.append(db.entry(linkify(e), align="center"))
+                row.append(linkify(e))
             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)))
+                row.append(linkify(e))
         tab = db.table(db.title(self.title),
                        db.tgroup(head,body,cols=nrcol,
                                  colsep='1',rowsep='1',align='left'),