# HG changeset patch # User boulanni # Date 1285174181 14400 # Node ID c0515c0dfef9baf1db8aae57e55cc21bbd70abc1 # Parent 5a8930e089ed3a946a08300a7a5b9c632b82db0a Wrote a sphinx extension for a taglist directive that outputs a javascript tag list and related functions diff -r 5a8930e089ed -r c0515c0dfef9 doc/conf.py --- a/doc/conf.py Wed Sep 22 12:12:30 2010 -0400 +++ b/doc/conf.py Wed Sep 22 12:49:41 2010 -0400 @@ -23,7 +23,7 @@ # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'ext'] +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'ext', 'taglist'] todo_include_todos = True diff -r 5a8930e089ed -r c0515c0dfef9 doc/index.txt --- a/doc/index.txt Wed Sep 22 12:12:30 2010 -0400 +++ b/doc/index.txt Wed Sep 22 12:49:41 2010 -0400 @@ -19,6 +19,10 @@ The ``pylearn`` subfolder should be on your ``$PYTHONPATH``. +Tags list +========== +.. taglist:: + Documentation ============= diff -r 5a8930e089ed -r c0515c0dfef9 doc/taglist.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/doc/taglist.py Wed Sep 22 12:49:41 2010 -0400 @@ -0,0 +1,69 @@ + +from docutils import nodes +from sphinx.util.compat import Directive +from collections import defaultdict + +def taglist_html(self, node): + env = app.builder.env + if not hasattr(env, 'tags_db'): return + + self.body.append(""" + + """) + + for tag in sorted(env.tags_db): + tid = tag.replace(' ','_') + self.body.append(""" %s  """ % (tid, tid, tid, tag)) + + for tag in env.tags_db: + tid = tag.replace(' ','_') + self.body.append('') + +class taglist_node(nodes.Element): pass + +class taglist(Directive): + def run(self): + a = taglist_node() + a.docname = app.builder.env.docname + return [a] + +def purge_tags(app, env, docname): + if not hasattr(env, 'tags_db'): return + + for tag in env.tags_db: + for f in [f for f in env.tags_db[tag]]: + if f[0]==docname: env.tags_db[tag].remove(f) + +def autodoc_process_docstring(app, what, name, obj, options, lines): + env = app.builder.env + if not hasattr(env, 'tags_db'): + env.tags_db = defaultdict(set) + + if what == 'function': + for tag in obj.tags: + env.tags_db[tag].add((env.docname, name)) + +def setup(app_): + global app + app = app_ + + app.add_node(taglist_node, html=(taglist_html, lambda a,b:None)) + app.add_directive('taglist', taglist) + app.connect('env-purge-doc', purge_tags) + app.connect('autodoc-process-docstring', autodoc_process_docstring) + +