Browse Source

Refactor text topic excerpt display into a macro

merge-requests/22/head
Deimos 6 years ago
parent
commit
34a933e6d3
  1. 5
      tildes/tildes/models/topic/topic.py
  2. 51
      tildes/tildes/templates/macros/topics.jinja2

5
tildes/tildes/models/topic/topic.py

@ -310,6 +310,11 @@ class Topic(DatabaseModel):
return (self.get_content_metadata('domain') return (self.get_content_metadata('domain')
or get_domain_from_url(self.link)) or get_domain_from_url(self.link))
@property
def is_spoiler(self) -> bool:
"""Return whether the topic is marked as a spoiler."""
return 'spoiler' in self.tags
def get_content_metadata(self, key: str) -> Any: def get_content_metadata(self, key: str) -> Any:
"""Get a piece of content metadata "safely". """Get a piece of content metadata "safely".

51
tildes/tildes/templates/macros/topics.jinja2

@ -47,31 +47,7 @@
</div> </div>
{% if topic.is_text_type and topic.get_content_metadata('excerpt') %} {% if topic.is_text_type and topic.get_content_metadata('excerpt') %}
{# warn about spoilers #}
{% if 'spoiler' in topic.tags %}
<details class="topic-text-excerpt">
<summary>
<span>Warning: this post may contain spoilers.</span>
</summary>
{{ topic.rendered_html|safe }}
</details>
{% else %}
{# if the "excerpt" is the full text, don't wrap in <details> #}
{% if not topic.get_content_metadata('excerpt').endswith('...') %}
<p class="topic-text-excerpt">{{ topic.get_content_metadata('excerpt') }}</p>
{% else %}
<details class="topic-text-excerpt"
{% if request.user and request.user.open_new_tab_text %}
data-js-external-links-new-tabs
{% endif %}
>
<summary>
<span>{{ topic.get_content_metadata('excerpt') }}</span>
</summary>
{{ topic.rendered_html|safe }}
</details>
{% endif %}
{% endif %}
{{ topic_excerpt_expandable(topic) }}
{% endif %} {% endif %}
<footer class="topic-info"> <footer class="topic-info">
@ -104,6 +80,31 @@
</article> </article>
{% endmacro %} {% endmacro %}
{% macro topic_excerpt_expandable(topic) %}
{% if topic.is_spoiler %}
{% set excerpt = 'Warning: this post may contain spoilers' %}
{% set is_expandable = True %}
{% else %}
{% set excerpt = topic.get_content_metadata('excerpt') %}
{# if the excerpt is the full text, it doesn't need to be expandable #}
{% set is_expandable = excerpt.endswith('...') %}
{% endif %}
{% if is_expandable %}
<details class="topic-text-excerpt"
{% if request.user and request.user.open_new_tab_text %}
data-js-external-links-new-tabs
{% endif %}
>
<summary><span>{{ excerpt }}</span></summary>
{{ topic.rendered_html|safe }}
</details>
{% else %}
<p class="topic-text-excerpt">{{ topic.get_content_metadata('excerpt') }}</p>
{% endif %}
{% endmacro %}
{% macro topic_voting(topic) %} {% macro topic_voting(topic) %}
{% if request.has_permission('vote', topic) %} {% if request.has_permission('vote', topic) %}
{% if topic.user_voted %} {% if topic.user_voted %}

Loading…
Cancel
Save