Skip to content

Commit a07dba2

Browse files
committed
Improve support for external theme packages by using a get_scss_sources_path() convention
1 parent 7f55f21 commit a07dba2

5 files changed

Lines changed: 61 additions & 14 deletions

File tree

docs/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
Release 1.8
5+
-----------
6+
:released: unreleased
7+
8+
* **Bugfix**: Improve support for external theme packages by using a ``get_scss_sources_path()`` convention.
9+
410
Release 1.7
511
-----------
612
:released: 02.12.2025

docs/configuration.rst

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,41 @@ simplepdf_theme
166166
---------------
167167
.. versionadded:: 1.5
168168

169-
Add custom theme for simplepdf. This overrides the default theme ``simplepdf_theme``
169+
Name of the theme to use for PDF output. This overrides the default theme ``simplepdf_theme``.
170+
The value must match both the Sphinx theme name and the importable Python module name.
171+
172+
.. code-block:: python
173+
174+
simplepdf_theme = "my_custom_pdf_theme"
175+
176+
Using an external theme
177+
~~~~~~~~~~~~~~~~~~~~~~~
178+
External themes are installed as separate Python packages that provide their own
179+
SCSS sources for PDF styling. To use one:
180+
181+
1. Install the theme package (e.g. ``pip install my-custom-pdf-theme``).
182+
2. Set ``simplepdf_theme`` in your ``conf.py`` to the theme's module name.
183+
184+
The theme module must define a ``get_scss_sources_path()`` function that returns
185+
the absolute path to its SCSS sources directory. This is how the builder locates
186+
the SCSS files to compile into CSS for the PDF.
187+
188+
**Minimal example:**
189+
190+
.. code-block:: python
191+
192+
from os import path
193+
194+
def get_scss_sources_path():
195+
"""Return the absolute path to the SCSS sources directory."""
196+
return path.join(path.abspath(path.dirname(__file__)), "static", "styles", "sources")
197+
198+
The SCSS sources directory should contain a ``main.scss`` file as the entry point.
199+
You can use the bundled ``simplepdf_theme`` as a reference for the expected
200+
directory structure and SCSS files.
201+
202+
.. note:: If the theme module cannot be imported or does not define ``get_scss_sources_path()``,
203+
the builder falls back to the bundled ``simplepdf_theme`` SCSS sources and emits a warning.w
170204

171205
.. _theme_options:
172206

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
44

55
[project]
66
name = "sphinx-simplepdf"
7-
version = "1.7.0"
7+
version = "1.8.0"
88
description = "An easy to use PDF Builder for Sphinx with a modern PDF-Theme."
99
readme = "README.rst"
1010
license = "MIT"

sphinx_simplepdf/builders/simplepdf.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,30 @@ def get_theme_option_var(self, name, default):
101101
def _resolve_scss_folder(self):
102102
"""Resolve the SCSS sources folder from the configured theme package.
103103
104-
Tries to import the theme module specified by simplepdf_theme and use
105-
its get_scss_sources_path() if available. Falls back to the bundled
106-
simplepdf_theme if the external theme cannot be found.
104+
Imports the theme module and calls its get_scss_sources_path(). Falls
105+
back to the bundled simplepdf_theme if the theme cannot be imported or
106+
does not define get_scss_sources_path().
107107
"""
108108
theme_name = self.app.config.simplepdf_theme or "simplepdf_theme"
109109
try:
110110
theme_module = importlib.import_module(theme_name)
111111
if hasattr(theme_module, "get_scss_sources_path"):
112112
return theme_module.get_scss_sources_path()
113-
return os.path.join(
114-
os.path.dirname(theme_module.__file__),
115-
"static", "styles", "sources",
113+
logger.warning(
114+
f"Theme '{theme_name}' does not define get_scss_sources_path(), "
115+
"falling back to bundled simplepdf_theme",
116+
type="simplepdf",
117+
subtype="theme",
116118
)
117119
except ImportError:
118-
logger.info(
120+
logger.warning(
119121
f"Could not import theme '{theme_name}', "
120-
"falling back to bundled simplepdf_theme"
121-
)
122-
return os.path.join(
123-
os.path.dirname(__file__), "..", "themes",
124-
"simplepdf_theme", "static", "styles", "sources",
122+
"falling back to bundled simplepdf_theme",
123+
type="simplepdf",
124+
subtype="theme",
125125
)
126+
from sphinx_simplepdf.themes.simplepdf_theme import get_scss_sources_path
127+
return get_scss_sources_path()
126128

127129
def finish(self) -> None:
128130
super().finish()

sphinx_simplepdf/themes/simplepdf_theme/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def get_html_theme_path():
1414
return cur_dir
1515

1616

17+
def get_scss_sources_path():
18+
"""Return the absolute path to the SCSS sources directory."""
19+
return path.join(path.abspath(path.dirname(__file__)), "static", "styles", "sources")
20+
21+
1722
# See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package
1823
def setup(app):
1924
app.add_html_theme("simplepdf_theme", path.abspath(path.dirname(__file__)))

0 commit comments

Comments
 (0)