Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • 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.

like image 506
Саша Черных Avatar asked Oct 28 '25 08:10

Саша Черных


1 Answers

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.

like image 161
MinchinWeb Avatar answered Oct 29 '25 21:10

MinchinWeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!