From d813c5148edab58c1052644dab42fc17fabee8f1 Mon Sep 17 00:00:00 2001 From: Frederik Elwert Date: Thu, 10 Oct 2024 14:10:12 +0200 Subject: [PATCH 1/3] Add more blocks to simple base template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change makes it easier to create new themes by inheriting from the simple theme. It allows customization of the whole body (while still making use of the theme’s head), or individual parts of the body like header, menu, or footer. --- pelican/themes/simple/templates/base.html | 64 +++++++++++++---------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/pelican/themes/simple/templates/base.html b/pelican/themes/simple/templates/base.html index e9461fd51..7afbc3c26 100644 --- a/pelican/themes/simple/templates/base.html +++ b/pelican/themes/simple/templates/base.html @@ -40,33 +40,41 @@ -
-

{{ SITENAME }}

{% if SITESUBTITLE %}

{{ SITESUBTITLE }}

{% endif %}
- -
-
- {% block content %} - {% endblock %} -
- + {% block body %} +
+ {% block header %} +

{{ SITENAME }}

{% if SITESUBTITLE %}

{{ SITESUBTITLE }}

{% endif %}
+ {% endblock %} + {% block nav %} + + {% endblock %} +
+
+ {% block content %} + {% endblock %} +
+ + {% endblock %} From af3c2ed0cf83523d7e1d80744e93909aa99a53ce Mon Sep 17 00:00:00 2001 From: Frederik Elwert Date: Wed, 15 Jan 2025 11:37:44 +0100 Subject: [PATCH 2/3] Add block name in endblock --- pelican/themes/simple/templates/base.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pelican/themes/simple/templates/base.html b/pelican/themes/simple/templates/base.html index 7afbc3c26..491a59038 100644 --- a/pelican/themes/simple/templates/base.html +++ b/pelican/themes/simple/templates/base.html @@ -44,7 +44,7 @@
{% block header %}

{{ SITENAME }}

{% if SITESUBTITLE %}

{{ SITESUBTITLE }}

{% endif %}
- {% endblock %} + {% endblock header %} {% block nav %} - {% endblock %} + {% endblock nav %}
{% block content %} - {% endblock %} + {% endblock content %}
{% block footer %} @@ -73,8 +73,8 @@ Proudly powered by Pelican, which takes great advantage of Python. - {% endblock %} + {% endblock footer %}
- {% endblock %} + {% endblock body %} From 4fcc24ddfe33ae67df58721214dd3ec6050cd523 Mon Sep 17 00:00:00 2001 From: Frederik Elwert Date: Wed, 15 Jan 2025 11:38:00 +0100 Subject: [PATCH 3/3] Add test for simple theme and inheritance --- pelican/tests/test_theme.py | 138 ++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 pelican/tests/test_theme.py diff --git a/pelican/tests/test_theme.py b/pelican/tests/test_theme.py new file mode 100644 index 000000000..0070078a9 --- /dev/null +++ b/pelican/tests/test_theme.py @@ -0,0 +1,138 @@ +import os +import unittest +from shutil import rmtree +from tempfile import mkdtemp + +from pelican import Pelican +from pelican.settings import read_settings +from pelican.tests.support import LoggedTestCase, mute + +CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) +CONTENT_DIR = os.path.join(CURRENT_DIR, "simple_content") + + +class TestTemplateInheritance(LoggedTestCase): + def setUp(self): + super().setUp() + self.temp_output = mkdtemp(prefix="pelican_test_output.") + self.temp_theme = mkdtemp(prefix="pelican_test_theme.") + self.temp_cache = mkdtemp(prefix="pelican_test_cache.") + + # Create test theme directory structure + os.makedirs(os.path.join(self.temp_theme, "templates"), exist_ok=True) + + # Create base.html template that inherits from simple theme + template_content = """{% extends "!simple/base.html" %} + +{% block head %} +{{ super() }} + +{% endblock %} + +{% block footer %} +

New footer

+{% endblock %} +""" + + with open(os.path.join(self.temp_theme, "templates", "base.html"), "w") as f: + f.write(template_content) + + def tearDown(self): + """Clean up temporary directories and files""" + for path in [self.temp_output, self.temp_theme, self.temp_cache]: + if os.path.exists(path): + rmtree(path) + + super().tearDown() + + def test_simple_theme(self): + """Test that when a template is missing from our theme, Pelican falls back + to using the template from the simple theme.""" + + 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", + # Disable unnecessary output that might cause failures + "ARCHIVES_SAVE_AS": "", + "CATEGORIES_SAVE_AS": "", + "TAGS_SAVE_AS": "", + "AUTHOR_SAVE_AS": "", + "AUTHORS_SAVE_AS": "", + }, + ) + + pelican = Pelican(settings=settings) + mute(True)(pelican.run)() + + output_file = os.path.join(self.temp_output, "test-md-file.html") + self.assertTrue(os.path.exists(output_file)) + + with open(output_file) as f: + content = f.read() + + # Verify file content is present + self.assertIn("Test md File", content) + + # Verify simple theme content is present + self.assertIn('', content) + self.assertIn("Proudly powered by", content) + + # Verify our custom theme additions are NOT present + # (since we should be using the simple theme's template directly) + self.assertNotIn( + '', content) # From simple theme + + # Verify super() maintained original head content + self.assertIn('