table: adding link support in simple tables
[treecutter.git] / xinclude / table.py
index ab6642717fcdf4bda3004c7ac6a32aaf09a78d5f..033ac63c74d7d8775f20444789de0a8fed161a09 100755 (executable)
@@ -6,17 +6,44 @@ 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):
@@ -39,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'),