Skip to content

Conversation

ntno
Copy link
Owner

@ntno ntno commented Jan 12, 2023

Description

control whitespace in jinja2 templates so that the final output is more readable

References

https://j2live.ttl255.com/

Testing

  • reviewed local doc site
  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in mkdocs-terminal/documentation
  • All active GitHub checks for tests, formatting, and security are passing

@ntno
Copy link
Owner Author

ntno commented Jan 12, 2023

{% macro make_link_start( tile ) -%}
<a  href="{{ tile.link_href }}" 
    {%- if tile.link_title is defined and tile.link_title|string|length %}title="{{ tile.link_title }}"{% endif %} 
    {%- if tile.link_target is defined and tile.link_target|string|length %}target="{{ tile.link_target }}"{% endif %} >
{%- endmacro -%}
{{ make_link_start(tile) }}

@ntno
Copy link
Owner Author

ntno commented Jan 12, 2023

{% macro make_image( tile ) -%}
<img src="{{ tile.img_src }}" alt="{{ tile.img_alt|default('', true ) }}"
{%- if tile.img_title is defined and tile.img_title|string|length %} title="{{ tile.img_title }}"{% endif %} 
    {%- if tile.img_width is defined and tile.img_width|string|length %} width="{{ tile.img_width }}"{% endif %} 
    {%- if tile.img_height is defined and tile.img_height|string|length %} height="{{ tile.img_height }}"{% endif %} >
{%- endmacro -%}

@ntno
Copy link
Owner Author

ntno commented Jan 12, 2023

{% macro make_image( tile ) -%}
                    <img src="{{ tile.img_src }}" alt="{{ tile.img_alt|default('', true ) }}"
{%- if tile.img_title is defined and tile.img_title|string|length %} title="{{ tile.img_title }}"{% endif %} 
    {%- if tile.img_width is defined and tile.img_width|string|length %} width="{{ tile.img_width }}"{% endif %} 
    {%- if tile.img_height is defined and tile.img_height|string|length %} height="{{ tile.img_height }}"{% endif %} >
{%- endmacro -%}
{% macro make_link_start( tile ) -%}
<a  href="{{ tile.link_href }}" 
    {%- if tile.link_title is defined and tile.link_title|string|length %}title="{{ tile.link_title }}"{% endif %} 
    {%- if tile.link_target is defined and tile.link_target|string|length %}target="{{ tile.link_target }}"{% endif %} >
{%- endmacro -%}

{% macro make_tile( tile ) -%}
{%- set ns = namespace(is_valid=false, has_link=false, has_image=false, has_caption=false) -%}
{% if tile.img_src is defined and tile.img_src|string|length -%}
    {%- set ns.has_image = true -%}
{%- endif -%}
{%- if tile.link_href is defined and tile.link_href|string|length -%}
    {%- set ns.has_link = true -%}
{%- endif -%}
{%- if tile.caption is defined and tile.caption|string|length -%}
    {%- set ns.has_caption = true -%}
{%- endif -%}
{%- if ns.has_link or ns.has_image -%}
    {%- set ns.is_valid = true -%}
{%- endif -%}
{%- if ns.is_valid -%}
<div {% if tile.id is defined and tile.id|string|length %} id="{{ tile.id }}"{%- endif -%} class="terminal-mkdocs-tile {{ tile.class }}">
    <figure>
        {% if ns.has_link -%} 
             {{ make_link_start(tile) }}
        {%- endif %}
        {% if ns.has_image -%} 
              {{ make_image(tile) }}
        {%- endif -%}

        {%- if ns.has_link -%}
            {% if ns.has_image %} 
        </a>
            {%- else -%}
            {{ tile.link_text|default(tile.link_href, true) }}</a>
            {%- endif -%}
        {%- endif -%}
        
        {%- if ns.has_caption %}
      <figcaption>{{ tile.caption|string|trim }}</figcaption>
        {%- endif %}
    </figure>
</div>
{% endif %}
{%- endmacro -%}

{{ make_tile(tile) }}

@ntno
Copy link
Owner Author

ntno commented Jan 12, 2023

{% macro make_image( tile ) -%}
                    <img src="{{ tile.img_src }}" alt="{{ tile.img_alt|default('', true ) }}"
{%- if tile.img_title is defined and tile.img_title|string|length %} title="{{ tile.img_title }}"{% endif %} 
    {%- if tile.img_width is defined and tile.img_width|string|length %} width="{{ tile.img_width }}"{% endif %} 
    {%- if tile.img_height is defined and tile.img_height|string|length %} height="{{ tile.img_height }}"{% endif %} >
{%- endmacro -%}
{% macro make_link_start( tile ) -%}
<a href="{{ tile.link_href }}" 
    {%- if tile.link_title is defined and tile.link_title|string|length %}title="{{ tile.link_title }}"{% endif %} 
    {%- if tile.link_target is defined and tile.link_target|string|length %}target="{{ tile.link_target }}"{% endif %} >
{%- endmacro -%}

{% macro make_tile( tile ) -%}
{%- set ns = namespace(is_valid=false, has_link=false, has_image=false, has_caption=false) -%}
{% if tile.img_src is defined and tile.img_src|string|length -%}
    {%- set ns.has_image = true -%}
{%- endif -%}
{%- if tile.link_href is defined and tile.link_href|string|length -%}
    {%- set ns.has_link = true -%}
{%- endif -%}
{%- if tile.caption is defined and tile.caption|string|length -%}
    {%- set ns.has_caption = true -%}
{%- endif -%}
{%- if ns.has_link or ns.has_image -%}
    {%- set ns.is_valid = true -%}
{%- endif -%}
{%- if ns.is_valid -%}
<div {% if tile.id is defined and tile.id|string|length %} id="{{ tile.id }}"{%- endif -%} class="terminal-mkdocs-tile {{ tile.class }}">
    <figure>
        {% if ns.has_link -%} 
             {{ make_link_start(tile) }}
        {%- endif -%}
        {% if ns.has_image -%} 
              {{ make_image(tile) }}
        {%- endif -%}
        {%- if ns.has_link -%}
            {%- if ns.has_image -%} 
                </a>{%- else -%} {{ tile.link_text|default(tile.link_href, true) }}</a>
            {%- endif -%}
        {%- endif -%}
        
        {%- if ns.has_caption %}
      <figcaption>{{ tile.caption|string|trim }}</figcaption>
        {%- endif %}
    </figure>
</div>
{% endif %}
{%- endmacro -%}

{{ make_tile(tile) }}

@ntno ntno merged commit dd51452 into main Jan 12, 2023
@ntno ntno deleted the strip-extra-whitespace branch January 12, 2023 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant