annotate doc/taglist.py @ 1257:d79070c60546

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