comparison doc/taglist.py @ 1217:c0515c0dfef9

Wrote a sphinx extension for a taglist directive that outputs a javascript tag list and related functions
author boulanni <nicolas_boulanger@hotmail.com>
date Wed, 22 Sep 2010 12:49:41 -0400
parents
children 621e03253f0c
comparison
equal deleted inserted replaced
1216:5a8930e089ed 1217:c0515c0dfef9
1
2 from docutils import nodes
3 from sphinx.util.compat import Directive
4 from collections import defaultdict
5
6 def taglist_html(self, node):
7 env = app.builder.env
8 if not hasattr(env, 'tags_db'): return
9
10 self.body.append("""
11 <script langage=javascript>
12 var cur = null;
13 var cura = null;
14 function change(a1, div1){
15 nexta = document.getElementById(a1)
16 next = document.getElementById(div1)
17 if (cur != null) { cur.style.display = 'none'; cura.style.fontWeight = 'normal'; }
18 if (cur != next) { next.style.display = 'block'; nexta.style.fontWeight = 'bold'; cur = next; cura = nexta; }
19 else cur = null
20 }
21 </script>
22 """)
23
24 for tag in sorted(env.tags_db):
25 tid = tag.replace(' ','_')
26 self.body.append(""" <a id=a_%s href="javascript:change('a_%s', 'div_%s')">%s</a> &nbsp;""" % (tid, tid, tid, tag))
27
28 for tag in env.tags_db:
29 tid = tag.replace(' ','_')
30 self.body.append('<div id=div_%s style="display:none;"><br>' % (tid,))
31 for f in sorted(env.tags_db[tag], lambda f,g: cmp(f[1], g[1])):
32 uri = app.builder.get_relative_uri(node.docname, f[0]) + '#' + f[1]
33 self.body.append('<a href="%s">%s</a><br>' % (uri, f[1]))
34 self.body.append('</div>')
35
36 class taglist_node(nodes.Element): pass
37
38 class taglist(Directive):
39 def run(self):
40 a = taglist_node()
41 a.docname = app.builder.env.docname
42 return [a]
43
44 def purge_tags(app, env, docname):
45 if not hasattr(env, 'tags_db'): return
46
47 for tag in env.tags_db:
48 for f in [f for f in env.tags_db[tag]]:
49 if f[0]==docname: env.tags_db[tag].remove(f)
50
51 def autodoc_process_docstring(app, what, name, obj, options, lines):
52 env = app.builder.env
53 if not hasattr(env, 'tags_db'):
54 env.tags_db = defaultdict(set)
55
56 if what == 'function':
57 for tag in obj.tags:
58 env.tags_db[tag].add((env.docname, name))
59
60 def setup(app_):
61 global app
62 app = app_
63
64 app.add_node(taglist_node, html=(taglist_html, lambda a,b:None))
65 app.add_directive('taglist', taglist)
66 app.connect('env-purge-doc', purge_tags)
67 app.connect('autodoc-process-docstring', autodoc_process_docstring)
68
69