size: printing size of style
[treecutter.git] / treecutter / docbook.py
index fd822ce8fe2cf6058fafa7528c56e0af0e38e0c8..9e970c98b679b2d8b81146e99e0e3141fac02d65 100644 (file)
@@ -2,6 +2,7 @@
 
 import os
 import subprocess
+import re
 
 from lxml import etree
 from lxml.builder import ElementMaker
@@ -11,6 +12,7 @@ from time import time
 
 import treecutter.constants as const
 from treecutter.image import Image
+#from treecutter.tools import warning
 
 class Docbook():
     """Class representing a docbook document"""
@@ -28,15 +30,54 @@ class Docbook():
            ta = unicode(ta[0].text)
         return (t, ta)
 
+    def status(self):
+        status = self._doc.xpath(u'/db:article[@status]',namespaces=const.XPATH)
+        if status:
+            return unicode(status[0].get('status'))
+        return None
+
+    def role(self):
+        art = self._doc.xpath(u'/db:article[@role]',namespaces=const.XPATH)
+        if art:
+            return unicode(art[0].get('role'))
+        return 'index'
+
+    def userlevel(self):
+        lvl = self._doc.xpath(u'/db:article[@userlevel]',namespaces=const.XPATH)
+        if lvl:
+            lvlstr = unicode(lvl[0].get('userlevel'))
+            return {
+                'Level 1': 1,
+                'Level 2': 2,
+                'Level 3': 3,
+                'Level 4': 4,
+                'Level 5': 5,
+            }.get(lvlstr, 0)
+        return 0
+
     def expand_imageobjects(self):
+        cwd = os.getcwd()
         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
         images  = self._doc.xpath(u"//db:imageobject/db:imagedata[@fileref]",namespaces=const.XPATH)
-        for io in images:
-            image = Image(io)
-            link = db.link(image.infostr(),**{const.XLINK+"href": f})
-            io = db.imageobject(
-                db.imagedata(fileref=image.format(800,600), width=str(800), depth=str(600)),
-                db.caption(db.para(image.caption())))
+        for i in images:
+            os.chdir(self._dirname)
+            im = i.get('fileref')
+            img = Image(im)
+            caption = db.caption()
+            for p in img.caption().split('\n\n'):
+                caption.append(db.para(p))
+            link = db.para(db.link(img.infostr(),
+                                   **{const.XLINK+"href": img.filename()}))
+            caption.append(link)
+            mo = db.mediaobject(db.imageobject(
+                db.imagedata(fileref=img.resize(800,600))),caption)
+            iop = i.getparent()
+            mop = iop.getparent()
+            mopp = mop.getparent()
+            mopp.insert(mopp.index(mop)+1,mo)
+            mopp.remove(mop)
+            os.chdir(cwd)
+
 
     def parse_xincludes(self):
         cwd = os.getcwd()
@@ -48,7 +89,7 @@ class Docbook():
             if ext in const.valid_scripts:
                 exe = []
                 script = os.path.join(os.path.abspath(self._dirname)+'/'+href)
-                if os.path.isfile(script):
+                if os.path.isfile(script) and os.access(script, os.X_OK):
                     exe.append(script)
                 else:
                     if href in resource_listdir('xinclude', ''):
@@ -60,6 +101,8 @@ class Docbook():
                     exe.append("lang="+alang)
                 if xpointer:
                     exe.append("xptr="+xpointer)
+                if exe == []:
+                    continue
                 print "  executing %15s" % (href),
                 ts = time()
                 os.chdir(self._dirname)
@@ -67,9 +110,10 @@ class Docbook():
                                        stderr=subprocess.PIPE)
                 (stdout, stderr) = xml.communicate()
                 #print xml.returnvalue
-                if stderr:
-                    print " ".join(exe)+" ERROR : [ "+stderr+" ]"
-                    exit
+#                if stderr:
+#                    warning("%s : %s" % (" ".join(exe),stderr))
+#                    warning(stdout)
+#                    exit
                 os.chdir(cwd)
                 te = time()
                 print " [%5.2f s]  (%s)" % (round(te-ts,2),xpointer)
@@ -79,6 +123,9 @@ class Docbook():
                 idp.insert(idp.index(c)+1,xstr)
                 idp.remove(c)
 
+    def xinclude(self):
+        self._doc.xinclude()
+
     def collect_links(self):
         res = []
         for r in self._doc.xpath(u"//db:link[@xlink:href]",namespaces=const.XPATH):
@@ -105,6 +152,16 @@ class Docbook():
                 print "WARNING: File "+im+" is missing!"
         return res
 
+    def collect_videos(self):
+        res = []
+        for i in self._doc.xpath(u"//db:videodata[@fileref]",namespaces=const.XPATH):
+            im = os.path.join(self._dirname,i.get('fileref'))
+            if os.path.isfile(im):
+                res.append(im)
+            else:
+                print "WARNING: File "+im+" is missing!"
+        return res
+
     def collect_forms(self):
         res = []
         for i in self._doc.xpath(u"//html:form[@action]",namespaces=const.XPATH):