Feat(plugin): add source-link plugin
This commit is contained in:
parent
ae45ff7d42
commit
7ea7fba87c
|
@ -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.
|
|
@ -0,0 +1 @@
|
|||
from .source_link import *
|
|
@ -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)
|
Loading…
Reference in New Issue