From: Vladimír Vondruš Date: Sun, 7 Jul 2019 19:23:35 +0000 (+0200) Subject: m.htmlsanity: add a rtrim Jinja2 filter. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=fcda3db0c1e02d63c7dc508fd305bf2559a5a4db;p=blog.git m.htmlsanity: add a rtrim Jinja2 filter. Argh, I wanted to update to Jina 2.10 without workarounds, but it's not possible. --- diff --git a/doc/plugins/htmlsanity.rst b/doc/plugins/htmlsanity.rst index 39291ea4..f6080e81 100644 --- a/doc/plugins/htmlsanity.rst +++ b/doc/plugins/htmlsanity.rst @@ -254,6 +254,9 @@ option, which also defaults to :py:`False`. `Jinja2 goodies`_ ================= +This plugin adds a ``rtrim`` filter to Jinja. It's like the builtin ``trim``, +but working only on the right side to get rid of excessive newlines at the end. + `reST rendering`_ ----------------- diff --git a/documentation/doxygen.py b/documentation/doxygen.py index fc3703e7..c765f430 100755 --- a/documentation/doxygen.py +++ b/documentation/doxygen.py @@ -3775,6 +3775,8 @@ def run(doxyfile, templates=default_templates, wildcard=default_wildcard, index_ def basename_or_url(path): if urllib.parse.urlparse(path).netloc: return path return os.path.basename(path) + def rtrim(value): return value.rstrip() + env.filters['rtrim'] = rtrim env.filters['basename_or_url'] = basename_or_url env.filters['urljoin'] = urllib.parse.urljoin diff --git a/plugins/m/htmlsanity.py b/plugins/m/htmlsanity.py index 0f91dcab..fc36ce69 100644 --- a/plugins/m/htmlsanity.py +++ b/plugins/m/htmlsanity.py @@ -690,6 +690,9 @@ def render_rst(value): pub.publish(enable_exit_status=True) return pub.writer.parts.get('body').strip() +def rtrim(value): + return value.rstrip() + def hyphenate(value, enable=None, lang=None): if enable is None: enable = settings['M_HTMLSANITY_HYPHENATION'] if lang is None: lang = settings['M_HTMLSANITY_LANGUAGE'] @@ -710,6 +713,7 @@ def register_mcss(mcss_settings, jinja_environment, **kwargs): docutils_settings['language_code'] = settings['M_HTMLSANITY_LANGUAGE'] docutils_settings.update(settings['M_HTMLSANITY_DOCUTILS_SETTINGS']) + jinja_environment.filters['rtrim'] = rtrim jinja_environment.filters['render_rst'] = render_rst jinja_environment.filters['hyphenate'] = hyphenate jinja_environment.filters['dehyphenate'] = dehyphenate @@ -751,6 +755,7 @@ def pelican_format_siteurl(url): return urljoin(pelican_settings['SITEURL'] + ('/' if not pelican_settings['SITEURL'].endswith('/') else ''), url) def _pelican_configure(pelicanobj): + pelicanobj.settings['JINJA_FILTERS']['rtrim'] = rtrim pelicanobj.settings['JINJA_FILTERS']['render_rst'] = render_rst pelicanobj.settings['JINJA_FILTERS']['expand_link'] = pelican_expand_link pelicanobj.settings['JINJA_FILTERS']['expand_links'] = pelican_expand_links