[gtk-doc] db2html: make navigation work



commit d76a00476019379436f198b7deddf69837ffa0b7
Author: Stefan Sauer <ensonic users sf net>
Date:   Sun Dec 17 13:13:28 2017 +0100

    db2html: make navigation work
    
    Extract titles in chunking step. Pass whole nodes to jinja so that
    we can access, filename and title.

 tools/db2html.py              |   41 ++++++++++++++++++++++++++++-------------
 tools/templates/book.html     |    2 +-
 tools/templates/common.html   |   30 +++++++++++++++++++++---------
 tools/templates/refentry.html |    3 +--
 4 files changed, 51 insertions(+), 25 deletions(-)
---
diff --git a/tools/db2html.py b/tools/db2html.py
index 6f0f17c..552d00d 100644
--- a/tools/db2html.py
+++ b/tools/db2html.py
@@ -25,7 +25,7 @@ The tool loaded the main xml document (<module>-docs.xml) and chunks it
 like the xsl-stylesheets would do. For that it resolves all the xml-includes.
 
 TODO: convert the docbook-xml to html
-- navigation
+- more templates
 - toc
 
 Requirements:
@@ -94,6 +94,13 @@ CHUNK_PARAMS = {
     'section': ChunkParams('s', 'chapter'),
 }
 
+TITLE_XPATH = {
+    'book': etree.XPath('//bookinfo/title/text()'),
+    'chapter': etree.XPath('//chapter/title/text()'),
+    'index': etree.XPath('//index/title/text()'),
+    'refentry': etree.XPath('//refentry/refmeta/refentrytitle/text()'),
+}
+
 # Jinja2 templates
 TOOL_PATH = os.path.dirname(os.path.abspath(__file__))
 TEMPLATE_ENV = Environment(
@@ -134,6 +141,16 @@ def gen_chunk_name(node):
     return name
 
 
+def get_chunk_title(node):
+    tag = node.tag
+    if tag not in TITLE_XPATH:
+        logging.warning('Add TITLE_XPATH for "%s"', tag)
+        return ''
+
+    xpath = TITLE_XPATH[tag]
+    return xpath(node, smart_strings=False)[0]
+
+
 def chunk(xml_node, parent=None):
     """Chunk the tree.
 
@@ -142,13 +159,14 @@ def chunk(xml_node, parent=None):
     """
     # print('<%s %s>' % (xml_node.tag, xml_node.attrib))
     if xml_node.tag in CHUNK_TAGS:
-        filename = gen_chunk_name(xml_node) + '.html'
         # TODO: do we need to remove the xml-node from the parent?
         #       we generate toc from the files tree
         # from copy import deepcopy
         # ..., xml=deepcopy(xml_node), ...
         # xml_node.getparent().remove(xml_node)
-        parent = Node(xml_node.tag, parent=parent, xml=xml_node, filename=filename)
+        parent = Node(xml_node.tag, parent=parent, xml=xml_node,
+                      filename=gen_chunk_name(xml_node) + '.html',
+                      title=get_chunk_title(xml_node))
     for child in xml_node:
         chunk(child, parent)
 
@@ -162,7 +180,7 @@ def convert(out_dir, files, node):
     with open(os.path.join(out_dir, node.filename), 'wt') as html:
         if node.name in TEMPLATES:
             # TODO: ideally precomiple common xpath exprs once:
-            #   func = etree.XPath("//b")
+            #   func = etree.XPath('//b')
             #   func(xml_node)[0]
             def lxml_xpath(expr):
                 return node.xml.xpath(expr, smart_strings=False)[0]
@@ -170,22 +188,19 @@ def convert(out_dir, files, node):
             template = TEMPLATES[node.name]
             template.globals['xpath'] = lxml_xpath
             params = {
-                'nav_home': (node.root.filename, ''),
+                'title': node.title,
+                'nav_home': node.root,
             }
-            # up, prev, next: link + title
-            # TODO: need titles, get them in the chunck stage from precompiled
-            # xpath exprs (xml_node.tag: get_title_xpath_expr)
+            # nav params: up, prev, next
             if node.parent:
-                params['nav_up'] = (node.parent.filename, '')
+                params['nav_up'] = node.parent
             ix = files.index(node)
             if ix > 0:
-                params['nav_prev'] = (files[ix - 1].filename, '')
+                params['nav_prev'] = files[ix - 1]
             if ix < len(files) - 1:
-                params['nav_next'] = (files[ix + 1].filename, '')
+                params['nav_next'] = files[ix + 1]
 
             html.write(template.render(**params))
-            # attempt to get the title, does not work
-            # print("Title: %s" % template.module.title)
         else:
             logging.warning('Add template for "%s"', node.name)
 
diff --git a/tools/templates/book.html b/tools/templates/book.html
index c606bb3..bc16c64 100644
--- a/tools/templates/book.html
+++ b/tools/templates/book.html
@@ -1,5 +1,4 @@
 {% from 'common.html' import doctype, head_links, navigation_main %}
-{% set title = xpath('//bookinfo/title/text()') %}
 {{ doctype() }}
 <html>
 <head>
@@ -12,6 +11,7 @@
 <div class="book">
 <div class="titlepage">
 {{ navigation_main(title) }}
+<hr>
 </div>
 </div>
 </body>
diff --git a/tools/templates/common.html b/tools/templates/common.html
index 64b3e86..7416e47 100644
--- a/tools/templates/common.html
+++ b/tools/templates/common.html
@@ -3,15 +3,15 @@
 {% endmacro %}
 
 {% macro head_links(home, up, prev, next) %}
-<link rel="home" href="{{ home[0] }}" title="{{ home[1] }}">
+<link rel="home" href="{{ home.filename }}" title="{{ home.title }}">
 {% if up is defined %}
-<link rel="up" href="{{ up[0] }}" title="{{ up[1] }}">
+<link rel="up" href="{{ up.filename }}" title="{{ up.title }}">
 {% endif %}
 {% if prev is defined %}
-<link rel="prev" href="{{ prev[0] }}" title="{{ prev[1] }}">
+<link rel="prev" href="{{ prev.filename }}" title="{{ prev.title }}">
 {% endif %}
 {% if next is defined %}
-<link rel="next" href="{{ next[0] }}" title="{{ next[1] }}">
+<link rel="next" href="{{ next.filename }}" title="{{ next.title }}">
 {% endif %}
 {% endmacro %}
 
@@ -24,7 +24,7 @@
 {% macro navigation_std(title) %}
 {% endmacro %}
 
-{% macro navigation_ref() %}
+{% macro navigation_ref(home, up, prev, next) %}
 <table class="navigation" id="top" width="100%" cellpadding="2" cellspacing="5">
   <tr valign="middle">
     <td width="100%" align="left" class="shortcuts">
@@ -34,10 +34,22 @@
         <a href="#tester-GtkdocTester.description" class="shortcut">Description</a>
       </span>
     </td>
-    <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" 
alt="Home"></a></td>
-    <td><a accesskey="u" href="ch01.html"><img src="up.png" width="16" height="16" border="0" 
alt="Up"></a></td>
-    <td><a accesskey="p" href="ch01.html"><img src="left.png" width="16" height="16" border="0" 
alt="Prev"></a></td>
-    <td><a accesskey="n" href="api-index.html"><img src="right.png" width="16" height="16" border="0" 
alt="Next"></a></td>
+    <td><a accesskey="h" href="{{ home.filename }}"><img src="home.png" width="16" height="16" border="0" 
alt="Home"></a></td>
+  {% if up is defined %}
+    <td><a accesskey="u" href="{{ up.filename }}"><img src="up.png" width="16" height="16" border="0" 
alt="Up"></a></td>
+  {% else %}
+    <td><img src="up-insensitive.png" width="16" height="16" border="0"></td>
+  {% endif %}
+  {% if prev is defined %}
+    <td><a accesskey="p" href="{{ prev.filename }}"><img src="left.png" width="16" height="16" border="0" 
alt="Prev"></a></td>
+  {% else %}
+    <td><img src="left-insensitive.png" width="16" height="16" border="0"></td>
+  {% endif %}
+  {% if next is defined %}
+    <td><a accesskey="n" href="{{ next.filename }}"><img src="right.png" width="16" height="16" border="0" 
alt="Next"></a></td>
+  {% else %}
+    <td><img src="right-insensitive.png" width="16" height="16" border="0"></td>
+  {% endif %}
   </tr>
 </table>
 
diff --git a/tools/templates/refentry.html b/tools/templates/refentry.html
index f9c2176..0d245a4 100644
--- a/tools/templates/refentry.html
+++ b/tools/templates/refentry.html
@@ -1,5 +1,4 @@
 {% from 'common.html' import doctype, head_links, navigation_ref %}
-{% set title = xpath('//refentry/refmeta/refentrytitle/text()') %}
 {{ doctype() }}
 <html>
 <head>
@@ -11,7 +10,7 @@
 <body>
 <div class="book">
 <div class="titlepage">
-{{ navigation_ref() }}
+{{ navigation_ref(nav_home, nav_up, nav_prev, nav_next) }}
 </div>
 </div>
 </body>


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