Added subdir argument, cleaned up arg handling
[treecutter.git] / treecutter / sitemap.py
index 688cb0cdaad079970c8e7657cf38efab1e5893f0..24b53fe5b4ab3bd84d65caca16cd1e1e07db54a6 100644 (file)
@@ -4,19 +4,24 @@ import re
 import shutil
 import gettext
 import tempfile
-from amara import bindery
+from lxml import etree
+from lxml.builder import ElementMaker
 from time import time
+from treecutter import constants as const
 from treecutter.trie import Trie
 from treecutter.link import Link
 from treecutter.tools import ssh_cmd, publish, mkdir_p
 
 class Sitemap():
     """Class keeping the internal site structure"""
-    def __init__(self):
+    def __init__(self,args):
+        self._output = args.output
+        self._style = args.style
+        self._subdir = args.subdir
         self._file = 'sitemap.txt'
         self._tree = Trie()
         self._sitelang = set()
-        self._isocode = bindery.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
+        self._isocode = etree.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
         self._tranlang = {}
         self._tmptarget = tempfile.mkdtemp()+'/'
 
@@ -50,7 +55,7 @@ class Sitemap():
 
     # Main driver in the application processing the documents
     # in the collected sitemap
-    def process(self, style):
+    def process(self):
         t1 = time()
         print "Prepareing the input"
         for link in self._tree:
@@ -66,11 +71,11 @@ class Sitemap():
         t3 = time()
         print "Language [%5.2f s]" % (round(t3-t2,2))
         for link in self._tree:
-            link.render(style)
+            link.render(self._style)
         t4 = time()
         print "Render   [%5.2f s]" % (round(t4-t3,2))
         for link in self._tree:
-            link.template(self, style, self._tmptarget)
+            link.template(self, self._style, self._tmptarget)
         t5 = time()
         print "Template [%5.2f s]" % (round(t5-t4,2))
         t6 = time()
@@ -90,7 +95,7 @@ class Sitemap():
             sitmaplink.add_page((l,'/sitemap.'+l+'.xml'))
         for l in self._sitelang:
             sitmaplink.page(l).set_article(self.gen_menu(l,None,"tree sitemap"))
-            sitmaplink.page(l).template(self,style,self._tmptarget)
+            sitmaplink.page(l).template(self,self._style,self._tmptarget)
         t7 = time()
         print "Sitemap  [%5.2f s]" % (round(t7-t6,2))
 
@@ -98,27 +103,29 @@ class Sitemap():
         self._tree.graph()
 
     def gen_menu(self,lang,page,cssclass):
-        return self._tree.menu(lang,page,cssclass)
+        return self._tree.menu(lang,page,cssclass,self._subdir)
 
     def lang_menu(self,lang,link):
-        html = "<ul>"
+        html = ElementMaker()
+        menu = html.ul()
         for l in link.languages():
             isoxml = u"//iso_639_3_entry[@*='"+l+"']"
-            ln = self._isocode.xml_select(isoxml)[0].name
+            ln = self._isocode.xpath(isoxml)[0].get('name')
             if lang != 'en':
                 ln = self._tranlang[lang].gettext(ln)
             p = link.link()
             if p[-1] == '/':
                 p = p +'index'
             p = p+'.'+l
-            html += '<li><a href="%s" hreflang="%s">%s</a></li>' % (p, l, ln)
-        html += "</ul>"
-        return html
+            li = html.li(html.a(ln.decode('utf-8'),
+                                href=self._subdir+p,hreflang=l))
+            menu.append(li)
+        return etree.tostring(menu,encoding='UTF-8',pretty_print=False)
 
-    def publish(self,output,style):
-        ssh_cmd(output,"mkdir -p")
-        publish(self._tmptarget, output)
+    def publish(self):
+        ssh_cmd(self._output,"mkdir -p")
+        publish(self._tmptarget, self._output)
         for res in ["css","images","js","fonts","favicon.ico"]:
-            if (os.path.exists(style+res)):
-                publish(style+res, output)
-        ssh_cmd(output,"chmod a+rx")
+            if (os.path.exists(self._style+res)):
+                publish(self._style+res, self._output)
+        ssh_cmd(self._output,"chmod a+rx")