Mind Flow Productions

Systems & Productivity

ETR: 1 minute

Depricated instructions

This is taken care of automatically by a script when requirements.txt is installed.


I wanted to add a custom test to Jinja2 but Pelican neglected to allow this in the settings. You can add custom filters, but not tests.

To add tests you need to modify two files in Pelican - settings.py and generators.py. These are found in the site-packages/pelican folder.

In settings.py add JINJA_TESTS to the DEFAULT_CONFIG dictionary. Best to add it just above (or below) the JINJA_FILTERS default value:

'JINJA_TESTS': {},
'JINJA_FILTERS': {},

In generator.py search for this code:

# get custom Jinja filters from user settings
custom_filters = self.settings['JINJA_FILTERS']
self.env.filters.update(custom_filters)

and add this just below it:

# get custom Jinja filters from user settings
custom_filters = self.settings['JINJA_FILTERS']
self.env.filters.update(custom_filters)

# get custom Jinja tests from user settings
custom_tests = self.settings['JINJA_TESTS']
self.env.tests.update(custom_tests)

Then add your test and intialize it in your projects pelicanconf.py:

def is_doc(path):
    return 'docs' in path

JINJA_TESTS = {
    'document': is_doc,
}

Yes, I know Jinja already has an ‘in’ test, but this was just an experiment!

Just make sure your test returns True or False.

In the template code use it like this:

{% if page.relative_dir is document %}
   Your code here...
{% endif %}

Now you know how to create custom jinja2 tests in Pelican.

My dev setup is on Windows 10 using Ubuntu in the Windows Subsystem for Linux

Whenever you upgrade pelican on Ubuntu you need to update the custom changes to pelican source.

This could be done directly in Ubuntu but I don’t like the editors installed on Ubuntu so I make the changes to Pelican source on Windows.

I keep a copy of current pelican source in a non-project folder on Windows, but a folder visible to Ubuntu.

Then, after making the modifications in the source on windows, I just copy the modified files from the windows pelican source to the ubuntu pelican source (in the virtual env):

Within Ubuntu Change ‘ve_name’ to the name of your virtualenv and cd into it’s pelican folder

cd ~/.virtualenvs/ve_name/lib/python3.5/site-packages/pelican

This is now your current working folder in Ubuntu Now copy the modified Pelican source files from Windows to Ubuntu (change paths according to your setup)

cp /mnt/d/dev/downloads/pelican-master/pelican/generators.py .
cp /mnt/d/dev/downloads/pelican-master/pelican/settings.py .
  devops | coding | backend | pelican      docs