xinclude: added pic and latests plugins
[treecutter.git] / xinclude / table.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import sys
5 import re
6
7 from urlparse import urlparse
8 from lxml import etree
9 from lxml.builder import ElementMaker
10 from treecutter import constants as const
11
12 def linkify(text):
13     db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
14     r = re.search(r"(?P<url>https?://[^ ]+)\|(?P<title>[\w\-\.]+)", text)
15     if r:
16         rep = r.groups(r.group(1))
17         text = db.link(rep[1],**{const.XLINK+"href": rep[0]})
18     return text
19
20 class Table(object):
21     def __init__(self, tablefile, title):
22         self.tablefile  = tablefile
23         self.title = title
24         self.cols = []
25
26     def parse(self):
27         f = file(self.tablefile, 'r')
28         for line in f:
29             c = line.split()
30             self.cols.append(c)
31
32     def db_xml(self):
33         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
34         cols = self.cols
35         nrcol = str(len(cols[0]))
36         if cols[0][0][0] == '*':
37             cols[0][0] = cols[0][0][1:]
38             h = cols.pop(0)
39             row = db.row()
40             for e in h:
41                 row.append(db.entry(linkify(e), align="center"))
42             head = db.thead(row)
43         body = db.tbody()
44         for r in cols:
45             row = db.row()
46             body.append(row)
47             for e in r:
48                 row.append(db.entry(linkify(e)))
49         tab = db.table(db.title(self.title),
50                        db.tgroup(head,body,cols=nrcol,
51                                  colsep='1',rowsep='1',align='left'),
52                        frame='all')
53         return tab
54
55 if __name__ == "__main__":
56     for arg in sys.argv[1:]:
57         al = arg.split("=")
58         if al[0] == "lang":
59             lang = al[1]
60         if al[0] == "xptr":
61             argument = al[1].decode('utf-8')
62
63     (tablefile, title) = argument.split('|')
64     tab = Table(tablefile,title)
65     tab.parse()
66     txml = tab.db_xml()
67
68     sys.stdout.write(etree.tostring(txml,encoding='UTF-8',pretty_print=False))