Dada
|> Structures
|> Algorithms

Quick jinja filter in Pelican

Writing a jinja filter for manual teaser breakin in Pelican

I have articles on this blog that has sample code. Sometimes that sample code appears early in the article. This causes a problem with the teaser sample because sometimes the code markup breaks the rest of the list of articles, so it looks ugly and broken.

I recalled that in Drupal you can add a manual break by making an html comment that says "break". I looked for an existing pluging that does this in Pelican. I couldn't find it. I decided to make my own.

I found this great post, Adding a Jinja2 Filter with a Pelican Plugin. It explains how to create a plugin. It is so good, that I am essentially writing this to copy the key points so that if that blog goes away, I have the notes.

It is so easy to create the plugin, that I am including the full code below.

    #python
    from pelican import signals

    def break_text(value):
        result = f"{value.split('<!--break-->')[0]}..."

        return result

    def add_filter(pelican):
        """Add age_in_days filter to Pelican."""
        pelican.env.filters.update({'break': break_text})

    def register():
        """Plugin registration."""
        signals.generator_init.connect(add_filter)

After you write that, you add in pelicanconf.py

PLUGIN_PATH = ["./plugins"]
PLUGINS = ["break"]