{%- macro automodule(modname, options) -%}
.. automodule:: {{ modname }}
{%- for option in options %}
   :{{ option }}:
{%- endfor %}
{%- endmacro %}

{%- macro toctree(docnames) -%}
.. toctree::
   :maxdepth: {{ maxdepth }}
{% for docname in docnames %}
   {{ docname }}
{%- endfor %}
{%- endmacro %}

{%- if is_namespace %}
{{- [pkgname, "namespace"] | join(" ") | e | heading }}
{% else %}
{{ pkgname | heading }}
{{ automodule(pkgname, automodule_options) }}
{% endif %}

{%- if subpackages %}
{{ toctree(subpackages) }}
{% endif %}

{%- if submodules %}
{%- for item in submodules %}
{% set last_item = item.split('.') | last %}
{{ last_item }}
{% for character in range(last_item|length) -%}-{% endfor %}
{{ automodule(item, automodule_options) }}
{% endfor %}
{% endif %}
