Skip to content

Commit 6e52450

Browse files
committed
decouple theme and builder
1 parent 5991ed4 commit 6e52450

File tree

2 files changed

+34
-56
lines changed

2 files changed

+34
-56
lines changed

sphinx_simplepdf/builders/simplepdf.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import subprocess
55
import weasyprint
66

7-
import sass
8-
97
from bs4 import BeautifulSoup
108

119
from sphinx import __version__
@@ -52,53 +50,6 @@ def __init__(self, *args, **kwargs):
5250
}
5351
self.app.config.html_context["spd"] = debug_sphinx
5452

55-
# Generate main.css
56-
logger.info("Generating css files from scss-templates")
57-
css_folder = os.path.join(self.app.outdir, f"_static")
58-
scss_folder = os.path.join(
59-
os.path.dirname(__file__), "..", "themes", "simplepdf_theme", "static", "styles", "sources"
60-
)
61-
sass.compile(
62-
dirname=(scss_folder, css_folder),
63-
output_style="nested",
64-
custom_functions={
65-
sass.SassFunction("config", ("$a", "$b"), self.get_config_var),
66-
sass.SassFunction("theme_option", ("$a", "$b"), self.get_theme_option_var),
67-
},
68-
)
69-
70-
def get_config_var(self, name, default):
71-
"""
72-
Gets a config variables for scss out of the Sphinx configuration.
73-
If name is not found in config, the specified default var is returned.
74-
75-
Args:
76-
name: Name of the config var to use
77-
default: Default value, if name can not be found in config
78-
79-
Returns: Value
80-
"""
81-
simplepdf_vars = self.app.config.simplepdf_vars
82-
if name not in simplepdf_vars:
83-
return default
84-
return simplepdf_vars[name]
85-
86-
def get_theme_option_var(self, name, default):
87-
"""
88-
Gets a option variables for scss out of the Sphinx theme options.
89-
If name is not found in theme options, the specified default var is returned.
90-
91-
Args:
92-
name: Name of the option var to use
93-
default: Default value, if name can not be found in config
94-
95-
Returns: Value
96-
"""
97-
simplepdf_theme_options = self.app.config.simplepdf_theme_options
98-
if name not in simplepdf_theme_options:
99-
return default
100-
return simplepdf_theme_options[name]
101-
10253
def finish(self) -> None:
10354
super().finish()
10455

sphinx_simplepdf/themes/simplepdf_theme/__init__.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
"""Sphinx ReadTheDocs theme.
22
From https://github.com/ryan-roemer/sphinx-bootstrap-theme.
33
"""
4+
import logging
45
from os import path
56

7+
import sass
8+
from sphinx.application import Sphinx
69

710
__version__ = '0.1.0'
811
__version_full__ = __version__
912

13+
logger = logging.getLogger(__name__)
1014

11-
def get_html_theme_path():
12-
"""Return list of HTML theme paths."""
13-
cur_dir = path.abspath(path.dirname(path.dirname(__file__)))
14-
return cur_dir
15+
16+
def get_html_theme_path() -> str:
17+
return path.abspath(path.dirname(__file__))
18+
19+
20+
def compile_css(app: Sphinx):
21+
# Generate main.css
22+
logger.info("Generating css files from scss-templates")
23+
css_folder = path.join(app.outdir, f"_static")
24+
scss_folder = path.join(get_html_theme_path(), "static", "styles", "sources")
25+
26+
sass.compile(
27+
dirname=(scss_folder, css_folder),
28+
output_style="nested",
29+
custom_functions={
30+
sass.SassFunction(
31+
"config",
32+
("$a", "$b"),
33+
app.config.simplepdf_vars.get
34+
),
35+
sass.SassFunction(
36+
"theme_option",
37+
("$a", "$b"),
38+
app.config.simplepdf_theme_options.get
39+
),
40+
},
41+
)
1542

1643

1744
# See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package
18-
def setup(app):
19-
app.add_html_theme('simplepdf_theme', path.abspath(path.dirname(__file__)))
20-
# app.add_css_file('styles/main.css')
45+
def setup(app: Sphinx):
46+
app.add_html_theme('simplepdf_theme', get_html_theme_path())
47+
app.connect('builder-inited', compile_css)
2148

2249
return {
2350
"parallel_read_safe": True,

0 commit comments

Comments
 (0)