Skip to content

Commit 4ae1d7c

Browse files
committed
decouple theme and builder
1 parent 5991ed4 commit 4ae1d7c

File tree

2 files changed

+63
-56
lines changed

2 files changed

+63
-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: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,79 @@
11
"""Sphinx ReadTheDocs theme.
22
From https://github.com/ryan-roemer/sphinx-bootstrap-theme.
33
"""
4+
import logging
5+
from functools import partial
46
from os import path
57

8+
import sass
9+
from sphinx.application import Sphinx
610

711
__version__ = '0.1.0'
812
__version_full__ = __version__
913

14+
logger = logging.getLogger(__name__)
1015

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
16+
17+
def get_html_theme_path() -> str:
18+
return path.abspath(path.dirname(__file__))
19+
20+
21+
def get_config_var(app: Sphinx, name, default):
22+
"""
23+
Gets a config variables for scss out of the Sphinx configuration.
24+
If name is not found in config, the specified default var is returned.
25+
26+
Args:
27+
app: Sphinx application
28+
name: Name of the config var to use
29+
default: Default value, if name can not be found in config
30+
31+
Returns: Value
32+
"""
33+
simplepdf_vars = app.config.simplepdf_vars
34+
if name not in simplepdf_vars:
35+
return default
36+
return simplepdf_vars[name]
37+
38+
39+
def get_theme_option_var(app: Sphinx, name, default):
40+
"""
41+
Gets a option variables for scss out of the Sphinx theme options.
42+
If name is not found in theme options, the specified default var is returned.
43+
44+
Args:
45+
app: Sphinx application
46+
name: Name of the option var to use
47+
default: Default value, if name can not be found in config
48+
49+
Returns: Value
50+
"""
51+
simplepdf_theme_options = app.config.simplepdf_theme_options
52+
if name not in simplepdf_theme_options:
53+
return default
54+
return simplepdf_theme_options[name]
55+
56+
57+
def compile_css(app: Sphinx):
58+
# Generate main.css
59+
logger.info("Generating css files from scss-templates")
60+
css_folder = path.join(app.outdir, f"_static")
61+
scss_folder = path.join(get_html_theme_path(), "static", "styles", "sources")
62+
63+
sass.compile(
64+
dirname=(scss_folder, css_folder),
65+
output_style="nested",
66+
custom_functions={
67+
sass.SassFunction("config", ("$a", "$b"), partial(get_config_var, app)),
68+
sass.SassFunction("theme_option", ("$a", "$b"), partial(get_theme_option_var, app)),
69+
},
70+
)
1571

1672

1773
# 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')
74+
def setup(app: Sphinx):
75+
app.add_html_theme('simplepdf_theme', get_html_theme_path())
76+
app.connect('builder-inited', compile_css)
2177

2278
return {
2379
"parallel_read_safe": True,

0 commit comments

Comments
 (0)