Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Last active January 2, 2016 11:59
Show Gist options
  • Save shimizukawa/8300704 to your computer and use it in GitHub Desktop.
Save shimizukawa/8300704 to your computer and use it in GitHub Desktop.
Sphinx extension to render local toctree in templates (ex. sidebar). see also: https://groups.google.com/d/msg/sphinx-users/d0dwJ4YiMhs/PjHqUVozriIJ
sys.path.insert(0, os.path.abspath('.'))
extensions = ['localtoc']
html_sidebars = {'somepage': ['localtoc.html']}
<h3>local toc</h3>
{{ localtoc(pagename) }}
# -*- coding: utf-8 -*-
"""
``localtoc``: A callable yielding the local TOC tree that contains
list of all headings in the specified page exclude page title.
``localtoc`` need pagename specifing like ``{{ localtoc(pagename) }}``.
"""
def init_localtoc(app):
def _get_localtoc(docname):
toc = app.env.get_toc_for(docname, app.builder)
try:
del toc[0][0]
return app.builder.render_partial(toc)['fragment']
except:
return ''
ctx = app.env.config['html_context']
if 'localtoc' not in ctx:
ctx['localtoc'] = _get_localtoc
def setup(app):
app.connect('builder-inited', init_localtoc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment