[telepathy-doc/master] Add devhelp support to link-mapper

Davyd Madeley davyd at madeley.id.au
Thu Apr 2 22:03:06 PDT 2009


---
 docs/book/insert-links.py |   87 +++++++++++++++++++++++++++++++++++---------
 1 files changed, 69 insertions(+), 18 deletions(-)

diff --git a/docs/book/insert-links.py b/docs/book/insert-links.py
index 5dbd6fd..9438fa7 100755
--- a/docs/book/insert-links.py
+++ b/docs/book/insert-links.py
@@ -12,26 +12,77 @@
 
 URL_PREFIX = 'http://telepathy.freedesktop.org/spec.html#org.freedesktop.Telepathy.'
 
-# FIXME - it would be awesome to be able to derive these mappings directly
-# from the Telepathy spec
-MAPPINGS = {
-    'interfacename': {
-    	# interfaces
-	'Requests'	: 'Connection.Interface.Requests',
-    },
-
-    'methodname': {
-    	# methods
-	'CreateChannel'	: 'Connection.Interface.Requests.CreateChannel',
-	'EnsureChannel'	: 'Connection.Interface.Requests.EnsureChannel',
-	'RequestChannel': 'Connection.RequestChannel',
-    },
-}
-
 import sys
 import os.path
 from lxml import etree
 
+DEVHELP_NS = 'http://www.devhelp.net/book'
+
+class Mapper(object):
+    def __init__(self, *maps):
+        self.maps = maps
+
+    def get_maps(self):
+        map_ = {}
+
+        for mapper in self.maps:
+            for key, submap in mapper.get_maps().iteritems():
+                if key not in map_: map_[key] = {}
+
+                for name, url in submap.iteritems():
+                    if name in map_[key]:
+                        print >> sys.stderr, \
+                                    "WARNING: %s could be ambiguous" % name
+
+                    map_[key][name] = url
+
+        return map_
+
+class DevhelpMapper(object):
+    query = 'dh:functions/dh:keyword[@type = $type]'
+    namespaces = {
+        'dh': DEVHELP_NS,
+    }
+
+    def __init__(self, urlprefix, filename):
+        self.urlprefix = urlprefix
+
+        dom = etree.parse(filename)
+
+        self._build_function_list(dom)
+        self._build_class_list(dom)
+
+    def get_maps(self):
+        return {
+            'function': self.functions,
+            'classname': self.classes,
+        }
+
+    def _build_url(self, link):
+        return os.path.join(self.urlprefix, link)
+
+    def _build_function_list(self, dom):
+        functions = dom.xpath(
+            'dh:functions/dh:keyword[@type = "function" or @type = "macro"]',
+                                namespaces = self.namespaces)
+
+        self.functions = dict((f.get("name")[:-3],
+                               self._build_url(f.get("link")))
+                          for f in functions)
+
+    def _build_class_list(self, dom):
+        classes = dom.xpath(self.query, type = 'struct',
+                                namespaces = self.namespaces)
+
+        self.classes = dict((f.get("name"),
+                               self._build_url(f.get("link")))
+                          for f in classes)
+
+MAPPINGS = Mapper(
+    DevhelpMapper('http://telepathy.freedesktop.org/doc/telepathy-glib/',
+        '/usr/share/gtk-doc/html/telepathy-glib/telepathy-glib.devhelp2.gz'),
+).get_maps()
+
 try:
     filename = sys.argv[1]
     doc = etree.parse (filename)
@@ -45,11 +96,11 @@ for elem in doc.xpath (xpathexpr):
 	if elem.tag not in MAPPINGS: raise Exception ("Got tag that's not in mappings")
 	mapping = MAPPINGS[elem.tag]
 	if elem.text not in mapping: continue
-	suffix = mapping[elem.text]
+	url = mapping[elem.text]
 
 	# print >> sys.stderr, elem.tag, elem.text
 
-	link = etree.Element ('ulink', url=URL_PREFIX + suffix)
+	link = etree.Element ('ulink', url=url)
 	link.tail = elem.tail
 	elem.tail = None
 
-- 
1.5.6.5




More information about the telepathy-commits mailing list