Fixing regexp to treat /xx special.
[treecutter.git] / src / tree-cutter.py
index a4c24f9ce91baea8eb0ef31076b0d6dfb063b203..79bd372aee24cbfa0e54495d56a1b02a66da3b8a 100755 (executable)
@@ -7,15 +7,22 @@ import re
 import tempfile
 import errno
 import time
+import argparse
+import shutil
 from amara import bindery
 from amara.xslt import transform
 from Cheetah.Template import Template
 
-dist = os.path.dirname(os.getcwd())
-style = "default"
-style_xslt = dist+"/style/"+style+"/docbook.xsl"
-style_tmpl = dist+"/style/"+style+"/index.html.tmpl"
-outputdir = dist+"/htdocs/"
+parser = argparse.ArgumentParser(description='Process docbook article tree.')
+parser.add_argument('--style', nargs='?',
+                    default=os.path.dirname(os.getcwd())+'/style/default/')
+parser.add_argument('--output', nargs='?',
+                    default=os.path.dirname(os.getcwd())+'/htdocs/')
+args = parser.parse_args()
+
+style_xslt = args.style+"docbook.xsl"
+style_tmpl = args.style+"index.en.html.tmpl"
+outputdir = args.output
 
 valid_scripts = ['.py','.pl']
 MAXLEVEL = 10000
@@ -28,6 +35,12 @@ def mkdir_p(path):
             pass
         else: raise
 
+def publish(src,target):
+    cmd = ["rsync","-a","--delete",src,target]
+    retcode = subprocess.call(cmd)
+    if retcode:
+        print 'Error: '+' '.join(cmd)+' Returncode ['+str(retcode)+']'
+
 def generateSitemap():
   sitemap = []
   try:
@@ -38,16 +51,20 @@ def generateSitemap():
       sitemap.append(dict(link=f))
   except IOError, what_error:
     print 'Sitemap missing - generating one.'
+
   for dirname, dirnames, filenames in os.walk('.'):
     for filename in filenames:
       if fnmatch.fnmatch(filename, '*.xml'):
         xfile = os.path.join(dirname,filename)
         doc = bindery.parse(xfile,
                             prefixes={u'db': u'http://docbook.org/ns/docbook',
-                                      u'xi': u'http://www.w3.org/2001/XInclude'})
+                                      u'xi': u'http://www.w3.org/2001/XInclude',
+                                      u'xl': u'http://www.w3.org/1999/xlink'})
         title = doc.xml_select(u'/db:article/db:info/db:title')
         menu  = doc.xml_select(u'/db:article/db:info/db:titleabbrev')
         code  = doc.xml_select(u"//xi:include[@parse='text']")
+        resource = doc.xml_select(u"//db:link[@xl:href]")
+        image = doc.xml_select(u"//db:imagedata[@fileref]")
         exe = 0
         for c in code:
           (p, ext) = os.path.splitext(c.href)
@@ -58,12 +75,23 @@ def generateSitemap():
           found = 0
           base = xfile.split('.')[1]
           link = base.replace('index','')
-          level = len(filter(None,re.split(r'(/\w*/)',link)))
+          level = len(filter(None,re.split(r'(^/\w*/|\w*/)',link)))
+          res = []
+          for r in resource:
+              rf = os.path.join(dirname,r.href)
+              if os.path.isfile(rf):
+                  res.append(rf)
+          for i in image:
+              im = os.path.join(dirname,i.fileref)
+              if os.path.isfile(im):
+                  res.append(im)
           page = dict(title=unicode(doc.article.info.title),
                       menu=unicode(doc.article.info.titleabbrev),
-                      output=os.path.join(dirname,filename.replace('xml','html')),
+                      output=os.path.join(dirname,
+                                          filename.replace('xml','html')),
                       exe=exe,
                       file=xfile,
+                      res=res,
                       level=level)
           for l in sitemap:
             if l['link'] == link:
@@ -81,8 +109,9 @@ def generateSitemap():
   return sitemap
 
 def expandXincludeTxt(page):
-  doc = bindery.parse(page['file'],prefixes={u'db': u'http://docbook.org/ns/docbook',
-                                             u'xi': u'http://www.w3.org/2001/XInclude'})
+  doc = bindery.parse(page['file'],
+                      prefixes={u'db': u'http://docbook.org/ns/docbook',
+                                u'xi': u'http://www.w3.org/2001/XInclude'})
   if page['exe']:
     code  = doc.xml_select(u"//xi:include[@parse='text']")
     for c in code:
@@ -163,24 +192,30 @@ def writeToTemplate(page,doc,sitemap):
   (menu,menuname) = genMenu(page,sitemap,1,MAXLEVEL)
   (levelmenu,levelname) = genMenu(page,sitemap,page['level'],page['level'])
   template = Template(file=style_tmpl,
-                      searchList=[{'menu':menu},
+                      searchList=[{'title':page['title']},
+                                  {'menu':menu},
                                   {'article':doc},
                                   {'levelmenu':levelmenu},
                                   {'levelname':levelname}])
-  outfile = outputdir+page['output']
-  d = os.path.split(outfile)[0]
-  if d != '':
-    mkdir_p(d)
+  outfile = tmptarget+page['output']
+  mkdir_p(os.path.dirname(outfile))
   out = open(outfile, 'w')
   out.write(str(template))
-
+  out.close()
+  for r in page['res']:
+      mkdir_p(os.path.dirname(tmptarget+r))
+      shutil.copyfile(r, tmptarget+r)
 sitemap = generateSitemap()
+tmptarget = tempfile.mkdtemp()+'/'
 for page in sitemap:
   t1 = time.time()
-  print "Page : "+page['link'],
+  print "Page : %-30s %30s" % (page['link'],
+                      time.ctime(os.stat(page['file']).st_mtime)),
   doc = expandXincludeTxt(page)
   pubdoc = xsltConvert(doc)
   writeToTemplate(page,pubdoc,sitemap)
-#  publishResources()
   t2 = time.time()
-  print "["+str(round(t2-t1,2))+"]  done."
+  print "[%5.2f s]" % (round(t2-t1,2))
+publish(tmptarget, args.output)
+publish(args.style+"css", args.output)
+publish(args.style+"images",args.output)