|
| 1 | +import importlib |
1 | 2 | import os |
2 | 3 | import re |
3 | 4 | import subprocess |
@@ -55,15 +56,7 @@ def __init__(self, *args, **kwargs): |
55 | 56 | # Generate main.css |
56 | 57 | logger.info("Generating css files from scss-templates") |
57 | 58 | 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() |
67 | 60 | sass.compile( |
68 | 61 | dirname=(scss_folder, css_folder), |
69 | 62 | output_style="nested", |
@@ -105,6 +98,32 @@ def get_theme_option_var(self, name, default): |
105 | 98 | return default |
106 | 99 | return simplepdf_theme_options[name] |
107 | 100 |
|
| 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 | + |
108 | 127 | def finish(self) -> None: |
109 | 128 | super().finish() |
110 | 129 |
|
|
0 commit comments