|
1 | 1 | """Sphinx ReadTheDocs theme. |
2 | 2 | From https://github.com/ryan-roemer/sphinx-bootstrap-theme. |
3 | 3 | """ |
| 4 | +import logging |
| 5 | +from functools import partial |
4 | 6 | from os import path |
5 | 7 |
|
| 8 | +import sass |
| 9 | +from sphinx.application import Sphinx |
6 | 10 |
|
7 | 11 | __version__ = '0.1.0' |
8 | 12 | __version_full__ = __version__ |
9 | 13 |
|
| 14 | +logger = logging.getLogger(__name__) |
10 | 15 |
|
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 | + ) |
15 | 71 |
|
16 | 72 |
|
17 | 73 | # 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) |
21 | 77 |
|
22 | 78 | return { |
23 | 79 | "parallel_read_safe": True, |
|
0 commit comments