From d29d1102acaa9309b84b5ce913f7b4325d921d80 Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Tue, 22 Jan 2019 20:42:57 +0100 Subject: [PATCH] table: adding link support in simple tables The code cans for special patterns that can be urls or addresses with email. It convert thouse parts to active docbook links that in turn gets translated into real html links when the site is published. --- xinclude/table.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/xinclude/table.py b/xinclude/table.py index ab66427..033ac63 100755 --- a/xinclude/table.py +++ b/xinclude/table.py @@ -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"(?Phttps?://[^ ]+)\|(?P[\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'), -- 2.30.2