Noob Pelican question here:
I want to include content on the main page of my pelican site that is not blog-related posts, but which simply reads an rst file and puts the results on the main page. I could put a single post together, but that really isn't what I want.
I can of course hack the index.html template and put the text directly there. But what I'd like to do is put some code there that would parse a file and put the same text there.
One of the benefits of using Python as the settings file format is that Pelican can do what you want without having to write a plugin or modify Pelican itself.
Let’s say you have some introductory content stored in intro.rst
that you want to be rendered in your index.html
template. The following additions to your Pelican settings file will result in a new INTRO
variable that contains the rendered contents of the intro.rst
file.
from pelican.settings import DEFAULT_CONFIG
from pelican.readers import RstReader
config = DEFAULT_CONFIG.copy()
# If you need to override default settings (e.g., DOCUTILS_SETTINGS / DEFAULT_LANG):
# config["DEFAULT_LANG"] = "de"
# .read() returns (content, metadata). Keep content only; we don’t need the metadata.
# Assign content to an ALL-CAPS variable to access from template:
INTRO, _ = RstReader(config).read("path/to/intro.rst")
With the above settings in place, you should be able to insert the rendered content by placing the {{ INTRO }}
variable in the desired place in your index template.