Auto-generate title from filename in Pelican

Viewed 244

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.

  • pelicanconf.py:

    """MCVE."""
    
    AUTHOR = 'Sasha Chernykh'
    SITENAME = 'SashaPelicanDebugging'
    SITEURL = '.'
    
    PATH = 'content'
    
    TIMEZONE = 'Europe/Moscow'
    
    DEFAULT_LANG = 'en'
    
    # [INFO] Use article name when preserve the slug:
    # https://docs.getpelican.com/en/stable/settings.html#url-settings
    SLUGIFY_SOURCE = 'basename'
    
    # [INFO] Preserve case of article filename
    SLUGIFY_PRESERVE_CASE = True
    
    # [INFO] Get title from article filename:
    # https://docs.getpelican.com/en/stable/settings.html#metadata
    # https://github.com/getpelican/pelican/issues/2107
    # https://github.com/getpelican/pelican/commit/2e82a53cdf3f1f9d66557850cc2811479d5bb645
    FILENAME_METADATA = '(?P<title>.*)'
    
  • Kira-Goddess.md:

    Date: 2020-09-24 18:57:33
    
    Kira Goddess!
    

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.

1 Answers

It seems like pelican doesn't provide a way to implement what you want. FILENAME_METADATA regex can only select a name from a filename, but you can't substitute - with whitespaces there.

So I think the current best way for you is to specify title tag manually in each file.

Related