Added subdir argument, cleaned up arg handling
authorFredrik Unger <fred@tree.se>
Fri, 6 Dec 2013 21:18:48 +0000 (22:18 +0100)
committerFredrik Unger <fred@tree.se>
Fri, 6 Dec 2013 21:18:48 +0000 (22:18 +0100)
Added the subdir argument to allow to put a treecutter site on
a deeper level than just www.example.org/ now also
www.example.org/subdir can be used.

Argument handling was changed so that sitemap gets all arguments in
its constructor as it deals the arguments at different stages.
All arguments are handed to sitemap that then distribute the needed
arguments.

treecutter/main.py
treecutter/sitemap.py
treecutter/trie.py

index 92055d20050d0af57254b0618991c3c75b629a56..6d7b7ddcecbd679f4775cebaaa55f54e7a288fce 100644 (file)
@@ -12,11 +12,13 @@ def main():
                         default=os.path.dirname(os.getcwd())+'/style/default/')
     parser.add_argument('--output', nargs='?',
                         default=os.path.dirname(os.getcwd())+'/htdocs/')
+    parser.add_argument('--subdir', nargs='?',
+                        default='')
     args = parser.parse_args()
 
     ts = time()
     dir_ = Directory()
-    sitemap = Sitemap()
+    sitemap = Sitemap(args)
 
     # Scanning current directory and subdirectory for docbook articles
     dir_.scan()
@@ -38,11 +40,11 @@ def main():
     # Generate a pygraphviz image of the site (TODO: currently not used)
     sitemap.graph()
     # Start processing the docbook articles to static html
-    sitemap.process(args.style)
+    sitemap.process()
 
     # Publish static html and style data (css, images, fonts) to destination dir
     t1 = time()
-    sitemap.publish(args.output,args.style)
+    sitemap.publish()
     t2 = time()
     print "Publish  [%5.2f s]" % (round(t2-t1,2))
     print "Total    [%5.2f s]" % (round(t2-ts,2))
index 57b978ec8d13853bd132d59203331d0312083913..24b53fe5b4ab3bd84d65caca16cd1e1e07db54a6 100644 (file)
@@ -14,7 +14,10 @@ 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()
@@ -52,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:
@@ -68,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()
@@ -92,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))
 
@@ -100,7 +103,7 @@ 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 = ElementMaker()
@@ -114,14 +117,15 @@ class Sitemap():
             if p[-1] == '/':
                 p = p +'index'
             p = p+'.'+l
-            li = html.li(html.a(ln.decode('utf-8'),href=p,hreflang=l))
+            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")
index b7a1c3d63228a126aa7a0479e803679760395b4c..ccbcada6af16f329e9a5b8bcfda9e9d763b391c7 100644 (file)
@@ -60,7 +60,7 @@ class Trie():
 #        G.draw('g.png')
 #        print G.string()
 
-    def _menu(self, trie, lang, page, css):
+    def _menu(self, trie, lang, page, css, subdir):
         html = "<ul%s>\n" % css
         for l in trie:
             sel = ''
@@ -68,18 +68,18 @@ class Trie():
             if p == page:
                 sel = ' class="selected"'
             if p != None:
-                html += '<li%s><a href="%s">%s</a>\n' \
-                    % (sel,l.value().link(),p.menu())
+                html += '<li%s><a href="%s%s">%s</a>\n' \
+                    % (sel,subdir,l.value().link(),p.menu())
             else:
-                html += '<li%s><a href="%s.en" hreflang="en">%s</a>*\n' \
-                    % (sel,l.value().link(), l.value().page('en').menu())
+                html += '<li%s><a href="%s%s.en" hreflang="en">%s</a>*\n' \
+                    % (sel,subdir,l.value().link(), l.value().page('en').menu())
             if l.children():
-                html += self._menu(l.children(), lang, page, "")
+                html += self._menu(l.children(), lang, page, "", subdir)
         html += "</ul>\n"
         return html
 
-    def menu(self,lang,page,cssclass):
+    def menu(self,lang,page,cssclass,subdir):
         css = ''
         if cssclass:
             css = ' class="'+cssclass+'"'
-        return self._menu(self._root, lang, page, css)
+        return self._menu(self._root, lang, page, css, subdir)