From: Vladimír Vondruš Date: Tue, 19 Sep 2017 18:56:19 +0000 (+0200) Subject: m.htmlsanity: add expand_link[s] Jinja2 filters. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=32c9cb8dcea1cfb4859d4524b91d061ac2d16ff3;p=blog.git m.htmlsanity: add expand_link[s] Jinja2 filters. This depends on part of https://github.com/getpelican/pelican/pull/2164 (in particular having the _link_expander() function), everything else from the PR is implemented here. --- diff --git a/pelican-plugins/m/htmlsanity.py b/pelican-plugins/m/htmlsanity.py index ac297e94..fbfd3c9b 100644 --- a/pelican-plugins/m/htmlsanity.py +++ b/pelican-plugins/m/htmlsanity.py @@ -20,6 +20,7 @@ enable_hyphenation = False smart_quotes = False hyphenation_lang = 'en' docutils_settings = {} +intrasite_link_regex = '' words_re = re.compile("""\w+""", re.UNICODE|re.X) @@ -514,16 +515,33 @@ def dehyphenate(value, enable=None): if not enable: return value return value.replace('­', '') +def expand_link(link, content): + link_regex = r"""^ + (?P)(?P) + (?P{0}(?P.*)) + $""".format(intrasite_link_regex) + links = re.compile(link_regex, re.X) + return links.sub( + lambda m: content._link_replacer(content.get_siteurl(), m), + link) + +def expand_links(text, content): + return content._update_content(content, text, content.get_siteurl()) + def configure_pelican(pelicanobj): pelicanobj.settings['JINJA_FILTERS']['render_rst'] = render_rst + pelicanobj.settings['JINJA_FILTERS']['expand_link'] = expand_link + pelicanobj.settings['JINJA_FILTERS']['expand_links'] = expand_links pelicanobj.settings['JINJA_FILTERS']['hyphenate'] = hyphenate pelicanobj.settings['JINJA_FILTERS']['dehyphenate'] = dehyphenate - global enable_hyphenation, smart_quotes, hyphenation_lang, docutils_settings + global enable_hyphenation, smart_quotes, hyphenation_lang, \ + docutils_settings, intrasite_link_regex enable_hyphenation = pelicanobj.settings.get('HTMLSANITY_HYPHENATION', False) smart_quotes = pelicanobj.settings.get('HTMLSANITY_SMART_QUOTES', False) hyphenation_lang = pelicanobj.settings['DEFAULT_LANG'] docutils_settings = pelicanobj.settings['DOCUTILS_SETTINGS'] + intrasite_link_regex = pelicanobj.settings['INTRASITE_LINK_REGEX'] def add_reader(readers): readers.reader_classes['rst'] = SaneRstReader