Skip to content

Commit 76c7d10

Browse files
committed
Refactor macros
1 parent c3222a1 commit 76c7d10

File tree

6 files changed

+78
-77
lines changed

6 files changed

+78
-77
lines changed

src/hexdoc/_templates/macros/formatting.html.jinja

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% import "macros/styles.html.jinja" as styles with context -%}
2-
{% import "macros/recipes.html.jinja" as recipe_macros with context %}
2+
{% import "macros/textures.html.jinja" as texture_macros with context -%}
33

44
{# jump to top icon in section headers #}
55
{% macro jump_to_top() -%}
@@ -22,28 +22,14 @@
2222
{# header for categories and entries #}
2323
{% macro section_header(value, header_tag, class_name) -%}
2424
<{{ header_tag }} class="{{ class_name }} page-header">
25-
{{ render_icon(value.icon) }}
25+
{{ texture_macros.render_icon(value.icon) }}
2626
{{- value.name ~ jump_to_top() ~ permalink(value.id.path) -}}
2727
</{{ header_tag }}>
2828
{%- endmacro %}
2929

30-
{% macro render_icon(item_or_texture, name="") -%}
31-
{% if item_or_texture.gaslighting is defined %}
32-
{{ recipe_macros.render_item(item_or_texture) }}
33-
{% else %}
34-
{{
35-
recipe_macros.render_texture(
36-
name=name,
37-
texture=item_or_texture,
38-
class_names=[],
39-
)
40-
}}
41-
{% endif %}
42-
{%- endmacro%}
43-
4430
{# link to value.id, with spoiler blur if value is a spoiler #}
4531
{% macro maybe_spoilered_link(value) -%}
46-
<a href="#{{ value.id.path }}" class="toc-elem {{ 'spoilered' if value.is_spoiler }}">{{ render_icon(value.icon) }} {{ value.name }}</a>
32+
<a href="#{{ value.id.path }}" class="toc-elem {{ 'spoilered' if value.is_spoiler }}">{{ texture_macros.render_icon(value.icon) }} {{ value.name }}</a>
4733
{%- endmacro %}
4834

4935
{# macro block which spoiler blurs its content if value is a spoiler #}

src/hexdoc/_templates/macros/recipes.html.jinja

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% import "macros/textures.html.jinja" as texture_macros with context -%}
2+
13
{# show the names of all the recipe results in a list of recipes #}
24
{% macro generic(recipes, result_attribute, description, separator) -%}
35
<blockquote class="crafting-info">
@@ -10,59 +12,6 @@
1012
</blockquote>
1113
{%- endmacro %}
1214

13-
{% macro render_texture(name, texture, class_names=[], lazy=true) -%}
14-
{% if texture.meta is defined %}
15-
<div
16-
role="img"
17-
title="{{ name }}"
18-
aria-label="Animated image of {{ name }}"
19-
class="{{ (class_names + ['texture', 'animated-sync', texture.class_name])|join(' ') }}"
20-
></div>
21-
{% else %}
22-
<img
23-
title="{{ name }}"
24-
alt="Image of {{ name }}"
25-
src="{{ texture.url }}"
26-
{{ 'loading="lazy"'|safe if lazy }}
27-
class="{{ (class_names + ['texture'])|join(' ') }}"
28-
>
29-
{% endif %}
30-
{%- endmacro %}
31-
32-
{# display a single item, with a badge if the count is greater than 1 #}
33-
{% macro render_item(item, is_first=true, count=1) -%}
34-
{% if item.gaslighting %}
35-
<div class="texture item-texture multi-textures gaslight-textures {{ 'multi-texture-active' if is_first }}">
36-
{% for texture in item.textures %}
37-
{{
38-
render_texture(
39-
name=item.name,
40-
texture=texture,
41-
class_names=[
42-
"item-texture",
43-
"multi-texture-active" if is_first,
44-
],
45-
)
46-
}}
47-
{% endfor %}
48-
</div>
49-
{% else %}
50-
{{
51-
render_texture(
52-
name=item.name,
53-
texture=item.texture,
54-
class_names=[
55-
"item-texture",
56-
"multi-texture-active" if is_first,
57-
],
58-
)
59-
}}
60-
{% endif %}
61-
{% if count > 1 %}
62-
<div class="badge">{{ count }}</div>
63-
{% endif %}
64-
{%- endmacro %}
65-
6615
{# render a list of recipe ingredients #}
6716
{% macro render_ingredients(ingredients, is_recursive=false) -%}
6817
{% for ingredient in ingredients %}
@@ -71,7 +20,7 @@
7120
{{ render_ingredients(ingredient.default, true) }}
7221
{{ render_ingredients(ingredient.if_loaded, true) }}
7322
{% else %}
74-
{{ render_item(ingredient.item, is_first=loop.first and not is_recursive) }}
23+
{{ texture_macros.render_item(ingredient.item, is_first=loop.first and not is_recursive) }}
7524
{% endif %}
7625
{% endfor %}
7726
{%- endmacro %}
@@ -106,7 +55,7 @@
10655
</div>
10756

10857
<div class="crafting-table-result">
109-
{{ render_item(recipe.result.item, count=recipe.result.count) }}
58+
{{ texture_macros.render_item(recipe.result.item, count=recipe.result.count) }}
11059
</div>
11160
</div>
11261
</div>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{% macro render_icon(item_or_texture, name="") -%}
2+
{% if item_or_texture.gaslighting is defined %}
3+
{{ render_item(item_or_texture) }}
4+
{% else %}
5+
{{
6+
render_texture(
7+
name=name,
8+
texture=item_or_texture,
9+
class_names=[],
10+
)
11+
}}
12+
{% endif %}
13+
{%- endmacro %}
14+
15+
{% macro render_texture(name, texture, class_names=[], lazy=true) -%}
16+
{% if texture.meta is defined %}
17+
<div
18+
role="img"
19+
title="{{ name }}"
20+
aria-label="Animated image of {{ name }}"
21+
class="{{ (class_names + ['texture', 'animated-sync', texture.class_name])|join(' ') }}"
22+
></div>
23+
{% else %}
24+
<img
25+
title="{{ name }}"
26+
alt="Image of {{ name }}"
27+
src="{{ texture.url }}"
28+
{{ 'loading="lazy"'|safe if lazy }}
29+
class="{{ (class_names + ['texture'])|join(' ') }}"
30+
>
31+
{% endif %}
32+
{%- endmacro %}
33+
34+
{# display a single item, with a badge if the count is greater than 1 #}
35+
{% macro render_item(item, is_first=true, count=1) -%}
36+
{% if item.gaslighting %}
37+
<div class="texture item-texture multi-textures gaslight-textures {{ 'multi-texture-active' if is_first }}">
38+
{% for texture in item.textures %}
39+
{{
40+
render_texture(
41+
name=item.name,
42+
texture=texture,
43+
class_names=[
44+
"item-texture",
45+
"multi-texture-active" if is_first,
46+
],
47+
)
48+
}}
49+
{% endfor %}
50+
</div>
51+
{% else %}
52+
{{
53+
render_texture(
54+
name=item.name,
55+
texture=item.texture,
56+
class_names=[
57+
"item-texture",
58+
"multi-texture-active" if is_first,
59+
],
60+
)
61+
}}
62+
{% endif %}
63+
{% if count > 1 %}
64+
<div class="badge">{{ count }}</div>
65+
{% endif %}
66+
{%- endmacro %}

src/hexdoc/_templates/pages/patchouli/image.html.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{% extends "pages/patchouli/text.html.jinja" %}
2-
{% import "macros/recipes.html.jinja" as recipe_macros with context %}
2+
{% import "macros/textures.html.jinja" as texture_macros with context %}
33

44
{% block inner_body %}
55
<p class="img-wrapper">
66
{% for image in page.images %}
77
{{
8-
recipe_macros.render_texture(
8+
texture_macros.render_texture(
99
name=page.title or image.file_id,
1010
texture=image,
1111
lazy=false,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends "pages/patchouli/text.html.jinja" %}
2-
{% import "macros/recipes.html.jinja" as recipe_macros with context %}
2+
{% import "macros/textures.html.jinja" as texture_macros with context -%}
33

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

0 commit comments

Comments
 (0)