5 from amara import bindery
6 from amara.xslt import transform
7 from Cheetah.Template import Template
8 from pkg_resources import resource_filename, resource_listdir
10 import treecutter.const as const
11 from treecutter.tools import mkdir_p
14 """Class representing a version of a webpage"""
15 def __init__(self,link,page):
23 self._rendered_article = None
29 return set(self._resources)
34 def set_article(self,art):
35 self._rendered_article = art
38 self._doc = bindery.parse(self._file, prefixes=const.PREFIXES)
39 if self._doc.xml_select(u'/db:article/db:info/db:title'):
40 self._title = unicode(self._doc.article.info.title)
41 if self._doc.xml_select(u'/db:article/db:info/db:titleabbrev'):
42 self._menu = unicode(self._doc.article.info.titleabbrev)
44 dirname = os.path.dirname(self._file)
46 code = self._doc.xml_select(u"//xi:include[@parse='text']")
49 (p, ext) = os.path.splitext(c.href)
50 if ext in const.valid_scripts:
52 script = os.path.join(os.path.abspath(dirname)+'/'+c.href)
53 if os.path.isfile(script):
56 if c.href in resource_listdir('xinclude', ''):
57 script = resource_filename('xinclude', c.href)
60 print "Script "+c.href+" in "+self._file+" missing"
61 if c.xml_select(u"//xi:include[@accept-language]"):
62 alang = c.xml_attributes[None, "accept-language"]
63 exe.append("lang="+alang)
64 if c.xml_select(u"//xi:include[@xpointer]"):
65 exe.append("xptr="+c.xpointer)
66 print " executing %15s" % (c.href),
69 xml = subprocess.Popen(exe,stdout=subprocess.PIPE)
71 xmlblock = str(xml.stdout.read())
73 print " [%5.2f s] (%s)" % (round(te-ts,2),c.xpointer)
74 xstr = bindery.parse(xmlblock)
75 idp = c.xml_index_on_parent
76 for x in xstr.xml_children:
77 c.xml_parent.xml_insert(idp,x)
78 c.xml_parent.xml_remove(c)
80 for r in self._doc.xml_select(u"//db:link[@xl:href]"):
81 rf = os.path.join(dirname,r.href)
82 if os.path.isfile(rf):
83 self._resources.append(rf)
84 for i in self._doc.xml_select(u"//db:imagedata[@fileref]"):
85 im = os.path.join(dirname,i.fileref)
86 if os.path.isfile(im):
87 self._resources.append(im)
88 for i in self._doc.xml_select(u"//html:form[@action]"):
89 pyscript = re.split('\.py',i.action,1)[0]+'.py'
90 im = os.path.join(dirname,pyscript)
91 if os.path.isfile(im):
92 self._resources.append(im)
94 def render(self, style):
95 # amara can not handle the docbook stylesheets
96 # xmlarticle = transform(doc,style_xslt)
98 dirname = os.path.dirname(self._file)
100 infile = os.path.basename(tempfile.mktemp())
101 outfile = tempfile.mktemp()
102 tfi = open(infile,'w')
103 tfi.write(self._doc.xml_encode(omit_xml_declaration=True))
105 # cmd = ["saxon-xslt-xinclude","-o",outfile,infile,style_xslt]
106 cmd = ["xsltproc","--xinclude","--output",outfile,style+"docbook.xsl",infile]
107 retcode = subprocess.call(cmd)
109 print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
110 tfo = open(outfile,'r')
111 self._rendered_article = tfo.read()
117 def template(self,sitemap,style,tdir):
118 htmlmenu = sitemap.gen_menu(self._lang,None,"menu")
119 levelmenu = sitemap.gen_menu(self._lang,self,"tree")
120 langmenu = sitemap.lang_menu(self._lang,self._link)
121 template = Template(file=style+'index.'+self._lang+'.html.tmpl',
122 searchList=[{'title':self._title},
124 {'article':self._rendered_article},
125 {'levelmenu':levelmenu},
126 {'langmenu':langmenu}])
127 outfile = tdir+'html'.join(self._file.rsplit('xml',1))
128 mkdir_p(os.path.dirname(outfile))
129 out = open(outfile, 'w')
130 out.write(str(template))