Created
March 19, 2020 17:43
-
-
Save decentral1se/4f5c2993c05eef9ce170e987fdd15a1b to your computer and use it in GitHub Desktop.
pelican.py
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
"""Pelican plugin for interacting with Homebase.""" | |
from collections import defaultdict | |
from datetime import datetime as dt | |
from operator import attrgetter | |
from pelican import signals | |
from pelican.contents import Article, Author, Category | |
from pelican.generators import ArticlesGenerator, Generator | |
from pelican.readers import BaseReader | |
from pelican.utils import order_content, process_translations | |
from pyhomebase import Homebase | |
class PyhomebaseGenerator(ArticlesGenerator): | |
def __init__(self, *args, **kwargs): | |
self.articles = [] | |
self.translations = [] | |
self.dates = [] | |
self.categories = defaultdict(list) | |
self.authors = defaultdict(list) | |
super(PyhomebaseGenerator, self).__init__(*args, **kwargs) | |
def generate_context(self): | |
all_articles = [] | |
baseReader = BaseReader(self.settings) | |
catobj = baseReader.process_metadata("category", "bazbar") | |
authorobj = baseReader.process_metadata("author", "jimbob") | |
article = Article( | |
content="foobar", | |
metadata={ | |
"title": "bingbong", | |
"date": dt.now(), | |
"category": catobj, | |
"authors": [authorobj], | |
"slug": "foobar", | |
}, | |
settings=self.settings, | |
context=self.context, | |
) | |
all_articles.append(article) | |
def _process(arts): | |
origs, translations = process_translations( | |
arts, translation_id=self.settings["ARTICLE_TRANSLATION_ID"] | |
) | |
origs = order_content(origs, self.settings["ARTICLE_ORDER_BY"]) | |
return origs, translations | |
self.articles, self.translations = _process(all_articles) | |
signals.article_generator_pretaxonomy.send(self) | |
for article in self.articles: | |
# only main articles are listed in categories and tags | |
# not translations | |
self.categories[article.category].append(article) | |
if hasattr(article, "tags"): | |
for tag in article.tags: | |
self.tags[tag].append(article) | |
for author in getattr(article, "authors", []): | |
self.authors[author].append(article) | |
self.dates = list(self.articles) | |
self.dates.sort( | |
key=attrgetter("date"), reverse=self.context["NEWEST_FIRST_ARCHIVES"] | |
) | |
# and generate the output :) | |
# order the categories per name | |
self.categories = list(self.categories.items()) | |
self.categories.sort(reverse=self.settings["REVERSE_CATEGORY_ORDER"]) | |
self.authors = list(self.authors.items()) | |
self.authors.sort() | |
self._update_context( | |
("articles", "dates", "tags", "categories", "authors", "related_posts",) | |
) | |
self.save_cache() | |
self.readers.save_cache() | |
signals.article_generator_finalized.send(self) | |
def get_generators(pelican_object): | |
return PyhomebaseGenerator | |
def register(): | |
signals.get_generators.connect(get_generators) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment