extract_toc
Hand-styled tables of content for sphinx themes.
Attention
This extension is enabled automatically when using the artisan
theme.
Usage
Custom themes
You can enable this extension by adding the following to your conf.py
file.
extensions = [
"sphinx_artisan_theme.ext.extract_toc",
...
]
Jinja2 helpers
get_navigation()
Extracts the in-page table of contents similar to the sphinx
{{toctree()}}
helper but returns a list ofNavigationSection
instances.
Example
{% macro navigation(item) %}
<li>
<a
href="{{ item.url }}"
{% if item.is_active %}class="active"{% endif %}
>
{{ item.title }}
</a>
{% if item.children %}
<ul>
{% for child in item.children %}
{{ navigation(item) }}
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
{% for section in get_navigation(
collapse=True,
maxdepth=3,
titles_only=True,
includehidden=False
) %}
{% if section.title %}
<h2>{{ section.title }}
{% endif %}
<ul>
{% for item in section.children %}
{{ navigation(item) }}
{% endfor %}
</ul>
{% endfor %}
get_table_of_contents()
- get_table_of_contents()
Extracts the in-page table of contents similar to the sphinx
{{toc}}
helper but returns an iterable ofNavigationItem
instances.- Returns:
A list of navigation items
- Return type:
Example
{% macro navigation(item) %}
<li>
<a
href="{{ item.url }}"
{% if item.is_active %}class="active"{% endif %}
>
{{ item.title }}
</a>
{% if item.children %}
<ul>
{% for child in item.children %}
{{ navigation(item) }}
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
<ul>
{% for item in get_table_of_contents() %}
{{ navigation(item) }}
{% endfor %}
</ul>
Reference
Provides jinja2 helpers for customisable tables of contents.
Navigation item.
Represents an individual link in a table of contents.
- Parameters:
title (str) – visible title
url (str) – link address
children (list[NavigationItem]) – child navigation items
is_active (bool) – is the current active page
Navigation section.
represents a section of the table of contents with optional title.
- Parameters:
title (str | None) – visible title
children (list[NavigationItem]) – child navigation items