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
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.
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>
See .travis.yml
:
Run Pelican build:
pelican content -s pelicanconf.py --fatal warnings --verbose
Finding content of <title>
tag:
grep -E "<title>.*</title>" output/Kira-Goddess.html
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>
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.
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.
For example, name your file Kira Goddess.md
, not Kira-Goddess.md
.
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.