{%+ macro render_line(line) +%}
            {% if line.boundary %}<TextLine id="{{ line.id }}"
                      {%- if line.tags %} custom="{% set items = [] -%}
                          {%- for k, v in line.tags | dictsort -%}
                              {%- for tag in v -%}
                                {% set tag_str = [k ~ ' {'] %}
                                {%- for tk, tv in tag.items() -%}
                                  {% set _ = tag_str.append(tk ~ ':' ~ tv ~ ';') %}
                                {%- endfor -%}
                                {% set _ = tag_str.append('}') %}
                                {% set _ = items.append(tag_str|join()) %}
                              {%- endfor -%}
                          {%- endfor -%}
                          {{ items | join(' ') }}"
                      {%- endif -%}
                      {% if line.base_dir %} readingDirection="{{ {'R': 'right-to-left', 'L': 'left-to-right'}[line.base_dir] }}"{% endif %}>
                {% if line.boundary %}
                <Coords points="{% for point in line.boundary %}{{ point|join(',') }}{% if not loop.last %} {% endif %}{% endfor %}"/>
                {% endif %}
                {% if line.baseline %}
                <Baseline points="{% for point in line.baseline %}{{ point|join(',') }}{% if not loop.last %} {% endif %}{% endfor %}"/>
                {% endif %}
                {% if line.text is string %}
                    <TextEquiv><Unicode>{{ line.text|e }}</Unicode></TextEquiv>
                {% else %}
                {% for segment in line.recognition %}
                <Word id="segment_{{ segment.index }}">
                    {% if segment.boundary %}
                    <Coords points="{% for point in segment.boundary %}{{ point|join(',') }}{% if not loop.last %} {% endif %}{% endfor %}"/>
                    {% else %}
                    <Coords points="{{ segment.bbox[0] }},{{ segment.bbox[1] }} {{ segment.bbox[0] }},{{ segment.bbox[3] }} {{ segment.bbox[2] }},{{ segment.bbox[3] }} {{ segment.bbox[2] }},{{ segment.bbox[1] }}"/>
                    {% endif %}
                {% for char in segment.recognition %}
                    <Glyph id="char_{{ char.index }}">
                        <Coords points="{% for point in char.boundary %}{{ point|join(',') }}{% if not loop.last %} {% endif %}{% endfor %}"/>
                        <TextEquiv conf="{{ char.confidence|round(4) }}"><Unicode>{{ char.text|e }}</Unicode></TextEquiv>
                    </Glyph>
                {% endfor %}
                    <TextEquiv conf="{{ (segment.confidences|sum / segment.confidences|length)|round(4) }}"><Unicode>{{ segment.text|e }}</Unicode></TextEquiv>
                </Word>
                {% endfor %}
                {%+ if line.confidences|length %}<TextEquiv conf="{{ (line.confidences|sum / line.confidences|length)|round(4) }}"><Unicode>{% for segment in line.recognition %}{{ segment.text|e }}{% endfor %}</Unicode></TextEquiv>{% endif +%}
                {% endif %}
            </TextLine>{% endif %}
{%+ endmacro %}
<?xml version="1.0" encoding="UTF-8"?>
<PcGts xmlns="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15 http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15/pagecontent.xsd">
    <Metadata>
        <Creator>kraken {{ metadata.version }}</Creator>
        <Created>{{ page.date }}</Created>
        <LastChange>{{ page.date }}</LastChange>
    </Metadata>
    <Page imageFilename="{{ page.name }}" imageWidth="{{ page.size[0] }}" imageHeight="{{ page.size[1] }}">
    {% for entity in page.entities %}
        {% if entity.type == "region" %}
        {% if loop.previtem and loop.previtem.type == 'line' %}
        </TextRegion>
        {% endif %}
        <TextRegion id="{{ entity.id }}"
                    {%- if entity.tags %} custom="{% set items = [] -%}
                    {%- for k, v in entity.tags | dictsort -%}
                        {%- for tag in v -%}
                          {% set tag_str = [k ~ ' {'] %}
                          {%- for tk, tv in tag.items() -%}
                            {% set _ = tag_str.append(tk ~ ':' ~ tv ~ ';') %}
                          {%- endfor -%}
                          {% set _ = tag_str.append('}') %}
                          {% set _ = items.append(tag_str|join()) %}
                        {%- endfor -%}
                    {%- endfor -%}
                    {{ items | join(' ') }}"
                    {%- endif -%}
                    {% if entity.base_dir %} readingDirection="{{ {'R': 'right-to-left', 'L': 'left-to-right'}[entity.base_dir] }}"{% endif %}>
                {% if entity.boundary %}<Coords points="{% for point in entity.boundary %}{{ point|join(',') }}{% if not loop.last %} {% endif %}{% endfor %}"/>{% else %}<Coords points="0,0 0,0 0,0"/>{% endif %}
            {%- for line in entity.lines -%}
            {{ render_line(line) }}
            {%- endfor %}
        </TextRegion>
        {% else %}
        {% if not loop.previtem or loop.previtem.type != 'line' %}
        <TextRegion id="textblock_{{ loop.index }}">
            <Coords points="0,0 0,{{ page.size[1] }} {{ page.size[0] }},{{ page.size[1] }} {{ page.size[0] }},0"/>
        {% endif %}
            {{ render_line(entity) }}
        {% if loop.last %}
        </TextRegion>
        {% endif %}
        {% endif %}
    {% endfor %}
    </Page>
</PcGts>
