From fb328475c715f74b039114e2507be9cc7448d800 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Mon, 13 Apr 2026 00:44:18 +0100 Subject: [PATCH] Conditional on file existing --- pelican/settings.py | 11 +++++ pelican/tests/test_theme.py | 55 +++++++++++++++++++++++ pelican/themes/simple/templates/base.html | 4 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/pelican/settings.py b/pelican/settings.py index 98b1357e..715b94c0 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -592,6 +592,17 @@ def configure_settings(settings: Settings) -> Settings: else: raise Exception("Could not find the theme {}".format(settings["THEME"])) + # Clear CSS_FILE if the file doesn't exist in the theme + css_file = settings.get("CSS_FILE") + if css_file: + theme = settings["THEME"] + static_paths = settings.get("THEME_STATIC_PATHS", ["static"]) + if not any( + os.path.isfile(os.path.join(theme, sp, "css", css_file)) + for sp in static_paths + ): + settings["CSS_FILE"] = "" + # standardize strings to lowercase strings for key in ["DEFAULT_LANG"]: if key in settings: diff --git a/pelican/tests/test_theme.py b/pelican/tests/test_theme.py index 0070078a..9c81002d 100644 --- a/pelican/tests/test_theme.py +++ b/pelican/tests/test_theme.py @@ -133,6 +133,61 @@ class TestTemplateInheritance(LoggedTestCase): self.assertNotIn("Proudly powered by", content) self.assertIn("New footer", content) + def test_simple_theme_no_css_link(self): + """The simple theme has no static/css/ directory, so the CSS_FILE + link should not be rendered.""" + + settings = read_settings( + path=None, + override={ + "THEME": "simple", + "PATH": CONTENT_DIR, + "OUTPUT_PATH": self.temp_output, + "CACHE_PATH": self.temp_cache, + "SITEURL": "http://example.com", + }, + ) + + pelican = Pelican(settings=settings) + mute(True)(pelican.run)() + + with open(os.path.join(self.temp_output, "test-md-file.html")) as f: + content = f.read() + + self.assertNotIn("/theme/css/main.css", content) + + def test_child_theme_with_css_file(self): + """A child theme that provides static/css/main.css should have the + CSS_FILE link rendered.""" + + # Add a CSS file to the child theme + css_dir = os.path.join(self.temp_theme, "static", "css") + os.makedirs(css_dir) + with open(os.path.join(css_dir, "main.css"), "w") as f: + f.write("body { margin: 0; }") + + settings = read_settings( + path=None, + override={ + "THEME": self.temp_theme, + "PATH": CONTENT_DIR, + "OUTPUT_PATH": self.temp_output, + "CACHE_PATH": self.temp_cache, + "SITEURL": "http://example.com", + }, + ) + + pelican = Pelican(settings=settings) + mute(True)(pelican.run)() + + with open(os.path.join(self.temp_output, "test-md-file.html")) as f: + content = f.read() + + self.assertIn( + 'href="http://example.com/theme/css/main.css"', + content, + ) + if __name__ == "__main__": unittest.main() diff --git a/pelican/themes/simple/templates/base.html b/pelican/themes/simple/templates/base.html index eb58d810..5ad38cff 100644 --- a/pelican/themes/simple/templates/base.html +++ b/pelican/themes/simple/templates/base.html @@ -9,7 +9,9 @@ {% if SITESUBTITLE %} {% endif %} - + {% if CSS_FILE %} + + {% endif %} {% if STYLESHEET_URL %} {% endif %}