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.
import codecs
from urlparse import urlparse
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
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)
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))
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):
class Table(object):
def __init__(self, tablefile, title):
h = cols.pop(0)
row = db.row()
for e in h:
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:
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'),
tab = db.table(db.title(self.title),
db.tgroup(head,body,cols=nrcol,
colsep='1',rowsep='1',align='left'),