tardyp / sphinx-jinja

MIT License
28 stars 22 forks source link

.. image:: https://github.com/tardyp/sphinx-jinja/actions/workflows/ci.yml/badge.svg

sphinx-jinja

A sphinx extension to include jinja based templates based documentation into a sphinx doc

Usage

In your rst doc, you can use the following snippet to use a jinja template to generate your doc

.. code:: rst

.. jinja:: first_ctx

    {% for k, v in topics.items() %}

    {{k}}
    ~~~~~
    {{v}}
    {% endfor %}

In your sphinx conf.py file, you can create or load the contexts needed for your jinja templates

.. code:: python

extensions = ['sphinx_jinja']

jinja_contexts = {
    'first_ctx': {'topics': {'a': 'b', 'c': 'd'}}
}

You can also customize the jinja Environment by passing custom kwargs, adding filters, tests, and globals, and setting policies:

.. code:: python

jinja_env_kwargs = {
    'lstrip_blocks': True,
}

jinja_filters = {
    'bold': lambda value: f'**{value}**',
}

jinja_tests = {
    'instanceof': lambda value, type: isinstance(value, type),
}

jinja_globals = {
    'list': list,
}

jinja_policies = {
    'compiler.ascii_str': False,
}

Which can then be used in the templates:

.. code:: rst

Lists
-----

{% for o in objects -%}
    {%- if o is instanceof list -%}
        {%- for x in o -%}
            - {{ x|bold }}
        {% endfor -%}
    {%- endif -%}
{%- endfor %}

Available options

Example of declaration in your RST file:

.. code:: rst

  .. jinja:: approval_checks_api
     :file: relative/path/to/template.jinja
     :header_char: -

Each element of the jinja_contexts dictionary is a context dict for use in your jinja templates.

Running tests