Skip to content

Commit 7f55f21

Browse files
authored
Support external theme packages for SCSS compilation
Refactor SCSS folder resolution to use a method that imports the theme module and retrieves the SCSS sources path, with a fallback to the bundled theme.
1 parent 1368138 commit 7f55f21

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

sphinx_simplepdf/builders/simplepdf.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
import os
23
import re
34
import subprocess
@@ -55,15 +56,7 @@ def __init__(self, *args, **kwargs):
5556
# Generate main.css
5657
logger.info("Generating css files from scss-templates")
5758
css_folder = os.path.join(self.app.outdir, "_static")
58-
scss_folder = os.path.join(
59-
os.path.dirname(__file__),
60-
"..",
61-
"themes",
62-
"simplepdf_theme",
63-
"static",
64-
"styles",
65-
"sources",
66-
)
59+
scss_folder = self._resolve_scss_folder()
6760
sass.compile(
6861
dirname=(scss_folder, css_folder),
6962
output_style="nested",
@@ -105,6 +98,32 @@ def get_theme_option_var(self, name, default):
10598
return default
10699
return simplepdf_theme_options[name]
107100

101+
def _resolve_scss_folder(self):
102+
"""Resolve the SCSS sources folder from the configured theme package.
103+
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.
107+
"""
108+
theme_name = self.app.config.simplepdf_theme or "simplepdf_theme"
109+
try:
110+
theme_module = importlib.import_module(theme_name)
111+
if hasattr(theme_module, "get_scss_sources_path"):
112+
return theme_module.get_scss_sources_path()
113+
return os.path.join(
114+
os.path.dirname(theme_module.__file__),
115+
"static", "styles", "sources",
116+
)
117+
except ImportError:
118+
logger.info(
119+
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",
125+
)
126+
108127
def finish(self) -> None:
109128
super().finish()
110129

0 commit comments

Comments
 (0)