Moving the creation of tmp directory to before the new Class structure.
[treecutter.git] / src / tree-cutter.py
index 0be43854431e635270704f59891bf8629db3ca9a..e1e80ee9157673fb87378c1158f9808616fa7fba 100755 (executable)
@@ -26,6 +26,8 @@ style_xslt = args.style+"docbook.xsl"
 style_tmpl = args.style+"index.en.html.tmpl"
 outputdir = args.output
 
+tmptarget = tempfile.mkdtemp()+'/'
+
 valid_scripts = ['.py','.pl']
 MAXLEVEL = 10000
 
@@ -81,6 +83,75 @@ class Page():
         self._menu = None
         self._rendered_article = None
 
+    def prepare(self):
+        self._doc = bindery.parse(self._file, prefixes=PREFIXES)
+        if self._doc.xml_select(u'/db:article/db:info/db:title'):
+            self._title = unicode(self._doc.article.info.title)
+        if self._doc.xml_select(u'/db:article/db:info/db:titleabbrev'):
+            self._menu = unicode(self._doc.article.info.titleabbrev)
+
+        dirname = os.path.dirname(self._file)
+        code  = self._doc.xml_select(u"//xi:include[@parse='text']")
+        if code:
+            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))
+                    xml = subprocess.Popen([exe],stdout=subprocess.PIPE)
+                    xstr = bindery.parse(str(xml.stdout.read()))
+                    idp = c.xml_index_on_parent
+                    for x in xstr.xml_children:
+                        c.xml_parent.xml_insert(idp,x)
+                        c.xml_parent.xml_remove(c)
+
+        for r in self._doc.xml_select(u"//db:link[@xl:href]"):
+            rf = os.path.join(dirname,r.href)
+            if os.path.isfile(rf):
+                self._resources.append(rf)
+        for i in self._doc.xml_select(u"//db:imagedata[@fileref]"):
+            im = os.path.join(dirname,i.fileref)
+            if os.path.isfile(im):
+                self._resources.append(im)
+
+    def render(self):
+        #  amara can not handle the docbook stylesheets
+        #  xmlarticle = transform(doc,style_xslt)
+        cwd = os.getcwd()
+        dirname = os.path.dirname(self._file)
+        os.chdir(dirname)
+        infile  = os.path.basename(tempfile.mktemp())
+        outfile = tempfile.mktemp()
+        tfi = open(infile,'w')
+        tfi.write(self._doc.xml_encode())
+        tfi.close()
+#  cmd = ["saxon-xslt-xinclude","-o",outfile,infile,style_xslt]
+        cmd = ["xsltproc","--xinclude","--output",outfile,style_xslt,infile]
+        retcode = subprocess.call(cmd)
+        if retcode:
+            print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
+        tfo = open(outfile,'r')
+        self._rendered_article = tfo.read()
+        tfo.close()
+        os.remove(infile)
+        os.remove(outfile)
+        os.chdir(cwd)
+
+    def template(self,sitemap):
+        htmlmenu =  sitemap.gen_menu(self._lang,None)
+        levelmenu = sitemap.gen_menu(self._lang,self)
+        template = Template(file=style_tmpl,
+                            searchList=[{'title':self._title},
+                                        {'menu':htmlmenu},
+                                        {'article':self._rendered_article},
+                                        {'levelmenu':levelmenu},
+                                        {'levelname':levelname}])
+        outfile = tmptarget+self._file+'.'+self._lang+'.html'
+        mkdir_p(os.path.dirname(outfile))
+        out = open(outfile, 'w')
+        out.write(str(template))
+        out.close()
+
+
 class Link():
     """Class representing a webpage on the site"""
     def __init__(self,link):
@@ -124,6 +195,15 @@ class Trie():
     def __init__(self):
         self._root = []
 
+    def __iter__(self):
+        return self.inorder(self._root)
+
+    def inorder(self,t):
+        for l in t:
+            yield l.value()
+            for ch in l.children():
+                self.inorder(ch)
+
     def _add(self,trie, key, content):
         # is the key a leaf
         k = key.pop(0)
@@ -153,7 +233,7 @@ class Trie():
         self._graph(self._root, G)
 #        G.layout('dot')
 #        G.draw('g.png')
-        print G.string()
+#        print G.string()
 
 class Sitemap():
     """Class keeping the internal site structure"""
@@ -181,6 +261,9 @@ class Sitemap():
     def graph(self):
         self._tree.graph()
 
+    def gen_menu(self,lang,page):
+        return 'Generate menu from sitemap - To be implemented'
+
 def generateSitemap():
     sitemap = []
     try: