{#- Template for TwinCAT POUs (Program Organization Units).

This template renders a TwinCAT POU (Function Block, Program, or Function).

Context:
  pou: The POU object to render.
  root (bool): Whether this is the root object, injected with `:::` in a Markdown page.
  heading_level (int): The HTML heading level to use.
  config (dict): The configuration options.
-#}


<div class="doc doc-object doc-pou">
  {% with obj = pou, html_id = pou.get_identifier() %}

    {% if root %}
      {% set show_full_path = config.show_root_full_path %}
      {% set root_members = True %}
    {% elif root_members %}
      {% set show_full_path = config.show_root_members_full_path or config.show_object_full_path %}
      {% set root_members = False %}
    {% else %}
      {% set show_full_path = config.show_object_full_path %}
    {% endif %}

    {% set pou_name = pou.get_identifier() if show_full_path else pou.name %}

    {% if not root or config.show_root_heading %}
      {% filter heading(
          heading_level,
          role="pou",
          id=html_id,
          class="doc doc-heading",
          toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-pou"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else pou.name),
        ) %}

        {% block heading scoped %}
          {#- Heading block.

          This block renders the heading for the POU.
          -#}
          {% if config.show_symbol_type_heading %}<code class="doc-symbol doc-symbol-heading doc-symbol-pou"></code>{% endif %}
          <span class="doc doc-object-name doc-pou-name">{{ config.heading if config.heading and root else pou_name }}</span>
        {% endblock heading %}
        {% block labels scoped %}
          {#- Labels block.

          This block renders the labels for the class.
          -#}
          {% with labels = pou.labels %}
            {% include "labels.html.jinja" %}
          {% endwith %}
        {% endblock labels %}
      {% endfilter %}
    {% else %}
      {% if config.show_root_toc_entry %}
        {% filter heading(heading_level,
            role="pou",
            id=html_id,
            toc_label=('<code class="doc-symbol doc-symbol-toc doc-symbol-pou"></code>&nbsp;'|safe if config.show_symbol_type_toc else '') + (config.toc_label if config.toc_label and root else pou.name),
            hidden=True,
          ) %}
        {% endfilter %}
      {% endif %}
      {% set heading_level = heading_level - 1 %}
    {% endif %}

    <div class="doc doc-contents {% if root %}first{% endif %}">
      {% block contents scoped %}
        {#- Contents block.

        This block renders the contents of the POU.
        It contains other blocks that users can override.
        Overriding the contents block allows to rearrange the order of the blocks.
        -#}

        {% block docstring scoped %}
          {#- Docstring block.

          This block renders the documentation for the POU.
          -#}

            {% with documentation=pou.documentation %}
                {% include "documentation.html.jinja"%}
            {% endwith %}          
        {% endblock docstring %}

        {% block summary scoped %}
          {#- Summary block.

        
          -#}
            {% with context=pou %}
              {% include "summary.html.jinja" %}
            {% endwith %}   
        {% endblock summary %}


        {% block sourcecode scoped %}
          {#- declaration block.

          This block renders the declaration for the POU.
          -#}

          {% if (pou.declaration or pou.implementation) and config.show_source %}
            <details class="quote">
              <summary>Source Code</summary>
              {{ pou.declaration|highlight(language="pascal", linestart=0, linenums=True) }}
              {{ pou.implementation|highlight(language="pascal", linestart=0, linenums=True) }}
            </details>
          {% endif %}
        {% endblock sourcecode %}



        {% block children scoped %}
          {#- Children block.

          This block renders the children.
          -#}
          {% set root = False %}
          {% set heading_level = heading_level + 1 %}
            {% with context=pou %}
                {% include "children.html.jinja" with context %}
            {% endwith %} 
        {% endblock children %}

      {% endblock contents %}
    </div>

  {% endwith %}
</div>

