From b303f497fa6fc0007d1e8ea612d22693e612b9e5 Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Thu, 19 Mar 2026 08:40:07 -0400 Subject: [PATCH] fix: move random_article plugin into tracked lib/ directory pelican-plugins/ lives in .gitignore so the random_article plugin never reached the build server after cleanup. Moved to lib/ which already serves as a PLUGIN_PATHS entry. Removed the os.path.exists check that caused the plugin to skip all articles when the generator ran before article directories existed in output. --- lib/random_article.py | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/random_article.py diff --git a/lib/random_article.py b/lib/random_article.py new file mode 100644 index 0000000..9a87464 --- /dev/null +++ b/lib/random_article.py @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- +""" +Random Article Plugin For Pelican +======================== + +This plugin generates a html file which redirect to a random article +using javascript's window.location. The generated html file is +saved at SITEURL. +""" + +from __future__ import unicode_literals + +import os.path + +from logging import info +from codecs import open + +from pelican import signals + +HTML_TOP = """ + + + random + + + + +""" + +ARTICLE_URL = """ "{0}/{1}", +""" + + +class RandomArticleGenerator(object): + """ + The structure is derived from sitemap plugin + """ + + def __init__(self, context, settings, path, theme, output_path, *null): + + self.output_path = output_path + self.context = context + self.siteurl = settings.get('SITEURL') + self.randomurl = settings.get('RANDOM') + + def write_url(self, article, fd): + if getattr(article, 'status', 'published') != 'published': + return + + fd.write(ARTICLE_URL.format(self.siteurl, article.url)) + + + def generate_output(self, writer): + path = os.path.join(self.output_path, self.randomurl) + articles = self.context['articles'] + info('writing {0}'.format(path)) + + if len(articles) == 0: + return + + with open(path, 'w', encoding='utf-8') as fd: + fd.write(HTML_TOP) + + for art in articles: + self.write_url(art, fd) + + fd.write(HTML_BOTTOM) + +def get_generators(generators): + return RandomArticleGenerator + + +def register(): + signals.get_generators.connect(get_generators)