I used to have a post revision plugin for Octopress and I love it. Here is my effort to achieve the same effect in Pelican.

For this purpose, I came up with this post-revision plugin and also a template in the pelican-bootstrap3 theme. The plugin generates some meta data for each article that contains the history information. And the templates consumes that meta data and present them in the HTML files.

Post Revision Plugin

The plugin itself is quite straightforward. The article or page's file path on the local file system can be accessed through the source_path attribute. After the site is generated (page_generator_finalized and article_generator_finalized signal), we iterate through the articles and pages, and do a git log on them.

For simplicity, right now we only store two piece of information for each Git commit: date and commit message title (the first line). The commits are stored as a list of (date, msg) tuple in reverse order as the history attribute.

Optionally, if you specified GITHUB_URL and PROJECT_ROOT variables in the configuration file, this plugin also generates a link to the Github commit history page for the post, stored as github_history_url attribute.

Templates

Now we have the history meta data for the article, we can put it some where in the article template. I used the pelican-bootstrap3 theme. And the history information is in the post-revision.html template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{% if article.history %}
<section class="well" id="related-posts">
    {% if article.github_history_url %}
      <h4><a href={{ article.github_history_url }}>{{ POST_REVISION_TEXT|default('Post History') }}</a></h4>
    {% else %}
      <h4>{{ POST_REVISION_TEXT|default('Post History:') }}</h4>
    {% endif %}

    {% for date, msg in article.history[:POST_HISTORY_MAX_COUNT|default(5)] %}
      <b>{{ date|strftime("%A %I:%M %p, %B %d %Y") }}</b><br/><p>{{ msg }}</p>
    {% endfor %}
</section>
{% endif %}

Here we have some more settings:

  • POST_REVISION_TEXT is the title of the post revision section.
  • POST_HISTORY_MAX_COUNT controls how many history items to show.

You can see a working example down below.