A Quick Pelican Summary Plugin

Pelican is a pretty great static site generator written in Python. Pelican's design makes it easy to write plugins, something that came in handy for me recently. Pelican generates article summaries based on a configurable character limit which defaults to fifty or on a summary provided in article metadata by the author. But sometimes you want to limit what's shown on the index to a fold below an image or perhaps include a longer summary (without duplicating some part of the article in metadata). What I wanted was a way to explicitly indicate where an article's summary should begin and end.

I wrote the following little plugin to allow me to tell Pelican where I want article summaries to begin and end:

from pelican import signals
from pelican.generators import ArticlesGenerator

SIGIL = '<!-- summary -->'
SANDWICH = '<!-- /summary -->'

def runme(generators):
    for g in generators:
        if isinstance(g, ArticlesGenerator):
            for a in g.articles:
                sigil_start = a.content.find(SIGIL)
                if sigil_start > -1:
                    sandwich = a.content.find(SANDWICH)
                    if sandwich > -1 and sandwich > sigil_start:
                        a._summary = a.content[sigil_start+len(SIGIL):sandwich]
                    else:
                        a._summary = a.content[0:sigil_start]

def register():
    signals.all_generators_finalized.connect(runme)

This plugin hooks into the articles generator and looks in each article for the "SIGIL". If the SIGIL is there, the plugin also checks for a "SANDWICH". If the SANDWICH isn't present, the plugin assumes that the author wants the summary to begin at the first character of the article's content and extend to the beginning of SIGIL. If SANDWICH is present, the content between the end of SIGIL and the beginning of SANDWICH is used.

With these markers, you can explicitly mark the beginning and/or end of a summary in a ReST or Markdown article to be processed by Pelican. What I don't do in the plugin is handle the generation of "Read more" or "Continue" links. I do this in my templates by checking the value of the summary against the content. If they differe, I display a "Read more" link. Most of the plugins folks have published that do something like mine include markup generation. I didn't really want to fiddle with the content beyond truncating it and stashing it in the summary.

In any case, this plugin works for me, and I thought it worth putting out there in case it shows up in search results for people looking for similar solutions.