diff --git a/lib/strip_toc_summary.py b/lib/strip_toc_summary.py new file mode 100644 index 0000000..bf18622 --- /dev/null +++ b/lib/strip_toc_summary.py @@ -0,0 +1,43 @@ +""" +Pelican plugin to strip Table of Contents and anchor links from article summaries. + +This prevents TOC links and heading anchor links with invalid anchors +from appearing on the homepage. +""" +from pelican import signals +import re + + +def strip_anchors(text): + """Jinja2 filter to strip TOC and anchor links from HTML text.""" + if not text: + return text + + # Remove the entire
...
block + text = re.sub( + r']*>.*?', + '', + text, + flags=re.DOTALL | re.IGNORECASE + ) + + # Remove anchor links from headings + # Converts text to just text + text = re.sub( + r']*class="[^"]*toc-backref[^"]*"[^>]*>(.*?)', + r'\1', + text, + flags=re.DOTALL | re.IGNORECASE + ) + + return text + + +def add_filter(pelican): + """Add the strip_anchors filter to Jinja2.""" + pelican.env.filters.update({'strip_anchors': strip_anchors}) + + +def register(): + """Register the plugin with Pelican.""" + signals.generator_init.connect(add_filter) diff --git a/pelicanconf.py b/pelicanconf.py index 48a2072..831eba3 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -105,7 +105,7 @@ OUTPUT_SOURCES_EXTENSION = '.rst' # Setup filters and plugins. -PLUGIN_PATHS = ["pelican-plugins"] +PLUGIN_PATHS = ["pelican-plugins", "lib"] PLUGINS = ["random_article", "strip_toc_summary"] # register filters.