Auto title plugin
By Peter Florijn - Updated on
"""[OVERVIEW] Pelican plugin to replace “-” with a space in the value of the key “title” articles and pages.
[NOTE] Not possible to have expected value of “title” key without plugin:
https://stackoverflow.com/q/64058029/5951529
[REQUIRED] “FILENAME_METADATA: (?P<title>.*)” in Pelican settings;
this is necessary so as not to add the “title” manually in each article and page.
Otherwise error when running “pelican” command on the command line:
“could not find information about 'title'”.
https://docs.getpelican.com/en/latest/content.html#file-metadata
[REQUIRED] Set “PreserveTitle” key with any value in article/page metadata,
if the “Title” key is specified in the metadata
and you don’t want autotitle to replace hyphens with spaces in its meaning.
[NOTE] I can’t find a solution without an additional “PreserveTitle” key:
https://stackoverflow.com/posts/comments/136979516
"""
from pelican import signals
def title_replace(generator, metadata):
"""[INFO] Replace title.
[INFO] Get the pair “key”: “value” from “generator.settings” dictionary:
https://stackoverflow.com/a/26660785/5951529
https://stackoverflow.com/a/25711744/5951529
"""
if ("FILENAME_METADATA",
"(?P<title>.*)") in generator.settings.items() and "preservetitle" not in metadata.keys():
metadata["title"] = metadata["title"].replace("-", " ")
def register():
"""[INFO] Register the plugin pieces with Pelican."""
signals.article_generator_context.connect(title_replace)
signals.page_generator_context.connect(title_replace)