diff --git a/docs/plugins.rst b/docs/plugins.rst index 8e24b0af..db7e00b4 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -301,6 +301,44 @@ Here is a basic example of how to set up your own 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/ .. _pelican-plugins bug #314: https://github.com/getpelican/pelican-plugins/issues/314 .. _Blinker: https://pythonhosted.org/blinker/