{# truncate words my number of words #}
{# http://craftsnippets.com/articles/truncating-text-with-twig-macros-in-craft-cms #}
{# v1 #}
{% macro truncateWords(text, length, suffix) %}
{% spaceless %}
{# settings #}
{% set defaultSuffix = '...' %}
{# logic #}
{% if text and length %}
{% set suffix = suffix|default(defaultSuffix) %}
{% set text = text|striptags %}
{% set text = text|replace('/\\n/', '\n ') %}
{% set array = text|split(' ') %}
{% set arrayTruncated = array|slice(0, length) %}
{% set string = arrayTruncated|join(' ') %}
{% if array|length > length %}
    {% set string = string ~ suffix %}
{% endif %}
{% set string = string|replace('/\\n\\s/', '\n') %}
{{ string }}
{% endif %}
{% endspaceless %}
{% endmacro %}



{# truncate text by number of characters #}
{# http://craftsnippets.com/articles/truncating-text-with-twig-macros-in-craft-cms #}
{# v1 #}
{% macro truncateChars(text, limit, suffix) %}
{% spaceless %}
{# settings #}
{% set defaultSuffix = '...' %}
{# logic #}
{% if text and limit %}
{% set text = text|striptags %}
{% set suffix = suffix|default(defaultSuffix) %}
{% set stringy = create(
    "Stringy\\Stringy",
    [text]
) %}
{{ stringy.safeTruncate(limit, suffix) }}
{% endif %}
{% endspaceless %}
{% endmacro %}



{# turns email in strings into links and secure them from spambots #}
{# http://craftsnippets.com/articles/converting-email-addresses-into-links-using-twig-macro #}
{# v1 #}
{% macro emailLinks(text, class) %}
{% spaceless %}
{% set text = text|replace('/([a-zA-Z0-9_.+-]+)+@([a-zA-Z0-9-]+.[a-zA-Z]+)/', '<a ' ~ (class ? 'class="' ~ class ~ '" ') ~ 'href="mailto:'~ '\\1&#64;\\2' ~'">'~ '\\1&#64;\\2' ~'</a>') %}
{{text|raw}}
{% endspaceless %}
{% endmacro %}


{# macro for formatting dates #}
{# http://craftsnippets.com/articles/working-with-dates-in-craft-cms-templates #}
{%- macro defaultDateFormat(date, additionalAttributes) -%}
{# v1 #}
{% if date is defined %}
{# settings #}
{% set format = 'medium' %}
{# logic #}
{% set attributes = {
	text: date|date(format),
	datetime: date|date('yy-m-d')
} %}
{% if additionalAttributes is defined and additionalAttributes is not iterable %}
	{% set attributes = attributes|merge({class: additionalAttributes}) %}
{% elseif additionalAttributes is iterable %}
	{% set attributes = attributes|merge(additionalAttributes) %}
{% endif %}
{{tag('time',attributes)}}
{% endif %}
{%- endmacro -%}


{# returns time elapsed since specific date in human readable format #}
{# http://craftsnippets.com/articles/working-with-dates-in-craft-cms-templates #}
{%- macro timeAgo(date, additionalAttributes) -%}
{# v1 #}
{% if date is defined %}
{# settings #}
{% set format = 'medium' %}
{% set locale = currentSite.language %}
{# logic #}
{% set formatter = create({ class: 'craft\\i18n\\Formatter', locale: locale }) %}
{% set attributes = {
	text: formatter.asRelativeTime(date),
	datetime: date|date('yy-m-d'),
	title: date|date(format),
} %}
{% if additionalAttributes is defined and additionalAttributes is not iterable %}
	{% set attributes = attributes|merge({class: additionalAttributes}) %}
{% elseif additionalAttributes is iterable %}
	{% set attributes = attributes|merge(additionalAttributes) %}
{% endif %}
{{tag('time',attributes)}}
{% endif %}
{%- endmacro -%}


{# loads svg image from web root #}
{# http://craftsnippets.com/articles/working-with-svg-images-in-craft-cms-templates #}
{# v2 #}
{%- macro svg(path, attributes = null, alt = null) -%}
{# settings #}
{% set directory = 'svg' %}
{# logic #}
{% if path is defined and path is not empty %}
{% set svg = svg('@webroot/'~directory~'/'~path~'.svg', namespace=true) %}
{% if alt %}
	{% set svg = svg|prepend('<title>'~alt~'</title>') %}
{% endif %}
{% if attributes and attributes is not iterable %}
	{% set svg = svg|attr({class: attributes}) %}
{% elseif attributes and attributes is iterable %}
	{% set svg = svg|attr(attributes) %}
{% endif %}
{{svg|raw}}
{% endif %}
{%- endmacro -%}