Skip to content

Commit

Permalink
Refactor macros
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Nov 6, 2023
1 parent c3222a1 commit 76c7d10
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 77 deletions.
20 changes: 3 additions & 17 deletions src/hexdoc/_templates/macros/formatting.html.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% import "macros/styles.html.jinja" as styles with context -%}
{% import "macros/recipes.html.jinja" as recipe_macros with context %}
{% import "macros/textures.html.jinja" as texture_macros with context -%}

{# jump to top icon in section headers #}
{% macro jump_to_top() -%}
Expand All @@ -22,28 +22,14 @@
{# header for categories and entries #}
{% macro section_header(value, header_tag, class_name) -%}
<{{ header_tag }} class="{{ class_name }} page-header">
{{ render_icon(value.icon) }}
{{ texture_macros.render_icon(value.icon) }}
{{- value.name ~ jump_to_top() ~ permalink(value.id.path) -}}
</{{ header_tag }}>
{%- endmacro %}

{% macro render_icon(item_or_texture, name="") -%}
{% if item_or_texture.gaslighting is defined %}
{{ recipe_macros.render_item(item_or_texture) }}
{% else %}
{{
recipe_macros.render_texture(
name=name,
texture=item_or_texture,
class_names=[],
)
}}
{% endif %}
{%- endmacro%}

{# link to value.id, with spoiler blur if value is a spoiler #}
{% macro maybe_spoilered_link(value) -%}
<a href="#{{ value.id.path }}" class="toc-elem {{ 'spoilered' if value.is_spoiler }}">{{ render_icon(value.icon) }} {{ value.name }}</a>
<a href="#{{ value.id.path }}" class="toc-elem {{ 'spoilered' if value.is_spoiler }}">{{ texture_macros.render_icon(value.icon) }} {{ value.name }}</a>
{%- endmacro %}

{# macro block which spoiler blurs its content if value is a spoiler #}
Expand Down
59 changes: 4 additions & 55 deletions src/hexdoc/_templates/macros/recipes.html.jinja
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% import "macros/textures.html.jinja" as texture_macros with context -%}

{# show the names of all the recipe results in a list of recipes #}
{% macro generic(recipes, result_attribute, description, separator) -%}
<blockquote class="crafting-info">
Expand All @@ -10,59 +12,6 @@
</blockquote>
{%- endmacro %}

{% macro render_texture(name, texture, class_names=[], lazy=true) -%}
{% if texture.meta is defined %}
<div
role="img"
title="{{ name }}"
aria-label="Animated image of {{ name }}"
class="{{ (class_names + ['texture', 'animated-sync', texture.class_name])|join(' ') }}"
></div>
{% else %}
<img
title="{{ name }}"
alt="Image of {{ name }}"
src="{{ texture.url }}"
{{ 'loading="lazy"'|safe if lazy }}
class="{{ (class_names + ['texture'])|join(' ') }}"
>
{% endif %}
{%- endmacro %}

{# display a single item, with a badge if the count is greater than 1 #}
{% macro render_item(item, is_first=true, count=1) -%}
{% if item.gaslighting %}
<div class="texture item-texture multi-textures gaslight-textures {{ 'multi-texture-active' if is_first }}">
{% for texture in item.textures %}
{{
render_texture(
name=item.name,
texture=texture,
class_names=[
"item-texture",
"multi-texture-active" if is_first,
],
)
}}
{% endfor %}
</div>
{% else %}
{{
render_texture(
name=item.name,
texture=item.texture,
class_names=[
"item-texture",
"multi-texture-active" if is_first,
],
)
}}
{% endif %}
{% if count > 1 %}
<div class="badge">{{ count }}</div>
{% endif %}
{%- endmacro %}

{# render a list of recipe ingredients #}
{% macro render_ingredients(ingredients, is_recursive=false) -%}
{% for ingredient in ingredients %}
Expand All @@ -71,7 +20,7 @@
{{ render_ingredients(ingredient.default, true) }}
{{ render_ingredients(ingredient.if_loaded, true) }}
{% else %}
{{ render_item(ingredient.item, is_first=loop.first and not is_recursive) }}
{{ texture_macros.render_item(ingredient.item, is_first=loop.first and not is_recursive) }}
{% endif %}
{% endfor %}
{%- endmacro %}
Expand Down Expand Up @@ -106,7 +55,7 @@
</div>

<div class="crafting-table-result">
{{ render_item(recipe.result.item, count=recipe.result.count) }}
{{ texture_macros.render_item(recipe.result.item, count=recipe.result.count) }}
</div>
</div>
</div>
Expand Down
66 changes: 66 additions & 0 deletions src/hexdoc/_templates/macros/textures.html.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{% macro render_icon(item_or_texture, name="") -%}
{% if item_or_texture.gaslighting is defined %}
{{ render_item(item_or_texture) }}
{% else %}
{{
render_texture(
name=name,
texture=item_or_texture,
class_names=[],
)
}}
{% endif %}
{%- endmacro %}

{% macro render_texture(name, texture, class_names=[], lazy=true) -%}
{% if texture.meta is defined %}
<div
role="img"
title="{{ name }}"
aria-label="Animated image of {{ name }}"
class="{{ (class_names + ['texture', 'animated-sync', texture.class_name])|join(' ') }}"
></div>
{% else %}
<img
title="{{ name }}"
alt="Image of {{ name }}"
src="{{ texture.url }}"
{{ 'loading="lazy"'|safe if lazy }}
class="{{ (class_names + ['texture'])|join(' ') }}"
>
{% endif %}
{%- endmacro %}

{# display a single item, with a badge if the count is greater than 1 #}
{% macro render_item(item, is_first=true, count=1) -%}
{% if item.gaslighting %}
<div class="texture item-texture multi-textures gaslight-textures {{ 'multi-texture-active' if is_first }}">
{% for texture in item.textures %}
{{
render_texture(
name=item.name,
texture=texture,
class_names=[
"item-texture",
"multi-texture-active" if is_first,
],
)
}}
{% endfor %}
</div>
{% else %}
{{
render_texture(
name=item.name,
texture=item.texture,
class_names=[
"item-texture",
"multi-texture-active" if is_first,
],
)
}}
{% endif %}
{% if count > 1 %}
<div class="badge">{{ count }}</div>
{% endif %}
{%- endmacro %}
4 changes: 2 additions & 2 deletions src/hexdoc/_templates/pages/patchouli/image.html.jinja
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% extends "pages/patchouli/text.html.jinja" %}
{% import "macros/recipes.html.jinja" as recipe_macros with context %}
{% import "macros/textures.html.jinja" as texture_macros with context %}

{% block inner_body %}
<p class="img-wrapper">
{% for image in page.images %}
{{
recipe_macros.render_texture(
texture_macros.render_texture(
name=page.title or image.file_id,
texture=image,
lazy=false,
Expand Down
4 changes: 2 additions & 2 deletions src/hexdoc/_templates/pages/patchouli/spotlight.html.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "pages/patchouli/text.html.jinja" %}
{% import "macros/recipes.html.jinja" as recipe_macros with context %}
{% import "macros/textures.html.jinja" as texture_macros with context -%}

{% block inner_body %}
<h4 class="spotlight-title page-header">{{ page.item.name }}</h4>
Expand All @@ -8,7 +8,7 @@
alt="Spotlight inventory slot"
src="{{ 'hexdoc:textures/gui/spotlight.png'|hexdoc_texture }}"
>
{{ recipe_macros.render_item(page.item) }}
{{ texture_macros.render_item(page.item) }}
</div>
{{ super() }}
{% endblock inner_body %}

0 comments on commit 76c7d10

Please sign in to comment.