sitemap: removing language menu namespace
[treecutter.git] / treecutter / sitemap.py
1 #!/usr/bin/python
2 import os
3 import re
4 import shutil
5 import gettext
6 import tempfile
7 from lxml import etree
8 from lxml.builder import ElementMaker
9 from time import time
10 from treecutter import constants as const
11 from treecutter.trie import Trie
12 from treecutter.link import Link
13 from treecutter.tools import ssh_cmd, publish, mkdir_p
14
15 class Sitemap():
16     """Class keeping the internal site structure"""
17     def __init__(self):
18         self._file = 'sitemap.txt'
19         self._tree = Trie()
20         self._sitelang = set()
21         self._isocode = etree.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
22         self._tranlang = {}
23         self._tmptarget = tempfile.mkdtemp()+'/'
24
25     # The sitemap uses a trie structure to keep track of links
26     # A link represents the path to the document and the link
27     # representing the text on the site.
28     # A link can have several pages in different languages.
29     def add_link(self, link):
30         tokens = filter(None,re.split(r'(^/[\w-]*/|[\w-]*/)',link))
31         self._tree.add(tokens,Link(link))
32
33     def write_map(self):
34         f = open(self._file,'w')
35         f.write('\n'.join(link.link() for link in self._tree))
36         f.close()
37
38     def read_map(self):
39         try:
40             f = open(self._file)
41             sml = f.read().split()
42             f.close()
43             for line in sml:
44                 self.add_link(line)
45         except IOError, what_error:
46             print 'INFO: Could not read sitemap.txt - one will be created'
47
48     # Create a set of the current tree for comparison with the
49     # directory scan
50     def set(self):
51         return set(link.link() for link in self._tree)
52
53     # Main driver in the application processing the documents
54     # in the collected sitemap
55     def process(self, style):
56         t1 = time()
57         print "Prepareing the input"
58         for link in self._tree:
59             link.prepare()
60         t2 = time()
61         print "Prepare  [%5.2f s]" % (round(t2-t1,2))
62         for link in self._tree:
63             self._sitelang = self._sitelang.union(set(link.languages()))
64         for tran in self._sitelang:
65             if tran != 'en':
66                 self._tranlang[tran] = gettext.translation('iso_639_3',
67                                                            languages=[tran])
68         t3 = time()
69         print "Language [%5.2f s]" % (round(t3-t2,2))
70         for link in self._tree:
71             link.render(style)
72         t4 = time()
73         print "Render   [%5.2f s]" % (round(t4-t3,2))
74         for link in self._tree:
75             link.template(self, style, self._tmptarget)
76         t5 = time()
77         print "Template [%5.2f s]" % (round(t5-t4,2))
78         t6 = time()
79         res = set()
80         # Collect all files used by the documents
81         for link in self._tree:
82             res = res.union(link.resources())
83         for f in res:
84             outfile = self._tmptarget+f
85             mkdir_p(os.path.dirname(outfile))
86             shutil.copyfile(f,outfile)
87         print "Resources[%5.2f s]" % (round(t6-t5,2))
88         # TODO: Improve the sitemap, it is a page that is generated from
89         #       the ground up and added a bit adhoc.
90         sitmaplink = Link('/sitemap')
91         for l in self._sitelang:
92             sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
93         for l in self._sitelang:
94             sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
95             sitmaplink.page(l).template(self,style,self._tmptarget)
96         t7 = time()
97         print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
98
99     def graph(self):
100         self._tree.graph()
101
102     def gen_menu(self,lang,page,cssclass):
103         return self._tree.menu(lang,page,cssclass)
104
105     def lang_menu(self,lang,link):
106         html = ElementMaker()
107         menu = html.ul()
108         for l in link.languages():
109             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
110             ln = self._isocode.xpath(isoxml)[0].get('name')
111             if lang != 'en':
112                 ln = self._tranlang[lang].gettext(ln)
113             p = link.link()
114             if p[-1] == '/':
115                 p = p +'index'
116             p = p+'.'+l
117             li = html.li(html.a(ln,href=p,hreflang=l))
118             menu.append(li)
119         return etree.tostring(menu,encoding='UTF-8',pretty_print=False)
120
121     def publish(self,output,style):
122         ssh_cmd(output,"mkdir -p")
123         publish(self._tmptarget, output)
124         for res in ["css","images","js","fonts","favicon.ico"]:
125             if (os.path.exists(style+res)):
126                 publish(style+res, output)
127         ssh_cmd(output,"chmod a+rx")