.. role:: html(code)
:language: html
-
+.. role:: jinja(code)
+ :language: jinja
.. role:: py(code)
:language: py
:legal: This article is released under `CC0 {filename}/license.rst`_.
:cover: {filename}/img/article-cover.jpg
+`SITEURL formatting`_
+---------------------
+
+Convenience filter replacing the common expression :jinja:`{{ SITEURL }}/{{ page.url }}`
+with a formatter that makes use of `urljoin <https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin>`_ so it does the right thing also when
+dealing with absolute URLs and even when they start with just ``//``:
+
+.. code:: jinja
+
+ {{ page.url|format_siteurl }}
+
`Text hyphenation`_
-------------------
import re
import six
-from six.moves.urllib.parse import urlparse, urlunparse
+from six.moves.urllib.parse import urlparse, urlunparse, urljoin
from docutils.writers.html5_polyglot import HTMLTranslator
from docutils.transforms import Transform
hyphenation_lang = 'en'
docutils_settings = {}
intrasite_link_regex = ''
+siteurl = ''
words_re = re.compile("""\w+""", re.UNICODE|re.X)
def expand_links(text, content):
return content._update_content(text, content.get_siteurl())
+# To be consistent with both what Pelican does now with '/'.join(SITEURL, url)
+# and with https://github.com/getpelican/pelican/pull/2196
+def format_siteurl(url):
+ return urljoin(siteurl + ('/' if not siteurl.endswith('/') else ''), url)
+
def configure_pelican(pelicanobj):
pelicanobj.settings['JINJA_FILTERS']['render_rst'] = render_rst
pelicanobj.settings['JINJA_FILTERS']['expand_links'] = expand_links
+ pelicanobj.settings['JINJA_FILTERS']['format_siteurl'] = format_siteurl
pelicanobj.settings['JINJA_FILTERS']['hyphenate'] = hyphenate
pelicanobj.settings['JINJA_FILTERS']['dehyphenate'] = dehyphenate
pelicanobj.settings['JINJA_FILTERS']['expand_link'] = expand_link
global enable_hyphenation, smart_quotes, hyphenation_lang, \
- docutils_settings, intrasite_link_regex
+ docutils_settings, intrasite_link_regex, siteurl
enable_hyphenation = pelicanobj.settings.get('M_HTMLSANITY_HYPHENATION', False)
smart_quotes = pelicanobj.settings.get('M_HTMLSANITY_SMART_QUOTES', False)
hyphenation_lang = pelicanobj.settings['DEFAULT_LANG']
docutils_settings = pelicanobj.settings['DOCUTILS_SETTINGS']
intrasite_link_regex = pelicanobj.settings['INTRASITE_LINK_REGEX']
+ siteurl = pelicanobj.settings['SITEURL']
def add_reader(readers):
# TODO: remove when 3.8 with https://github.com/getpelican/pelican/pull/2163
{% block head %}
{{- super() -}}
{% if articles_page.has_previous() %}
- <link rel="prev" href="{{ articles_previous_page.url }}" />
+ <link rel="prev" href="{{ articles_previous_page.url|format_siteurl }}" />
{% endif %}
{% if articles_page.has_next() %}
- <link rel="next" href="{{ articles_next_page.url }}" />
+ <link rel="next" href="{{ articles_next_page.url|format_siteurl }}" />
{% endif %}
{% endblock head %}
<meta name="keywords" content="{{ article.tags|join(", ") }}" />
<meta property="og:title" content="{{ article.title }}" />
<meta name="twitter:title" content="{{ article.title }}" />
- <meta property="og:url" content="{{ article.url }}" />
- <meta name="twitter:url" content="{{ article.url }}" />
+ <meta property="og:url" content="{{ article.url|format_siteurl }}" />
+ <meta name="twitter:url" content="{{ article.url|format_siteurl }}" />
<meta property="og:description" content="{{ article.summary|striptags }}" />
<meta name="twitter:description" content="{{ article.summary|striptags }}" />
{% if article.cover %}
<div class="m-container">
<div class="m-row">
<div class="m-col-t-6 m-col-s-5 m-push-s-1 m-text-left">{{ article.locale_date }}</div>
- <div class="m-col-t-6 m-col-s-5 m-push-s-1 m-text-right">{% for author in article.authors %}<a href="{{ author.url }}">{{ author }}</a>{% endfor %}</div>
+ <div class="m-col-t-6 m-col-s-5 m-push-s-1 m-text-right">{% for author in article.authors %}<a href="{{ author.url|format_siteurl }}">{{ author }}</a>{% endfor %}</div>
</div>
<div class="m-row">
<div class="m-col-t-12 m-col-s-10 m-push-s-1 m-col-m-8 m-push-m-2">
{% set title = article.title.split(' — ') %}
- <h1><a href="{{ article.url }}" rel="bookmark" title="Permalink to {{ article.title }}">
+ <h1><a href="{{ article.url|format_siteurl }}" rel="bookmark" title="Permalink to {{ article.title }}">
{{ title[0] }}
</a></h1>
{% if title|length >= 2 %}
<footer class="m-container">
<div class="m-row">
<div class="m-col-m-10 m-push-m-1 m-nopadb">
- <p>Posted {% if article.authors %}by {% for author in article.authors %}<a href="{{ author.url }}">{{ author }}</a>{% endfor %}{% endif %} on <time datetime="{{ article.date.isoformat() }}">{{ article.locale_date }}</time> in <a href="{{ article.category.url }} ">{{ article.category }}</a>.{% if article.tags %} Tags: {% for tag in article.tags %}<a href="{{ tag.url }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}</p>
+ <p>Posted {% if article.authors %}by {% for author in article.authors %}<a href="{{ author.url|format_siteurl }}">{{ author }}</a>{% endfor %}{% endif %} on <time datetime="{{ article.date.isoformat() }}">{{ article.locale_date }}</time> in <a href="{{ article.category.url|format_siteurl }} ">{{ article.category }}</a>.{% if article.tags %} Tags: {% for tag in article.tags %}<a href="{{ tag.url|format_siteurl }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}</p>
</div>
</div>
</footer>
<footer>
- <p>Posted {% if article.authors %}by {% for author in article.authors %}<a href="{{ author.url }}">{{ author }}</a>{% endfor %}{% endif %} on <time datetime="{{ article.date.isoformat() }}">{{ article.locale_date }}</time> in <a href="{{ article.category.url }} ">{{ article.category }}</a>.{% if article.tags %} Tags: {% for tag in article.tags %}<a href="{{ tag.url }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}</p>
+ <p>Posted {% if article.authors %}by {% for author in article.authors %}<a href="{{ author.url|format_siteurl }}">{{ author }}</a>{% endfor %}{% endif %} on <time datetime="{{ article.date.isoformat() }}">{{ article.locale_date }}</time> in <a href="{{ article.category.url|format_siteurl }} ">{{ article.category }}</a>.{% if article.tags %} Tags: {% for tag in article.tags %}<a href="{{ tag.url|format_siteurl }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}</p>
</footer>
<header>
- <h1><a href="{{ article.url }}" rel="bookmark" title="Permalink to {{ article.title|striptags }}">
+ <h1><a href="{{ article.url|format_siteurl }}" rel="bookmark" title="Permalink to {{ article.title|striptags }}">
<time class="m-date" datetime="{{ article.date.isoformat() }}">
{% set month, day, year = article.locale_date.split(' ') %}
{{ month }} <span class="m-date-day">{{ day }}</span> {{year}}
{% block content_title %}
<div class="m-info m-note">
- Showing only posts by <em>{{ author }}</em>. <a href="{{ BLOGURL }}/">Show all posts.</a>
+ Showing only posts by <em>{{ author }}</em>. <a href="{{ BLOGURL|format_siteurl }}">Show all posts.</a>
</div>
{% endblock %}
{% endif %}{% endfor %}
</ul>
{% else %}
- <h3><a href="{{ BLOGURL }}">Blog</a></h3>
+ <h3><a href="{{ BLOGURL|format_siteurl }}">Blog</a></h3>
<ul>
{% for cat, null in categories %}
- <li><a href="{{ cat.url }}">{{ cat }}</a></li>
+ <li><a href="{{ cat.url|format_siteurl }}">{{ cat }}</a></li>
{% endfor %}
</ul>
{% endif %}
<h3>Cate­gories</h3>
<ol class="m-block-bar-m">
{% for cat, null in categories %}
- <li><a href="{{ cat.url }}">{{ cat }}</a></li>
+ <li><a href="{{ cat.url|format_siteurl }}">{{ cat }}</a></li>
{% endfor %}
</ol>
</div>
<ul class="m-tagcloud">
{% set max_articles_per_tag = tags|map(attribute='1')|map('length')|sort|last %}
{% for tag, articles in tags|sort(attribute='0') %}
- <li class="m-tag-{{ (5*(articles|length)/max_articles_per_tag)|round(0, 'ceil')|int }} "><a href="{{ tag.url }}">{{ tag }}</a></li>
+ <li class="m-tag-{{ (5*(articles|length)/max_articles_per_tag)|round(0, 'ceil')|int }} "><a href="{{ tag.url|format_siteurl }}">{{ tag }}</a></li>
{% endfor %}
</ul>
</div>
<h3>Cate­gories</h3>
<ol class="m-block-bar-m">
{% for cat, null in categories %}
- <li><a href="{{ cat.url }}">{{ cat|hyphenate }}</a></li>
+ <li><a href="{{ cat.url|format_siteurl }}">{{ cat|hyphenate }}</a></li>
{% endfor %}
</ol>
<h3>Tag cloud</h3>
<ul class="m-tagcloud">
{% set max_articles_per_tag = tags|map(attribute='1')|map('length')|sort|last %}
{% for tag, articles in tags|sort(attribute='0') %}
- <li class="m-tag-{{ (5*(articles|length)/max_articles_per_tag)|round(0, 'ceil')|int }} "><a href="{{ tag.url }}">{{ tag }}</a></li>
+ <li class="m-tag-{{ (5*(articles|length)/max_articles_per_tag)|round(0, 'ceil')|int }} "><a href="{{ tag.url|format_siteurl }}">{{ tag }}</a></li>
{% endfor %}
</ul>
</nav>
{% block head %}
{{- super() -}}
{% if articles_page.has_previous() %}
- <link rel="prev" href="{{ articles_previous_page.url }}" />
+ <link rel="prev" href="{{ articles_previous_page.url|format_siteurl }}" />
{% endif %}
{% if articles_page.has_next() %}
- <link rel="next" href="{{ articles_next_page.url }}" />
+ <link rel="next" href="{{ articles_next_page.url|format_siteurl }}" />
{% endif %}
{% endblock head %}
{% block content_title %}
<div class="m-info m-note">
- Showing only posts in <em>{{ category }}</em>. <a href="{{ BLOGURL }}/">Show all posts.</a>
+ Showing only posts in <em>{{ category }}</em>. <a href="{{ BLOGURL|format_siteurl }}">Show all posts.</a>
</div>
{% endblock %}
{% block head %}
{{- super() -}}
{% if articles_page.has_previous() %}
- <link rel="prev" href="{{ articles_previous_page.url }}" />
+ <link rel="prev" href="{{ articles_previous_page.url|format_siteurl }}" />
{% endif %}
{% if articles_page.has_next() %}
- <link rel="next" href="{{ articles_next_page.url }}" />
+ <link rel="next" href="{{ articles_next_page.url|format_siteurl }}" />
{% endif %}
{% endblock head %}
{% endif %}
<meta property="og:title" content="{{ page.title }}" />
<meta name="twitter:title" content="{{ page.title }}" />
- <meta property="og:url" content="{% if page.url %}{{ page.url }}{% else %}{{ SITEURL }}{% endif %}" />
- <meta name="twitter:url" content="{% if page.url %}{{ page.url }}{% else %}{{ SITEURL }}{% endif %}" />
+ <meta property="og:url" content="{% if page.url %}{{ page.url|format_siteurl }}{% else %}{{ SITEURL }}{% endif %}" />
+ <meta name="twitter:url" content="{% if page.url %}{{ page.url|format_siteurl }}{% else %}{{ SITEURL }}{% endif %}" />
<meta property="og:description" content="{{ page.summary|striptags }}" />
<meta name="twitter:description" content="{{ page.summary|striptags }}" />
{% if page.cover %}
{% if DEFAULT_PAGINATION %}
<div class="m-article-pagination">
- {%- if articles_page.has_previous() %}<a href="{{ articles_previous_page.url }}">« newer articles</a> | {% endif -%}
+ {%- if articles_page.has_previous() %}<a href="{{ articles_previous_page.url|format_siteurl }}">« newer articles</a> | {% endif -%}
page {{ articles_page.number }}
- {%- if articles_page.has_next() %} | <a href="{{ articles_next_page.url }}">older articles »</a>{% endif -%}
+ {%- if articles_page.has_next() %} | <a href="{{ articles_next_page.url|format_siteurl }}">older articles »</a>{% endif -%}
</div>
{% endif %}
{% block content_title %}
<div class="m-info m-note">
- Showing only posts tagged <em>{{ tag }}</em>. <a href="{{ BLOGURL }}/">Show all posts.</a>
+ Showing only posts tagged <em>{{ tag }}</em>. <a href="{{ BLOGURL|format_siteurl }}">Show all posts.</a>
</div>
{% endblock %}
SITEURL = ''
BLOGNAME = 'm.css example articles'
-BLOGURL = '/examples'
+BLOGURL = 'examples/'
STATIC_URL = '/{path}'