diff --git a/sphinx_simplepdf/builders/simplepdf.py b/sphinx_simplepdf/builders/simplepdf.py index c4576dd..8c3c8b0 100644 --- a/sphinx_simplepdf/builders/simplepdf.py +++ b/sphinx_simplepdf/builders/simplepdf.py @@ -4,8 +4,6 @@ import subprocess import weasyprint -import sass - from bs4 import BeautifulSoup from sphinx import __version__ @@ -44,7 +42,7 @@ def __init__(self, *args, **kwargs): debug_sphinx = { "version": __version__, - "confidr": self.app.confdir, + "confdir": self.app.confdir, "srcdir": self.app.srcdir, "outdir": self.app.outdir, "extensions": self.app.config.extensions, @@ -52,53 +50,6 @@ def __init__(self, *args, **kwargs): } self.app.config.html_context["spd"] = debug_sphinx - # Generate main.css - logger.info("Generating css files from scss-templates") - css_folder = os.path.join(self.app.outdir, f"_static") - scss_folder = os.path.join( - os.path.dirname(__file__), "..", "themes", "simplepdf_theme", "static", "styles", "sources" - ) - sass.compile( - dirname=(scss_folder, css_folder), - output_style="nested", - custom_functions={ - sass.SassFunction("config", ("$a", "$b"), self.get_config_var), - sass.SassFunction("theme_option", ("$a", "$b"), self.get_theme_option_var), - }, - ) - - def get_config_var(self, name, default): - """ - Gets a config variables for scss out of the Sphinx configuration. - If name is not found in config, the specified default var is returned. - - Args: - name: Name of the config var to use - default: Default value, if name can not be found in config - - Returns: Value - """ - simplepdf_vars = self.app.config.simplepdf_vars - if name not in simplepdf_vars: - return default - return simplepdf_vars[name] - - def get_theme_option_var(self, name, default): - """ - Gets a option variables for scss out of the Sphinx theme options. - If name is not found in theme options, the specified default var is returned. - - Args: - name: Name of the option var to use - default: Default value, if name can not be found in config - - Returns: Value - """ - simplepdf_theme_options = self.app.config.simplepdf_theme_options - if name not in simplepdf_theme_options: - return default - return simplepdf_theme_options[name] - def finish(self) -> None: super().finish() @@ -156,7 +107,7 @@ def finish(self) -> None: success = True break except subprocess.TimeoutExpired: - logger.warning(f"TimeoutExpired in weasyprint, retrying") + logger.warning("TimeoutExpired in weasyprint, retrying") except subprocess.CalledProcessError as e: logger.warning(f"CalledProcessError in weasyprint, retrying\n{str(e)}") finally: diff --git a/sphinx_simplepdf/themes/simplepdf_theme/__init__.py b/sphinx_simplepdf/themes/simplepdf_theme/__init__.py index 9837ce8..18de630 100644 --- a/sphinx_simplepdf/themes/simplepdf_theme/__init__.py +++ b/sphinx_simplepdf/themes/simplepdf_theme/__init__.py @@ -3,6 +3,8 @@ """ from os import path +import sass + __version__ = '0.1.0' __version_full__ = __version__ @@ -14,10 +16,58 @@ def get_html_theme_path(): return cur_dir +def create_custom_css(app): + + try: + simplepdf_vars = app.config.simplepdf_vars + except AttributeError: + simplepdf_vars = {} + + def get_config_var(name, default): + """ + Gets a config variables for scss out of the Sphinx configuration. + If name is not found in config, the specified default var is returned. + + Args: + name: Name of the config var to use + default: Default value, if name can not be found in config + + Returns: Value + """ + return simplepdf_vars.get(name, default) + + def get_theme_var(name, default): + """ + Gets a option variables for scss out of the Sphinx theme options. + If name is not found in theme options, the specified default var is returned. + + Args: + name: Name of the option var to use + default: Default value, if name can not be found in config + + Returns: Value + """ + return app.config.html_theme_options.get(name, default) + + here = path.abspath(path.dirname(__file__)) + scss_folder = path.join(here, "static", "styles", "sources") + + staticdir = path.join(app.builder.outdir, "_static") + + sass.compile( + dirname=(scss_folder, staticdir), + output_style="nested", + custom_functions={ + sass.SassFunction("config", ("$a", "$b"), get_config_var), + sass.SassFunction("theme_option", ("$a", "$b"), get_theme_var), + }, + ) + + # See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package def setup(app): app.add_html_theme('simplepdf_theme', path.abspath(path.dirname(__file__))) - # app.add_css_file('styles/main.css') + app.connect('builder-inited', create_custom_css) return { "parallel_read_safe": True,