[library-web] create mallard cache for links support



commit ea1d98d22c5ff8f43f460b9765f7f9669679afae
Author: Frédéric Péters <fpeters 0d be>
Date:   Fri Aug 7 14:17:43 2009 +0200

    create mallard cache for links support

 src/modtypes/mallard.py |   91 ++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 87 insertions(+), 4 deletions(-)
---
diff --git a/src/modtypes/mallard.py b/src/modtypes/mallard.py
index 2ed0f38..5bb9a2f 100644
--- a/src/modtypes/mallard.py
+++ b/src/modtypes/mallard.py
@@ -39,6 +39,85 @@ except ImportError:
 import errors
 from base import DocModule
 
+def dup_node(e):
+    return ET.fromstring(ET.tostring(e))
+
+
+MAL_NS = 'http://projectmallard.org/1.0/'
+
+
+class MallardPage:
+    link_title = None
+    sort_title = None
+    info_nodes = None
+
+    def __init__(self, filename):
+        self.info_nodes = []
+
+        tree = ET.parse(filename)
+        self.id = tree.getroot().attrib['id']
+        if self.id is None:
+            return
+        self.page_info(tree.find('{%s}info' % MAL_NS))
+        self.title_tag = tree.find('{%s}title' % MAL_NS)
+        if self.title_tag is not None:
+            if self.link_title is None:
+                self.link_title = dup_node(self.title_tag)
+                self.link_title.attrib['type'] = 'link'
+            if self.sort_title is None:
+                self.sort_title = dup_node(self.title_tag)
+                self.sort_title.attrib['type'] = 'sort'
+        # XXX: support for section
+
+    def page_info(self, element):
+        if element is None:
+            return
+        title_tag = element.find('{%s}title' % MAL_NS)
+        if title_tag is not None:
+            type = title_tag.attrib['type']
+            role = title_tag.attrib['role']
+            if type == 'link' and not role:
+                self.link_title = dup_node(title_tag)
+            if type == 'sort':
+                self.sort_title = dup_node(title_tag)
+        for desc_tag in element.findall('{%s}desc' % MAL_NS):
+            self.info_nodes.append(desc_tag)
+        for link_tag in element.findall('{%s}link' % MAL_NS):
+            self.info_nodes.append(link_tag)
+
+    def toxml(self):
+        e = ET.Element('{%s}page' % MAL_NS)
+        e.attrib['id'] = self.id
+        info = ET.SubElement(e, '{%s}info' % MAL_NS)
+        for node in self.info_nodes:
+            info.append(node)
+        info.append(self.link_title)
+        info.append(self.sort_title)
+        e.append(dup_node(self.title_tag))
+        return e
+
+
+class MallardCache:
+    def __init__(self):
+        self.all_pages = []
+
+    def add_page(self, filename):
+        page = MallardPage(filename)
+        if page.id is None:
+            return
+        self.all_pages.append(page)
+
+    def toxml(self):
+        mallard_cache = ET.Element('{%s}cache' % MAL_NS)
+        for page in self.all_pages:
+            mallard_cache.append(page.toxml())
+        return mallard_cache
+
+    def dump(self, filename):
+        tree = ET.ElementTree(self.toxml())
+        tree.write(filename)
+
+
 class MallardModule(DocModule):
     '''Class for documentation shipped in a tarball and using gnome-doc-utils'''
     mal2html_xsl_file = os.path.join(data_dir, 'xslt', 'mal2html.xsl')
@@ -139,17 +218,21 @@ class MallardModule(DocModule):
             else:
                 lang_dirname = os.path.join(ext_dirname, self.dirname, lang)
 
+            mallard_cache = MallardCache()
+            for doc_page in doc_pages:
+                xml_file = os.path.join(lang_dirname, doc_page)
+                mallard_cache.add_page(xml_file)
+            temporary = tempfile.NamedTemporaryFile()
+            mallard_cache.dump(temporary.name)
+
             for doc_page in doc_pages:
                 xml_file = os.path.join(lang_dirname, doc_page)
-                if not os.path.exists(xml_file):
-                    # the document had a translation available in a previous
-                    # version, and it got removed
-                    continue
                 filename = os.path.splitext(doc_page)[0] + '.html.' + lang
 
                 # format docbook into html files
                 cmd = ['xsltproc', '--output', web_output_dir + '/' + filename,
                         '--nonet', '--xinclude',
+                        '--stringparam', 'mal.cache.file', temporary.name,
                         '--stringparam', 'libgo.lang', lang,
                         '--stringparam', 'libgo.channel', self.channel,
                         self.mal2html_xsl_file, xml_file]



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]