Adding a css mark for the top menu.
[treecutter.git] / src / tree-cutter.py
index 37b893a0961529fcdb66b230f5bb9d68f0e83e31..519acc9e21e98120aed3807b2507cdab829a154d 100755 (executable)
@@ -11,6 +11,7 @@ import argparse
 import shutil
 import pygraphviz as pgv
 import glob
+import gettext
 from amara import bindery
 from amara.xslt import transform
 from Cheetah.Template import Template
@@ -105,7 +106,7 @@ class Page():
             for c in code:
                 (p, ext) = os.path.splitext(c.href)
                 if ext in valid_scripts:
-                    exe = os.path.join(os.path.abspath(dirname+c.href))
+                    exe = os.path.join(os.path.abspath(dirname)+'/'+c.href)
                     xml = subprocess.Popen([exe],stdout=subprocess.PIPE)
                     xstr = bindery.parse(str(xml.stdout.read()))
                     idp = c.xml_index_on_parent
@@ -146,14 +147,15 @@ class Page():
         os.chdir(cwd)
 
     def template(self,sitemap):
-        htmlmenu =  sitemap.gen_menu(self._lang,None,None)
+        htmlmenu =  sitemap.gen_menu(self._lang,None,"menu")
         levelmenu = sitemap.gen_menu(self._lang,self,"tree")
+        langmenu = sitemap.lang_menu(self._lang)
         template = Template(file=style_tmpl,
                             searchList=[{'title':self._title},
                                         {'menu':htmlmenu},
                                         {'article':self._rendered_article},
                                         {'levelmenu':levelmenu},
-                                        {'levelname':'Menu'}])
+                                        {'langmenu':langmenu}])
         outfile = tmptarget+'html'.join(self._file.rsplit('xml',1))
         mkdir_p(os.path.dirname(outfile))
         out = open(outfile, 'w')
@@ -290,6 +292,9 @@ class Sitemap():
     def __init__(self):
         self._file = 'sitemap.txt'
         self._tree = Trie()
+        self._sitelang = set()
+        self._isocode = bindery.parse('/usr/share/xml/iso-codes/iso_639_3.xml')
+        self._tranlang = {}
 
     def add_link(self, link):
         tokens = filter(None,re.split(r'(^/\w*/|\w*/)',link))
@@ -319,9 +324,11 @@ class Sitemap():
             link.prepare()
         t2 = time.time()
         print "Prepare  [%5.2f s]" % (round(t2-t1,2))
-        sitelang = set()
         for link in self._tree:
-            sitelang = lang.union(set(link.languages()))
+            self._sitelang = self._sitelang.union(set(link.languages()))
+        for tran in self._sitelang:
+            if tran != 'en':
+                self._tranlang[tran] = gettext.translation('iso_639_3', languages=[tran])
         t3 = time.time()
         print "Language [%5.2f s]" % (round(t3-t2,2))
         for link in self._tree:
@@ -333,12 +340,12 @@ class Sitemap():
         t5 = time.time()
         print "Template [%5.2f s]" % (round(t5-t4,2))
         sm = {}
-        for l in sitelang:
+        for l in self._sitelang:
             sm[l] = Page((l,'/sitemap'))
             sm[l].set_article(self.gen_menu(l,None,"tree sitemap"))
             sm[l].template(self)
         t6 = time.time()
-        print "Sitemap [%5.2f s]" % (round(t6-t5,2))
+        print "Sitemap  [%5.2f s]" % (round(t6-t5,2))
 
     def graph(self):
         self._tree.graph()
@@ -346,6 +353,17 @@ class Sitemap():
     def gen_menu(self,lang,page,cssclass):
         return self._tree.menu(lang,page,cssclass)
 
+    def lang_menu(self,lang):
+        html = "<ul>"
+        for l in self._sitelang:
+            isoxml = u"//iso_639_3_entry[@*='"+l+"']"
+            ln = self._isocode.xml_select(isoxml)[0].name
+            if lang != 'en':
+                ln = self._tranlang[lang].gettext(ln)
+            html += '<li><a href="%s">%s</a></li>' % ('link'+'.'+l, ln)
+        html += "</ul>"
+        return html
+
     def publish(self):
         publish(tmptarget, args.output)
         publish(args.style+"css", args.output)
@@ -542,7 +560,7 @@ sitemap.read_map()
 missing = dir_.set() - sitemap.set()
 removed = sitemap.set() - dir_.set()
 for page in removed:
-    print removed+' pages missing!!'
+    print page+' pages missing!!'
 for page in missing:
     print 'adding missing page '+page
     sitemap.add_link(page)
@@ -560,6 +578,7 @@ print "Publish  [%5.2f s]" % (round(t2-t1,2))
 
 sitemap = generateSitemap()
 tmptarget = tempfile.mkdtemp()+'/'
+tot = 0
 for page in sitemap:
     t1 = time.time()
     print "Page : %-30s %30s" % (page['link'],
@@ -569,7 +588,9 @@ for page in sitemap:
     writeToTemplate(page,pubdoc,sitemap)
     t2 = time.time()
     print "[%5.2f s]" % (round(t2-t1,2))
+    tot = tot + (t2-t1)
 
+print "Total time\t\t\t\t\t\t\t     [%5.2f s]" % (round(tot,2))
 createSitemap(sitemap)
 publish(tmptarget, args.output)
 publish(args.style+"css", args.output)