Link pour Liquid Tags

Liquid Tags

Le CMS Pelican supporte des extensions. Liquid Tags est l'une d'elles. Cette extension est ce qui se rapproche le plus des "actions" utilisées dans un wiki tel que WikkaWiki.

Link est un tag qui permet de définir le rendu d'un hyperlien en fonction d'un type - une classe CSS, en fait - passé en paramètre.

Exemple de lien:

{% link {static}../code/link.py "Code source" "download" "Télécharger le fichier source" %}

Code source

"""
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