aboutsummaryrefslogtreecommitdiff
path: root/templates/components/img.html
blob: 6aa5b2ea06d98a883cf63584190ff6f4be179834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{% component img(page, config, src, caption="", class="", alt="", w=0, h=0, link="") %}
{%- set img = src | split(pat="/") | last %}
{%- set dirtest = src | trim_start(pat="./") %}
{%- if dirtest is containing(pat="/") %} {%- set reldir = true %} {%- endif %}
{%- if page and page.path %}{%- set pagepath = page.path %}{%- elif section and section.path %}{%- set pagepath = section.path %}{%- endif %}

{%- if src is starting_with(pat="./") and reldir %} {# Relative Path #}
  {%- set path = (src | trim_start(pat="./") | split(pat="/"))[:-1] | join(sep="/") | trim_start(pat="/") -%}
  {%- set path = path ~ "/" %}
  {%- if not w or not h %}
    {%- if page.lang != config.default_language %}
      {%- set metapath = (pagepath ~ path ~ img) | replace(from="/" ~ page.lang ~ "/", to="/") %}
    {%- else %}
      {%- set metapath = pagepath ~ path ~ img %}
    {%- endif %}
    {%- set meta = get_image_metadata(path=metapath) %}
    {%- set w = meta.width %}
    {%- set h = meta.height %}
  {%- endif %}

{%- elif src is starting_with(pat="/") %} {# Root Path #}
  {%- set rootpath = (src | split(pat="/"))[:-1] | join(sep="/") -%}
  {%- set path = (config.base_url if config else "") ~ rootpath ~ "/"  %}
  {%- if not w or not h %}
    {%- set metapath = rootpath ~ "/" ~ img %}
    {%- set meta = get_image_metadata(path=metapath) %}
    {%- set w = meta.width %}
    {%- set h = meta.height %}
  {%- endif %}

{%- else %} {# Same Directory Path #}
  {%- set path = "" %}
  {%- if not w or not h %}
    {%- if page.lang != config.default_language %}
      {%- set metapath = (pagepath ~ img) | replace(from="/" ~ page.lang ~ "/", to="/") %}
    {%- else %}
      {%- set metapath = pagepath ~ img %}
    {%- endif %}
    {%- set meta = get_image_metadata(path=metapath) %}
    {%- set w = meta.width %}
    {%- set h = meta.height %}
  {%- endif %}
{%- endif %}

{%- if not alt %}
  {%- set alt = img | split(pat=".") | first %}
{%- endif %}
{%- if caption %}<figure class="fi{% if class %} {{class}}{% endif %}">{% endif  %}
{%- if link %}<a href="{{link | safe}}">{% endif %}<img src="{{ path | safe }}{{ img | safe }}"{% if class %} class="{{class}}"{% endif %}{% if alt %} alt="{{alt}}"{% endif %}{% if w %} width="{{w}}"{% endif %}{% if h %} height="{{h}}"{% endif %} loading="lazy" />{% if link %}</a>{% endif  %}
{%- if caption %}<figcaption>{{ caption | safe }}</figcaption></figure>{%- endif %}

{% endcomponent img %}