{% macro format_count(count) %} {% if count is none or count == 0 %} {# Return non-breaking space for zero or none values to maintain alignment #}   {% elif count >= 1000000 %} {{ "%.1f"|format(count/1000000)|replace('.0', '') }}M {% elif count >= 1000 %} {{ "%.1f"|format(count/1000)|replace('.0', '') }}K {% else %} {{ count }} {% endif %} {% endmacro %} {% macro render_media_src(media_item) %} {% if media_item.raw_preview_bytes and media_item.raw_preview_bytes | length > 0 %} data:image/jpeg;base64,{{ media_item.raw_preview_bytes | b64encode | string }} {% else %} {{ media_item.preview_url }} {% endif %} {% endmacro %} {%- macro format_tweet_text(text) -%} {%- set text = text | regex_replace("(#\\w+)", '\\1') -%} {%- set text = text | regex_replace("(@\\w+)", '\\1') -%} {%- set text = text | regex_replace("(https?://[\\w./?&=]+)", '\\1') -%} {{- text | safe -}} {%- endmacro -%} {% macro render_tweet_macro(tweet_item, is_quoted_tweet=False, is_last_in_thread=False, is_single_tweet=False, show_connector_on_last=False) %} {% if is_quoted_tweet %}
{{ tweet_item.user.username }} avatar

{{- format_tweet_text(tweet_item.text) -}}

{% if tweet_item.media %} {% set media_count = tweet_item.media | length %} {% set media_class = 'media-count-' + ((media_count|string) if media_count <= 4 else 'many') %}
{% for media_item in tweet_item.media %} {% if media_item.type == 'video' %}
Tweet video
{% else %} Tweet image {% endif %} {% endfor %}
{% endif %}
{% if tweet_item.quoted_tweet %}
{{ render_tweet_macro(tweet_item.quoted_tweet, is_quoted_tweet=True, is_last_in_thread=True) }}
{% endif %}
{% else %}
{{ tweet_item.user.username }} avatar {# Show connector if it's NOT the last in thread OR if show_connector_on_last is true and it IS the last #} {% if not is_last_in_thread or (is_last_in_thread and show_connector_on_last) %}
{% endif %}
{% if is_single_tweet %} {{ tweet_item.user.username }} avatar {% endif %}

{{- format_tweet_text(tweet_item.text) -}}

{% if tweet_item.media %} {% set media_count = tweet_item.media | length %} {% set media_class = 'media-count-' + ((media_count|string) if media_count <= 4 else 'many') %}
{% for media_item in tweet_item.media %} {% if media_item.type == 'video' %}
Tweet video
{% else %} Tweet image {% endif %} {% endfor %}
{% endif %}
{% if tweet_item.quoted_tweet %}
{{ render_tweet_macro(tweet_item.quoted_tweet, is_quoted_tweet=True, is_last_in_thread=True) }}
{% endif %}
{% endif %} {% endmacro %}
{% for tweet in thread %} {# Pass show_connector_on_last from the context, default to false if not provided #} {{ render_tweet_macro(tweet, is_quoted_tweet=False, is_last_in_thread=loop.last, is_single_tweet=(is_single_tweet|default(false)), show_connector_on_last=show_connector_on_last|default(false)) }} {% endfor %}