Skip to content

Commit

Permalink
m.htmlsanity: add expand_link[s] Jinja2 filters.
Browse files Browse the repository at this point in the history
This depends on part of getpelican/pelican#2164
(in particular having the _link_expander() function), everything else
from the PR is implemented here.
  • Loading branch information
mosra committed Oct 11, 2017
1 parent ae325b1 commit 32c9cb8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pelican-plugins/m/htmlsanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
smart_quotes = False
hyphenation_lang = 'en'
docutils_settings = {}
intrasite_link_regex = ''

words_re = re.compile("""\w+""", re.UNICODE|re.X)

Expand Down Expand Up @@ -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<markup>)(?P<quote>)
(?P<path>{0}(?P<value>.*))
$""".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
Expand Down

0 comments on commit 32c9cb8

Please sign in to comment.