Pelican Generate index.html by reStructuredText or Markdown


Use Pelican static site generator to generate index.html by writing reStructuredText or Markdown.

In Pelican, you can create your own theme. In this post, we will show you how to wrtie rst or md to generate index.html in your customized theme.

Create hidden pages

First we create hidden pages by rst or md, in which the content will be our index.html. We create the following index.rst (or index.md if you use Markdown):

:slug: index
:lang: en
:status: hidden
:summary: English index.html
:og_image: YOUR og:image URL

.. write your English index.html content here

If you use i18n_subsites plugin, you can create hidden pages with different lang metadata for each supported language. For example, you can create index_zh.rst for Chinese language with the following content:

:slug: index
:lang: zh
:status: hidden
:summary: Chinese index.html
:og_image: YOUR og:image URL

.. write your Chinese index.html content here

Jinja2 filter in pelicanconf.py

We create a custom Jinja2 filter to select the hidden pages with slug is index.

# custom Jinja2 filter
def hidden_pages_get_page_with_slug_index(hidden_pages):
    for page in hidden_pages:
        if page.slug == "index":
            return page

JINJA_FILTERS = {
    "hidden_pages_get_page_with_slug_index": hidden_pages_get_page_with_slug_index,
}

index.html in theme

Then we modify the index.html in theme to do something like following:

{% extends "layout/layout.html" %}
{% set index = (hidden_pages|hidden_pages_get_page_with_slug_index) %}

<div class="title">{{ index.title }}</div>
<div class="summary">{{ index.summary }}</div>
<div class="main-content">{{ index.content }}</div>

Just the same as you are writing page.html or article.html in the customized theme.


Tested on: Ubuntu Linux 15.10, Python 2.7.10, Pelican 3.6.3.


References:

[1]pelican custom Jinja2 filter to let you write index.html in rst · siongui/pelican-template@e70b7ca · GitHub
[2]

JINJA_FILTERS in Settings — Pelican documentation

Jinja custom filters documentation

[3]GitHub - siongui/pelican-template
[4]python - Querying for specific articles (via tag/category) in Pelican themes - Stack Overflow