mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Document how to inject articles with a plugin
Add to the plugin documentation a recipe for injecting articles programmatically when Pelican is running.
This commit is contained in:
parent
bb10d286a6
commit
9ec1750709
1 changed files with 38 additions and 0 deletions
|
|
@ -301,6 +301,44 @@ Here is a basic example of how to set up your own writer::
|
||||||
signals.get_writer.connect(add_writer)
|
signals.get_writer.connect(add_writer)
|
||||||
|
|
||||||
|
|
||||||
|
Using Plugins to Inject Content
|
||||||
|
-------------------------------
|
||||||
|
|
||||||
|
You can programmatically inject articles or pages using plugins. This can be
|
||||||
|
useful if you plan to fetch articles from an API, for example.
|
||||||
|
|
||||||
|
Following is a simple example of how one can build a plugin that injects a
|
||||||
|
custom article, using the ``article_generator_pretaxonomy`` signal::
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
from pelican import signals
|
||||||
|
from pelican.contents import Article
|
||||||
|
from pelican.readers import BaseReader
|
||||||
|
|
||||||
|
def addArticle(articleGenerator):
|
||||||
|
settings = articleGenerator.settings
|
||||||
|
|
||||||
|
# Author, category, and tags are objects, not strings, so they need to
|
||||||
|
# be handled using BaseReader's process_metadata() function.
|
||||||
|
baseReader = BaseReader(settings)
|
||||||
|
|
||||||
|
content = "I am the body of an injected article!"
|
||||||
|
|
||||||
|
newArticle = Article(content, {
|
||||||
|
"title": "Injected Article!",
|
||||||
|
"date": datetime.datetime.now(),
|
||||||
|
"category": baseReader.process_metadata("category", "fromAPI"),
|
||||||
|
"tags": baseReader.process_metadata("tags", "tagA, tagB")
|
||||||
|
})
|
||||||
|
|
||||||
|
articleGenerator.articles.insert(0, newArticle)
|
||||||
|
|
||||||
|
def register():
|
||||||
|
signals.article_generator_pretaxonomy.connect(addArticle)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.. _Pip: https://pip.pypa.io/
|
.. _Pip: https://pip.pypa.io/
|
||||||
.. _pelican-plugins bug #314: https://github.com/getpelican/pelican-plugins/issues/314
|
.. _pelican-plugins bug #314: https://github.com/getpelican/pelican-plugins/issues/314
|
||||||
.. _Blinker: https://pythonhosted.org/blinker/
|
.. _Blinker: https://pythonhosted.org/blinker/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue