From 9476408bc63a4496d5643a7d91f30155faf7f0fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 24 Oct 2017 18:23:08 +0200 Subject: [PATCH] m.htmlsanity: introduce and use new format_siteurl filter. I needed a way to prepend SITEURL to page, article, tag etc URLs that's consistent with current (wrong) Pelican way of doing it: '/'.join([SITEURL, url]) That of course doesn't work when `url` is an absolute URL, so I'm using urljoin (but still staying compatible with the above). This is now also used throughout the template -- I mistakenly assumed that when SITEURL is set, all page.url etc. properties already have it, but that's apparently *not* the case, which caused me a bit of headache the past months. --- doc/plugins/htmlsanity.rst | 14 +++++++++++++- pelican-plugins/m/htmlsanity.py | 12 ++++++++++-- pelican-theme/templates/archives.html | 4 ++-- pelican-theme/templates/article.html | 10 +++++----- pelican-theme/templates/article_footer.html | 2 +- pelican-theme/templates/article_header.html | 2 +- pelican-theme/templates/author.html | 2 +- pelican-theme/templates/base.html | 4 ++-- pelican-theme/templates/base_blog.html | 8 ++++---- pelican-theme/templates/base_blog_section.html | 4 ++-- pelican-theme/templates/category.html | 2 +- pelican-theme/templates/index.html | 4 ++-- pelican-theme/templates/page.html | 4 ++-- pelican-theme/templates/pagination.html | 4 ++-- pelican-theme/templates/tag.html | 2 +- site/pelicanconf.py | 2 +- 16 files changed, 50 insertions(+), 30 deletions(-) diff --git a/doc/plugins/htmlsanity.rst b/doc/plugins/htmlsanity.rst index 09d142bb..a2fb2977 100644 --- a/doc/plugins/htmlsanity.rst +++ b/doc/plugins/htmlsanity.rst @@ -29,7 +29,8 @@ HTML sanity .. role:: html(code) :language: html - +.. role:: jinja(code) + :language: jinja .. role:: py(code) :language: py @@ -275,6 +276,17 @@ making use of both fields could look like this: :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 `_ 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`_ ------------------- diff --git a/pelican-plugins/m/htmlsanity.py b/pelican-plugins/m/htmlsanity.py index 77fb7053..6bf4f318 100644 --- a/pelican-plugins/m/htmlsanity.py +++ b/pelican-plugins/m/htmlsanity.py @@ -26,7 +26,7 @@ import os.path 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 @@ -53,6 +53,7 @@ smart_quotes = False hyphenation_lang = 'en' docutils_settings = {} intrasite_link_regex = '' +siteurl = '' words_re = re.compile("""\w+""", re.UNICODE|re.X) @@ -697,9 +698,15 @@ def expand_link_pelican371(link, content): 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 @@ -712,12 +719,13 @@ def configure_pelican(pelicanobj): 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 diff --git a/pelican-theme/templates/archives.html b/pelican-theme/templates/archives.html index 08589117..ded6964b 100644 --- a/pelican-theme/templates/archives.html +++ b/pelican-theme/templates/archives.html @@ -3,10 +3,10 @@ {% block head %} {{- super() -}} {% if articles_page.has_previous() %} - + {% endif %} {% if articles_page.has_next() %} - + {% endif %} {% endblock head %} diff --git a/pelican-theme/templates/article.html b/pelican-theme/templates/article.html index a9be63f1..0a00ee67 100644 --- a/pelican-theme/templates/article.html +++ b/pelican-theme/templates/article.html @@ -10,8 +10,8 @@ - - + + {% if article.cover %} @@ -33,12 +33,12 @@
{{ article.locale_date }}
-
{% for author in article.authors %}{{ author }}{% endfor %}
+
{% for author in article.authors %}{{ author }}{% endfor %}
{% set title = article.title.split(' — ') %} -

+

{{ title[0] }}

{% if title|length >= 2 %} @@ -69,7 +69,7 @@
-

Posted {% if article.authors %}by {% for author in article.authors %}{{ author }}{% endfor %}{% endif %} on in {{ article.category }}.{% if article.tags %} Tags: {% for tag in article.tags %}{{ tag }}{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}

+

Posted {% if article.authors %}by {% for author in article.authors %}{{ author }}{% endfor %}{% endif %} on in {{ article.category }}.{% if article.tags %} Tags: {% for tag in article.tags %}{{ tag }}{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}

diff --git a/pelican-theme/templates/article_footer.html b/pelican-theme/templates/article_footer.html index b69bad59..0b31bef7 100644 --- a/pelican-theme/templates/article_footer.html +++ b/pelican-theme/templates/article_footer.html @@ -1,3 +1,3 @@
-

Posted {% if article.authors %}by {% for author in article.authors %}{{ author }}{% endfor %}{% endif %} on in {{ article.category }}.{% if article.tags %} Tags: {% for tag in article.tags %}{{ tag }}{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}

+

Posted {% if article.authors %}by {% for author in article.authors %}{{ author }}{% endfor %}{% endif %} on in {{ article.category }}.{% if article.tags %} Tags: {% for tag in article.tags %}{{ tag }}{% if not loop.last %}, {% endif %}{% endfor %}.{% endif %}

diff --git a/pelican-theme/templates/article_header.html b/pelican-theme/templates/article_header.html index 626ab8f3..cc3be45e 100644 --- a/pelican-theme/templates/article_header.html +++ b/pelican-theme/templates/article_header.html @@ -1,5 +1,5 @@
-

+

{% endblock %} diff --git a/pelican-theme/templates/base.html b/pelican-theme/templates/base.html index 8124b170..b3007125 100644 --- a/pelican-theme/templates/base.html +++ b/pelican-theme/templates/base.html @@ -127,10 +127,10 @@ {% endif %}{% endfor %} {% else %} -

Blog

+

Blog

{% endif %} diff --git a/pelican-theme/templates/base_blog.html b/pelican-theme/templates/base_blog.html index 597c88d4..6e9dbb8c 100644 --- a/pelican-theme/templates/base_blog.html +++ b/pelican-theme/templates/base_blog.html @@ -18,7 +18,7 @@

Cate­gories

    {% for cat, null in categories %} -
  1. {{ cat }}
  2. +
  3. {{ cat }}
  4. {% endfor %}

@@ -27,7 +27,7 @@
    {% set max_articles_per_tag = tags|map(attribute='1')|map('length')|sort|last %} {% for tag, articles in tags|sort(attribute='0') %} -
  • {{ tag }}
  • +
  • {{ tag }}
  • {% endfor %}
@@ -38,14 +38,14 @@

Cate­gories

    {% for cat, null in categories %} -
  1. {{ cat|hyphenate }}
  2. +
  3. {{ cat|hyphenate }}
  4. {% endfor %}

Tag cloud

    {% set max_articles_per_tag = tags|map(attribute='1')|map('length')|sort|last %} {% for tag, articles in tags|sort(attribute='0') %} -
  • {{ tag }}
  • +
  • {{ tag }}
  • {% endfor %}
diff --git a/pelican-theme/templates/base_blog_section.html b/pelican-theme/templates/base_blog_section.html index 1dfaf350..8072d378 100644 --- a/pelican-theme/templates/base_blog_section.html +++ b/pelican-theme/templates/base_blog_section.html @@ -3,10 +3,10 @@ {% block head %} {{- super() -}} {% if articles_page.has_previous() %} - + {% endif %} {% if articles_page.has_next() %} - + {% endif %} {% endblock head %} diff --git a/pelican-theme/templates/category.html b/pelican-theme/templates/category.html index 3b91c063..22e71c51 100644 --- a/pelican-theme/templates/category.html +++ b/pelican-theme/templates/category.html @@ -4,6 +4,6 @@ {% block content_title %}
- Showing only posts in {{ category }}. Show all posts. + Showing only posts in {{ category }}. Show all posts.
{% endblock %} diff --git a/pelican-theme/templates/index.html b/pelican-theme/templates/index.html index 08589117..ded6964b 100644 --- a/pelican-theme/templates/index.html +++ b/pelican-theme/templates/index.html @@ -3,10 +3,10 @@ {% block head %} {{- super() -}} {% if articles_page.has_previous() %} - + {% endif %} {% if articles_page.has_next() %} - + {% endif %} {% endblock head %} diff --git a/pelican-theme/templates/page.html b/pelican-theme/templates/page.html index cd7d7172..f3dd08c8 100644 --- a/pelican-theme/templates/page.html +++ b/pelican-theme/templates/page.html @@ -20,8 +20,8 @@ {% endif %} - - + + {% if page.cover %} diff --git a/pelican-theme/templates/pagination.html b/pelican-theme/templates/pagination.html index c62e8994..da0ae460 100644 --- a/pelican-theme/templates/pagination.html +++ b/pelican-theme/templates/pagination.html @@ -1,7 +1,7 @@ {% if DEFAULT_PAGINATION %}
- {%- if articles_page.has_previous() %}« newer articles | {% endif -%} + {%- if articles_page.has_previous() %}« newer articles | {% endif -%} page {{ articles_page.number }} - {%- if articles_page.has_next() %} | older articles »{% endif -%} + {%- if articles_page.has_next() %} | older articles »{% endif -%}
{% endif %} diff --git a/pelican-theme/templates/tag.html b/pelican-theme/templates/tag.html index 1dd694c1..9a81fad1 100644 --- a/pelican-theme/templates/tag.html +++ b/pelican-theme/templates/tag.html @@ -4,6 +4,6 @@ {% block content_title %}
- Showing only posts tagged {{ tag }}. Show all posts. + Showing only posts tagged {{ tag }}. Show all posts.
{% endblock %} diff --git a/site/pelicanconf.py b/site/pelicanconf.py index 29eeba81..b01d4c1b 100644 --- a/site/pelicanconf.py +++ b/site/pelicanconf.py @@ -30,7 +30,7 @@ SITENAME = 'm.css' SITEURL = '' BLOGNAME = 'm.css example articles' -BLOGURL = '/examples' +BLOGURL = 'examples/' STATIC_URL = '/{path}' -- 2.30.2