Link for Liquid Tags

Liquid Tags

PelicanCMS supports plugins. Liquid Tags is one of them. This plugin is what can be considered as actions for a wiki like WikkaWiki.

Link is a tag that enables to define the rendering style of an hyperlink according to a type - a CSS class, in fact - given as a parameter.

Link example:

{% link {static}../code/link.py "Source code" "download" "Download the source file" %}

Source code

"""
Link Tag
---------
This implements a Liquid-style link tag for Pelican,
based on the Liquid_tags / b64img tag

Syntax
------
{%link [http[s]:/]/path/to/file text | ["text" ["icon-type"] ["title"]] %}

Example
-------
{% link https://path/to/filename %}
{% link https://path/to/filename "Click" %}
{% link https://path/to/filename "Click" "download" %}
{% link https://path/to/filename "Click" "download" "This is a download link" %}

Add CSS rules:

[class^="icon-"]:before, [class*=" icon-"]:before {
    font-family:entypo;
    font-weight: normal;
    font-style: normal;
    margin-left: .5em;
}
.icon-download:before {
    content:'\1f4e5';
}
.icon-link:before {
    content:'\1F517';
}
.icon-mail:before {
    content:'\2709';
}

Output
------
<div>
    <a class="button" href="https://path/to/filename" rel="external norefferer noopener" target="_blank" title="Link">
        <div class="inset-text">Click<i class="icon-download"></i></div>
    </a>
</div>
"""
import re
from .mdx_liquid_tags import LiquidTags

SYNTAX = '{% link [http[s]:/]/path/to/file text | ["text" ["icon-type"] ["title"]] %}'

# Regular expression to match the entire syntax
ReLnk = re.compile(ur"""(?P<class>\S.*\s+)?(?P<src>(?:https?:\/\/|\/|\S+\/)\S+)(?P<text>\s+.+)?""", re.UNICODE)

# Regular expression to split the title text and icon type
ReTextType = re.compile(ur"""(?:")(?P<text>[^"]+)?(?:")\s+(?:")(?P<type>[^"]+)?(?:")\s*(?:")?(?P<title>[^"]+)?(?:")?""", re.UNICODE)

@LiquidTags.register('link')
def link(preprocessor, tag, markup):
    attrs = None
    rel = 'external '

    match = ReLnk.search(markup)

    if match:
        attrs = dict([(key, val.strip()) for (key, val) in match.groupdict().items() if val])
    else:
        raise ValueError('Error processing input. '
                         'Expected syntax: {0}'.format(SYNTAX))

    if 'text' in attrs:
        match = ReTextType.search(attrs['text'])
        if match:
            attrs.update(match.groupdict())
        if not attrs.get('type'):
            attrs['type'] = None
        if not attrs.get('title'):
           attrs['title'] = None

    text = attrs['text'] or 'LINK'
    link_url = attrs['src'] or 'none'
    type = attrs['type'] or 'link'
    title = attrs['title'] or 'Link'

    if 'mail' == type:
        rel = ''

    if link_url:
        link_out = u"""
            <span>
                <a class="button" href="{link_url}" rel="{rel}noreferrer noopener" target="_blank" title="{title:s}">
                    <span class="inset-text">{text}<i class="icon-{type}"></i></span>
                </a>
            </span>
        """.format(link_url=link_url, rel=rel, title=title, text=text, type=type).strip()
    else:
        raise ValueError("Error processing input, expected syntax: {0} {1} {2} {3} {4}".format(SYNTAX, link_url, text, type, title))

    return link_out


# ---------------------------------------------------
# This import allows image tag to be a Pelican plugin 
from liquid_tags import register  # noqa
Links
social