Created
December 21, 2023 18:20
-
-
Save Kristinita/da80bdb435e6617878af5f509cd1fee7 to your computer and use it in GitHub Desktop.
Pelican plugin that allows to avoid manually writing “Title” in articles and pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""[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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment