8 from lxml.builder import ElementMaker
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
16 """Class keeping the internal site structure"""
18 self._file = 'sitemap.txt'
20 self._sitelang = set()
21 self._isocode = etree.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
23 self._tmptarget = tempfile.mkdtemp()+'/'
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))
34 f = open(self._file,'w')
35 f.write('\n'.join(link.link() for link in self._tree))
41 sml = f.read().split()
45 except IOError, what_error:
46 print 'INFO: Could not read sitemap.txt - one will be created'
48 # Create a set of the current tree for comparison with the
51 return set(link.link() for link in self._tree)
53 # Main driver in the application processing the documents
54 # in the collected sitemap
55 def process(self, style):
57 print "Prepareing the input"
58 for link in self._tree:
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:
66 self._tranlang[tran] = gettext.translation('iso_639_3',
69 print "Language [%5.2f s]" % (round(t3-t2,2))
70 for link in self._tree:
73 print "Render [%5.2f s]" % (round(t4-t3,2))
74 for link in self._tree:
75 link.template(self, style, self._tmptarget)
77 print "Template [%5.2f s]" % (round(t5-t4,2))
80 # Collect all files used by the documents
81 for link in self._tree:
82 res = res.union(link.resources())
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)
97 print "Sitemap [%5.2f s]" % (round(t7-t6,2))
102 def gen_menu(self,lang,page,cssclass):
103 return self._tree.menu(lang,page,cssclass)
105 def lang_menu(self,lang,link):
106 html = ElementMaker()
108 for l in link.languages():
109 isoxml = u"//iso_639_3_entry[@*='"+l+"']"
110 ln = self._isocode.xpath(isoxml)[0].get('name')
112 ln = self._tranlang[lang].gettext(ln)
117 li = html.li(html.a(ln.decode('utf-8'),href=p,hreflang=l))
119 return etree.tostring(menu,encoding='UTF-8',pretty_print=False)
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")