From: Fredrik Unger Date: Wed, 6 Apr 2011 20:09:52 +0000 (+0200) Subject: Initial layout of what work the Page object has to do, based on non object version. X-Git-Tag: v1.0~21 X-Git-Url: https://source.tree.se/git?p=treecutter.git;a=commitdiff_plain;h=2c6b958c124ba5d84265f5f3623c9f6fcd1950b0 Initial layout of what work the Page object has to do, based on non object version. prepare method to collect all information from the bindery, and to splice in results from scripts on the page. render method to do the xsl stylesheet conversion of the docbook xml to html template method to create html menues and merge the text into the template. from order, the template for one page can not run before all pages have run prepare. (sitemap does not have all titles before that) Solution for language menu and how to deal with a missing page for a language has to be implemented. --- diff --git a/src/tree-cutter.py b/src/tree-cutter.py index 0be4385..0ac7d9f 100755 --- a/src/tree-cutter.py +++ b/src/tree-cutter.py @@ -81,6 +81,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(doc.article.info.title) + if self._doc.xml_select(u'/db:article/db:info/db:titleabbrev'): + self._menu = unicode(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(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):