Last active
January 2, 2016 11:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sys.path.insert(0, os.path.abspath('.')) | |
extensions = ['localtoc'] | |
html_sidebars = {'somepage': ['localtoc.html']} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h3>local toc</h3> | |
{{ localtoc(pagename) }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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