{%- macro ref(slug, abs=False) -%}
{% set path, sep, fragment = slug.strip("/").partition("#") %}
{% set ns = namespace(found=false) %}
{% set pathpart = "/{}".format(path) %}
{%- for page in site.pages if page.url.endswith(pathpart) or page.source.path.endswith(pathpart) -%}
    {%- if ns.found -%}
        {{- raise("cannot uniquely resolve ref to {}".format(slug)) -}}
    {%- endif -%}

    {% set ns.found = true %}
    {% if abs %}{{ page.url }}{% else %}{{ page.relurl }}{% endif %}{{ sep }}{{ fragment }}
{%- endfor -%}
{%- if not ns.found -%}
    {{- raise("ref to {} not found".format(slug)) -}}
{%- endif -%}
{%- endmacro -%}

{%- macro figure(src, class=None, alt="") -%}
<figure{%- if class %} class="{{ class }}"{% endif %}>
    <img src="{{ src }}"{%- if alt %} alt="{{ alt }}"{% endif %} />
</figure>
{%- endmacro -%}
