From 7ea7fba87cfb621caea84c1edd3d43f2fe8b7529 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 14 Apr 2023 13:56:29 +0200 Subject: [PATCH] Feat(plugin): add source-link plugin --- plugins/source-link/README.md | 31 +++++++++++++++++++++++++ plugins/source-link/__init__.py | 1 + plugins/source-link/source_link.py | 37 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 plugins/source-link/README.md create mode 100644 plugins/source-link/__init__.py create mode 100644 plugins/source-link/source_link.py diff --git a/plugins/source-link/README.md b/plugins/source-link/README.md new file mode 100644 index 0000000..a098928 --- /dev/null +++ b/plugins/source-link/README.md @@ -0,0 +1,31 @@ +# Pelican source link plugin + +This plugin for [Pelican](https://github.com/getpelican/pelican/) generate two attributes for articles: + +- `source_link`: link to the source of the article from your forge +- `path_source_link`: link to the path of the article from your forge + +## Installation + +Then, add the plugin to your pelicanconf.py file: + +```python +PLUGINS = [ + # other plugins... + "source_link", +] +``` + +## Usage + +To use the plugin, define a constant named `GIT_SOURCE` in your pelicanconf.py file: + +```python +GIT_SOURCE_BASE_URLL = "https://github.com/your-username/your-repo" +``` + +This constant should contain the base URL of your Git repository where your source code is stored. + +Once the constant is defined, the plugin will generate a `source_link` and a `path_source_link` attributes for each article in your Pelican project. + +Those links are generated based on the article's source path relative to the root directory of your Pelican project. If the constant PATH is defined in pelicanconf.py, the plugin will use this value as the root directory. Otherwise, it will use the default value of "", which assumes that your Pelican project is located in the same directory as your pelicanconf.py file. diff --git a/plugins/source-link/__init__.py b/plugins/source-link/__init__.py new file mode 100644 index 0000000..95ade5b --- /dev/null +++ b/plugins/source-link/__init__.py @@ -0,0 +1 @@ +from .source_link import * diff --git a/plugins/source-link/source_link.py b/plugins/source-link/source_link.py new file mode 100644 index 0000000..fa54e0d --- /dev/null +++ b/plugins/source-link/source_link.py @@ -0,0 +1,37 @@ +from pelican import signals +import logging +import os + +logger = logging.getLogger(__name__) +from pelican.settings import DEFAULT_CONFIG + + +def set_default_settings(settings): + settings.setdefault("GIT_SOURCE_BASE_URL", None) + + +def init_default_config(pelican): + set_default_settings(DEFAULT_CONFIG) + if pelican: + set_default_settings(pelican.settings) + + +def source_link_generator(article_generator): + git_source = article_generator.settings.get( + "GIT_SOURCE_BASE_URL", DEFAULT_CONFIG["GIT_SOURCE_BASE_URL"] + ) + root_path = article_generator.settings.get("PATH", DEFAULT_CONFIG.get("PATH", "")) + for article in article_generator.articles: + article_path = os.path.abspath(os.path.join(root_path, article.source_path)) + relative_path = os.path.relpath(article_path, root_path) + if git_source.endswith("/"): + article.source_link = f"{git_source}{relative_path}" + else: + article.source_link = f"{git_source}/{relative_path}" + + article.path_source_link = "/".join(article.source_link.split("/")[:-1]) + + +def register(): + signals.initialized.connect(init_default_config) + signals.article_generator_finalized.connect(source_link_generator)