pythonjinja2regex-grouppython-repelican

Auto-generate title from filename in Pelican


1. Summary

I can’t automatically generate the correct title from filenames of articles/pages.

For example, I can’t automatically generate metadata key title Kira Goddess from article Kira-Goddess.md

2. Argumentation

DRY, automation. I don’t want to manually write the title every time for each article and page if I can do it automatically.

An exception — files with words, that contain hyphens — “well-known”, “English-speaking”. In this case, I must explicitly specify title in the metadata of my articles. But words with hyphens are rare in filenames of my articles.

3. MCVE

3.1. Data

You can see it in my KiraTitleFromFilename branch of my repository for Pelican debugging.

Another Pelican files generated by pelican-quickstart.

Simplified part of base.html:

<title>{{ article.title }}</title>

3.2. Steps to reproduce

See .travis.yml:

  1. Run Pelican build:

    pelican content -s pelicanconf.py --fatal warnings --verbose
    
  2. Finding content of <title> tag:

    grep -E "<title>.*</title>" output/Kira-Goddess.html
    

3.3. Behavior

3.3.1. Current

See Travis build:

<title>Kira-Goddess</title>
3.3.2. Desired

It would be nice, if:

<title>{{ article.title }}</title>

will transform to:

<title>Kira Goddess</title>

4. Not helped

In the description of EXTRA_PATH_METADATA variable I read, that Pelican used Python group name notation syntax (?P<name>…). I couldn’t find, how I can make substitutions in Python <class 'str'> (print(type(FILENAME_METADATA))<class 'str'>). I tried variants as:

import re

KIRA_DEFAULT_FILENAME_REGEX = '(?P<title>.*)'
FILENAME_METADATA = re.sub(KIRA_DEFAULT_FILENAME_REGEX, "-", " ")

or

KIRA_DEFAULT_FILENAME_REGEX = '(?P<title>.*)'
FILENAME_METADATA = KIRA_DEFAULT_FILENAME_REGEX.replace("-", "")

It doesn’t work.

5. Don’t offer

5.1. Use Jinja2 filters in templates

5.1.1. Suggestion

Use replace() filter in your template files like this:

<title>{{ article.title|replace('-', " ") }}</title>
5.1.2. Why is it not good

Pelican plugins (e.g. Pelican Open Graph) still will use article.title. Unwanted data as Kira-Goddess, not Kira Goddess still will pass to plugins.

5.2. Use spaces in your Markdown

5.2.1. Suggestion

For example, name your file Kira Goddess.md, not Kira-Goddess.md.

5.2.2. Why is it not good

Whitespaces in filenames is a bad practice — 1, 2, 3, 4, 5.


Solution

  • One way to do this would be to write a (small) plugin for Pelican. You probably want to hook up to the article_generator_context (and/or page_generator_context) signal. Your plugin would be passed the metadata table for the article, and you could do the substitution there. Something like this (untested):

    """
    Pelican plugin to replace "-" with a space in an article's title.
    """
    
    from pelican import signals
    
    def title_replace(generator, metadata):
        try:
            metadata["title"] = metadata["title"].replace("-", " ")
        except KeyError:
            pass
    
        # not sure if you have to return metadata, or if it is
        # globally updated as it's a dictionary
        return metadata
    
    
    def register():
        """Register the plugin pieces with Pelican."""
    
        signals.article_generator_context.connect(title_replace)
        signals.page_generator_context.connect(title_replace)
    
    

    Don't forget to add this your your list of plugins in your pelicanconf.py.

    More on Pelican Plugins.