From 06a5d6e5639a0def020cc0b05f1a953bb46ae823 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Wed, 12 Feb 2020 23:28:34 +0100 Subject: [PATCH 01/27] Add option for auxiliary pygments styles --- sphinx/builders/html.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index b3d2f1da2..0faefc18f 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -247,6 +247,7 @@ class StandaloneHTMLBuilder(Builder): self.init_templates() self.init_highlighter() + self.init_aux_highlighters() self.init_css_files() self.init_js_files() @@ -300,6 +301,12 @@ class StandaloneHTMLBuilder(Builder): style = 'sphinx' self.highlighter = PygmentsBridge('html', style) + def init_aux_highlighters(self) -> None: + self.aux_highlighters = {} # type: Dict[str, PygmentsBridge] + for style, media_query in self.config.html_aux_pygments_styles.items(): + self.aux_highlighters[style] = PygmentsBridge('html', style) + self.add_css_file(f'pygments-{style}.css', media=media_query) + def init_css_files(self) -> None: for filename, attrs in self.app.registry.css_files: self.add_css_file(filename, **attrs) @@ -747,6 +754,13 @@ class StandaloneHTMLBuilder(Builder): with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f: f.write(self.highlighter.get_stylesheet()) + def create_pygments_aux_style_files(self) -> None: + """create a style file for pygments.""" + for style, highlighter in self.aux_highlighters.items(): + filename = f'pygments-{style}.css' + with open(path.join(self.outdir, '_static', filename), 'w') as f: + f.write(highlighter.get_stylesheet()) + def copy_translation_js(self) -> None: """Copy a JavaScript file for translations.""" if self.config.language is not None: @@ -796,6 +810,7 @@ class StandaloneHTMLBuilder(Builder): context.update(self.indexer.context_for_searchtool()) self.create_pygments_style_file() + self.create_pygments_aux_style_files() self.copy_translation_js() self.copy_stemmer_js() self.copy_theme_static_files(context) @@ -1211,6 +1226,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_style', None, 'html', [str]) app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) + app.add_config_value('html_aux_pygments_styles', [], 'html') app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') app.add_config_value('html_static_path', [], 'html') From 5614f6f60ad42b7431dceba18c3a7abc607bc41b Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Thu, 13 Feb 2020 02:02:58 +0100 Subject: [PATCH 02/27] Fix default value for html_aux_pygments_styles --- sphinx/builders/html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 0faefc18f..ec74c3280 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -1226,7 +1226,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_style', None, 'html', [str]) app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) - app.add_config_value('html_aux_pygments_styles', [], 'html') + app.add_config_value('html_aux_pygments_styles', {}, 'html') app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') app.add_config_value('html_static_path', [], 'html') From 9e38d3999ece27e09ac5a9e8292045f9a2efac4b Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Thu, 13 Feb 2020 02:38:12 +0100 Subject: [PATCH 03/27] Replace f-strings with percent syntax --- sphinx/builders/html.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index ec74c3280..da3ef3ab1 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -305,7 +305,7 @@ class StandaloneHTMLBuilder(Builder): self.aux_highlighters = {} # type: Dict[str, PygmentsBridge] for style, media_query in self.config.html_aux_pygments_styles.items(): self.aux_highlighters[style] = PygmentsBridge('html', style) - self.add_css_file(f'pygments-{style}.css', media=media_query) + self.add_css_file('pygments-%s.css' % style, media=media_query) def init_css_files(self) -> None: for filename, attrs in self.app.registry.css_files: @@ -757,7 +757,7 @@ class StandaloneHTMLBuilder(Builder): def create_pygments_aux_style_files(self) -> None: """create a style file for pygments.""" for style, highlighter in self.aux_highlighters.items(): - filename = f'pygments-{style}.css' + filename = 'pygments-%s.css' % style with open(path.join(self.outdir, '_static', filename), 'w') as f: f.write(highlighter.get_stylesheet()) From 3f9be211eeb59b1e4391cf3cb6884c86cef8f4a0 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 13:46:24 +0100 Subject: [PATCH 04/27] Add optional css_selector arg to get_stylesheet --- sphinx/highlighting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 4dc9ce543..3f8601b61 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -10,7 +10,7 @@ from functools import partial from importlib import import_module -from typing import Any, Dict +from typing import Any, Dict, Union, Iterable from pygments import highlight from pygments.filters import ErrorToken @@ -157,9 +157,9 @@ class PygmentsBridge: # MEMO: this is done to escape Unicode chars with non-Unicode engines return texescape.hlescape(hlsource, self.latex_engine) - def get_stylesheet(self) -> str: + def get_stylesheet(self, *, css_selector: Union[str, Iterable[str]] = '.highlight') -> str: formatter = self.get_formatter() if self.dest == 'html': - return formatter.get_style_defs('.highlight') + return formatter.get_style_defs(css_selector) else: return formatter.get_style_defs() + _LATEX_ADD_STYLES From a98e6f10a88ddaba3430c9726354bdea841e0e71 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 13:47:02 +0100 Subject: [PATCH 05/27] Add dark theme to template --- sphinx/builders/html/__init__.py | 48 ++++++++++++++++++++++---------- sphinx/themes/basic/layout.html | 3 ++ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index f16621f7c..25551257e 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -15,7 +15,7 @@ import sys import warnings from hashlib import md5 from os import path -from typing import Any, Dict, IO, Iterable, Iterator, List, Set, Tuple +from typing import Any, Dict, IO, Iterable, Iterator, List, Set, Tuple, Union from docutils import nodes from docutils.core import publish_parts @@ -241,10 +241,10 @@ class StandaloneHTMLBuilder(Builder): candidates = [path.join(dir, self.config.language, 'LC_MESSAGES', 'sphinx.js') for dir in self.config.locale_dirs] + \ - [path.join(package_dir, 'locale', self.config.language, - 'LC_MESSAGES', 'sphinx.js'), - path.join(sys.prefix, 'share/sphinx/locale', - self.config.language, 'sphinx.js')] + [path.join(package_dir, 'locale', self.config.language, + 'LC_MESSAGES', 'sphinx.js'), + path.join(sys.prefix, 'share/sphinx/locale', + self.config.language, 'sphinx.js')] for jsfile in candidates: if path.isfile(jsfile): @@ -264,19 +264,32 @@ class StandaloneHTMLBuilder(Builder): def init_highlighter(self) -> None: # determine Pygments style and create the highlighter + style = 'sphinx' if self.config.pygments_style is not None: style = self.config.pygments_style elif self.theme: style = self.theme.get_config('theme', 'pygments_style', 'none') - else: - style = 'sphinx' self.highlighter = PygmentsBridge('html', style) + dark_style = None + if self.config.html_pygments_dark_style is not None: + dark_style = self.config.pygments_dark_style + elif self.theme: + dark_style = self.theme.get_config('theme', 'pygments_dark_style', 'none') + + if dark_style is not None: + self.dark_highlighter = PygmentsBridge('html', dark_style) + else: + self.dark_highlighter = None + def init_aux_highlighters(self) -> None: - self.aux_highlighters = {} # type: Dict[str, PygmentsBridge] - for style, media_query in self.config.html_aux_pygments_styles.items(): - self.aux_highlighters[style] = PygmentsBridge('html', style) - self.add_css_file('pygments-%s.css' % style, media=media_query) + self.aux_highlighters: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] = {} + if self.config.html_aux_pygments_styles is not None: + aux_styles = self.config.html_aux_pygments_styles.items() + elif self.theme and self.theme.config.has_section('auxiliary_styles'): + aux_styles = self.theme.config.items('options') + for style, css_selector in aux_styles.items(): + self.aux_highlighters[style] = (PygmentsBridge('html', style), css_selector) def init_css_files(self) -> None: for filename, attrs in self.app.registry.css_files: @@ -490,7 +503,8 @@ class StandaloneHTMLBuilder(Builder): 'parents': [], 'logo': logo, 'favicon': favicon, - 'html5_doctype': html5_ready and not self.config.html4_writer + 'html5_doctype': html5_ready and not self.config.html4_writer, + 'pygments_dark_style': self.dark_highlighter is not None, } if self.theme: self.globalcontext.update( @@ -725,12 +739,15 @@ class StandaloneHTMLBuilder(Builder): with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f: f.write(self.highlighter.get_stylesheet()) + with open(path.join(self.outdir, '_static', 'pygments_dark.css'), 'w') as f: + f.write(self.dark_highlighter.get_stylesheet()) + def create_pygments_aux_style_files(self) -> None: """create a style file for pygments.""" - for style, highlighter in self.aux_highlighters.items(): + for style, (highlighter, css_selector) in self.aux_highlighters.items(): filename = 'pygments-%s.css' % style with open(path.join(self.outdir, '_static', filename), 'w') as f: - f.write(highlighter.get_stylesheet()) + f.write(highlighter.get_stylesheet(css_selector=css_selector)) def copy_translation_js(self) -> None: """Copy a JavaScript file for translations.""" @@ -1196,7 +1213,8 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_style', None, 'html', [str]) app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) - app.add_config_value('html_aux_pygments_styles', {}, 'html') + app.add_config_value('html_pygments_dark', None, 'html', [str]) + app.add_config_value('html_pygments_aux_styles', {}, 'html') app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') app.add_config_value('html_static_path', [], 'html') diff --git a/sphinx/themes/basic/layout.html b/sphinx/themes/basic/layout.html index 9692ea2c4..0cb54e18a 100644 --- a/sphinx/themes/basic/layout.html +++ b/sphinx/themes/basic/layout.html @@ -96,6 +96,9 @@ {%- macro css() %} + {%- if pygments_dark_style %} + + {%- endif %} {%- for css in css_files %} {%- if css|attr("filename") %} {{ css_tag(css) }} From 5481d6da05af677d37e22920254d6129a3f9a60e Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 14:17:05 +0100 Subject: [PATCH 06/27] Fix formatting --- sphinx/builders/html/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 25551257e..fffd75b29 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -276,14 +276,15 @@ class StandaloneHTMLBuilder(Builder): dark_style = self.config.pygments_dark_style elif self.theme: dark_style = self.theme.get_config('theme', 'pygments_dark_style', 'none') - + if dark_style is not None: self.dark_highlighter = PygmentsBridge('html', dark_style) else: self.dark_highlighter = None def init_aux_highlighters(self) -> None: - self.aux_highlighters: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] = {} + self.aux_highlighters = \ + {} # type: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] if self.config.html_aux_pygments_styles is not None: aux_styles = self.config.html_aux_pygments_styles.items() elif self.theme and self.theme.config.has_section('auxiliary_styles'): From d42e4c9a1267f82b3d8320b2de52cd386999adc3 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 14:23:31 +0100 Subject: [PATCH 07/27] Fix config value name --- sphinx/builders/html/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index fffd75b29..cf2283c0b 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -1214,7 +1214,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_style', None, 'html', [str]) app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) - app.add_config_value('html_pygments_dark', None, 'html', [str]) + app.add_config_value('html_pygments_dark_style', None, 'html', [str]) app.add_config_value('html_pygments_aux_styles', {}, 'html') app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') From 0abb05c863ec36c56d764a6fb20fa5b459fa9ac8 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 14:36:28 +0100 Subject: [PATCH 08/27] Fix config value name --- sphinx/builders/html/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index cf2283c0b..064c196d5 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -285,8 +285,8 @@ class StandaloneHTMLBuilder(Builder): def init_aux_highlighters(self) -> None: self.aux_highlighters = \ {} # type: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] - if self.config.html_aux_pygments_styles is not None: - aux_styles = self.config.html_aux_pygments_styles.items() + if self.config.html_pygments_aux_styles is not None: + aux_styles = self.config.html_pygments_aux_styles.items() elif self.theme and self.theme.config.has_section('auxiliary_styles'): aux_styles = self.theme.config.items('options') for style, css_selector in aux_styles.items(): From 355f5d53958a28545220ca3bfb1a0d77167c8e60 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 14:44:05 +0100 Subject: [PATCH 09/27] Fix double dict.items() call --- sphinx/builders/html/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 064c196d5..1b93e0359 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -286,7 +286,7 @@ class StandaloneHTMLBuilder(Builder): self.aux_highlighters = \ {} # type: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] if self.config.html_pygments_aux_styles is not None: - aux_styles = self.config.html_pygments_aux_styles.items() + aux_styles = self.config.html_pygments_aux_styles elif self.theme and self.theme.config.has_section('auxiliary_styles'): aux_styles = self.theme.config.items('options') for style, css_selector in aux_styles.items(): @@ -1215,7 +1215,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) app.add_config_value('html_pygments_dark_style', None, 'html', [str]) - app.add_config_value('html_pygments_aux_styles', {}, 'html') + app.add_config_value('html_pygments_aux_styles', None, 'html', [dict]) app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') app.add_config_value('html_static_path', [], 'html') From ccfe2307cb3d70a1a4c6c41d127900309d7c1256 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 14:51:27 +0100 Subject: [PATCH 10/27] Fix reference before assignment error --- sphinx/builders/html/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 1b93e0359..75f6c671e 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -285,6 +285,7 @@ class StandaloneHTMLBuilder(Builder): def init_aux_highlighters(self) -> None: self.aux_highlighters = \ {} # type: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] + aux_styles = {} # type: Dict[str, Union[str, Iterable[str]]] if self.config.html_pygments_aux_styles is not None: aux_styles = self.config.html_pygments_aux_styles elif self.theme and self.theme.config.has_section('auxiliary_styles'): From ced9fef2dcdde37dda52210b5340024cc9081114 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 15:31:52 +0100 Subject: [PATCH 11/27] Fix mypy type error --- sphinx/builders/html/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 75f6c671e..be61c4b55 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -289,7 +289,7 @@ class StandaloneHTMLBuilder(Builder): if self.config.html_pygments_aux_styles is not None: aux_styles = self.config.html_pygments_aux_styles elif self.theme and self.theme.config.has_section('auxiliary_styles'): - aux_styles = self.theme.config.items('options') + aux_styles = dict(self.theme.config.items('options')) for style, css_selector in aux_styles.items(): self.aux_highlighters[style] = (PygmentsBridge('html', style), css_selector) From f61f17d524f56bddae272057b0250971661567db Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 16:20:58 +0100 Subject: [PATCH 12/27] Revert some redundant changes --- sphinx/builders/html/__init__.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index be61c4b55..a6a32f83c 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -241,10 +241,10 @@ class StandaloneHTMLBuilder(Builder): candidates = [path.join(dir, self.config.language, 'LC_MESSAGES', 'sphinx.js') for dir in self.config.locale_dirs] + \ - [path.join(package_dir, 'locale', self.config.language, - 'LC_MESSAGES', 'sphinx.js'), - path.join(sys.prefix, 'share/sphinx/locale', - self.config.language, 'sphinx.js')] + [path.join(package_dir, 'locale', self.config.language, + 'LC_MESSAGES', 'sphinx.js'), + path.join(sys.prefix, 'share/sphinx/locale', + self.config.language, 'sphinx.js')] for jsfile in candidates: if path.isfile(jsfile): @@ -264,18 +264,20 @@ class StandaloneHTMLBuilder(Builder): def init_highlighter(self) -> None: # determine Pygments style and create the highlighter - style = 'sphinx' if self.config.pygments_style is not None: style = self.config.pygments_style elif self.theme: style = self.theme.get_config('theme', 'pygments_style', 'none') + else: + style = 'sphinx' self.highlighter = PygmentsBridge('html', style) - dark_style = None if self.config.html_pygments_dark_style is not None: dark_style = self.config.pygments_dark_style elif self.theme: dark_style = self.theme.get_config('theme', 'pygments_dark_style', 'none') + else: + dark_style = None if dark_style is not None: self.dark_highlighter = PygmentsBridge('html', dark_style) From 876c6b4770934916a86740d49602cf5c9a1e57ab Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 17:03:57 +0100 Subject: [PATCH 13/27] Remove html_pygments_aux_styles option --- sphinx/builders/html/__init__.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index a6a32f83c..c5606d566 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -287,13 +287,10 @@ class StandaloneHTMLBuilder(Builder): def init_aux_highlighters(self) -> None: self.aux_highlighters = \ {} # type: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] - aux_styles = {} # type: Dict[str, Union[str, Iterable[str]]] - if self.config.html_pygments_aux_styles is not None: - aux_styles = self.config.html_pygments_aux_styles - elif self.theme and self.theme.config.has_section('auxiliary_styles'): - aux_styles = dict(self.theme.config.items('options')) - for style, css_selector in aux_styles.items(): - self.aux_highlighters[style] = (PygmentsBridge('html', style), css_selector) + + if self.theme and self.theme.config.has_section('auxiliary_styles'): + for style, css_selector in self.theme.config.items('options'): + self.aux_highlighters[style] = (PygmentsBridge('html', style), css_selector) def init_css_files(self) -> None: for filename, attrs in self.app.registry.css_files: @@ -1218,7 +1215,6 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) app.add_config_value('html_pygments_dark_style', None, 'html', [str]) - app.add_config_value('html_pygments_aux_styles', None, 'html', [dict]) app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') app.add_config_value('html_static_path', [], 'html') From 6041e7a15f4483fbf6e8d742863b14f4fa56b642 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 17:09:04 +0100 Subject: [PATCH 14/27] Fix default value for pygments_dark_style --- sphinx/builders/html/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index c5606d566..5105c4e2c 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -275,7 +275,7 @@ class StandaloneHTMLBuilder(Builder): if self.config.html_pygments_dark_style is not None: dark_style = self.config.pygments_dark_style elif self.theme: - dark_style = self.theme.get_config('theme', 'pygments_dark_style', 'none') + dark_style = self.theme.get_config('theme', 'pygments_dark_style', None) else: dark_style = None From da5f5078e1d574c35cfc784194f172de68feaedd Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 17:20:15 +0100 Subject: [PATCH 15/27] Check existence of dark highlighter --- .vscode/settings.json | 3 +++ sphinx/builders/html/__init__.py | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..6089b5e8e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": ".venv/bin/python3" +} diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 5105c4e2c..1ff529ca1 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -740,8 +740,9 @@ class StandaloneHTMLBuilder(Builder): with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f: f.write(self.highlighter.get_stylesheet()) - with open(path.join(self.outdir, '_static', 'pygments_dark.css'), 'w') as f: - f.write(self.dark_highlighter.get_stylesheet()) + if self.dark_hightlighter: + with open(path.join(self.outdir, '_static', 'pygments_dark.css'), 'w') as f: + f.write(self.dark_highlighter.get_stylesheet()) def create_pygments_aux_style_files(self) -> None: """create a style file for pygments.""" From 5ce5e788ce12b4cebc7c1f005377d50a10a69f61 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 17:25:56 +0100 Subject: [PATCH 16/27] Fix typo --- sphinx/builders/html/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 1ff529ca1..8d9e8821c 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -740,7 +740,7 @@ class StandaloneHTMLBuilder(Builder): with open(path.join(self.outdir, '_static', 'pygments.css'), 'w') as f: f.write(self.highlighter.get_stylesheet()) - if self.dark_hightlighter: + if self.dark_highlighter: with open(path.join(self.outdir, '_static', 'pygments_dark.css'), 'w') as f: f.write(self.dark_highlighter.get_stylesheet()) From d269483a6f3050c75a306f57afe51c3fabf984a4 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 8 Mar 2020 18:40:11 +0100 Subject: [PATCH 17/27] Fix typo --- sphinx/builders/html/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 8d9e8821c..4beadab1e 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -273,7 +273,7 @@ class StandaloneHTMLBuilder(Builder): self.highlighter = PygmentsBridge('html', style) if self.config.html_pygments_dark_style is not None: - dark_style = self.config.pygments_dark_style + dark_style = self.config.html_pygments_dark_style elif self.theme: dark_style = self.theme.get_config('theme', 'pygments_dark_style', None) else: From 25f90ed3eec0ffe590a990c2b7b452db7a38ad67 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Fri, 13 Mar 2020 15:59:28 +0100 Subject: [PATCH 18/27] Use add_css_file instead of template modification --- sphinx/builders/html/__init__.py | 4 +++- sphinx/themes/basic/layout.html | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 4beadab1e..bd8db9b54 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -505,7 +505,6 @@ class StandaloneHTMLBuilder(Builder): 'logo': logo, 'favicon': favicon, 'html5_doctype': html5_ready and not self.config.html4_writer, - 'pygments_dark_style': self.dark_highlighter is not None, } if self.theme: self.globalcontext.update( @@ -743,6 +742,9 @@ class StandaloneHTMLBuilder(Builder): if self.dark_highlighter: with open(path.join(self.outdir, '_static', 'pygments_dark.css'), 'w') as f: f.write(self.dark_highlighter.get_stylesheet()) + self.add_css_file('pygments_dark.css', + media='(prefers-color-scheme: dark)', + id='pygments_dark_css') def create_pygments_aux_style_files(self) -> None: """create a style file for pygments.""" diff --git a/sphinx/themes/basic/layout.html b/sphinx/themes/basic/layout.html index 0cb54e18a..9692ea2c4 100644 --- a/sphinx/themes/basic/layout.html +++ b/sphinx/themes/basic/layout.html @@ -96,9 +96,6 @@ {%- macro css() %} - {%- if pygments_dark_style %} - - {%- endif %} {%- for css in css_files %} {%- if css|attr("filename") %} {{ css_tag(css) }} From 08708e111ab86839e815bb71bffd6c174e5a531b Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Fri, 13 Mar 2020 16:10:33 +0100 Subject: [PATCH 19/27] Remove vscode file --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6089b5e8e..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.pythonPath": ".venv/bin/python3" -} From a34ff848dccbb853d3dfead9cdb5da7418a5e00e Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 14:29:46 +0100 Subject: [PATCH 20/27] Remove auxiliary highlighter feature --- sphinx/builders/html/__init__.py | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index bd8db9b54..40085776e 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -218,7 +218,6 @@ class StandaloneHTMLBuilder(Builder): self.init_templates() self.init_highlighter() - self.init_aux_highlighters() self.init_css_files() self.init_js_files() @@ -281,17 +280,12 @@ class StandaloneHTMLBuilder(Builder): if dark_style is not None: self.dark_highlighter = PygmentsBridge('html', dark_style) + self.add_css_file('pygments_dark.css', + media='(prefers-color-scheme: dark)', + id='pygments_dark_css') else: self.dark_highlighter = None - def init_aux_highlighters(self) -> None: - self.aux_highlighters = \ - {} # type: Dict[str, Tuple[PygmentsBridge, Union[str, Iterable[str]]]] - - if self.theme and self.theme.config.has_section('auxiliary_styles'): - for style, css_selector in self.theme.config.items('options'): - self.aux_highlighters[style] = (PygmentsBridge('html', style), css_selector) - def init_css_files(self) -> None: for filename, attrs in self.app.registry.css_files: self.add_css_file(filename, **attrs) @@ -742,16 +736,6 @@ class StandaloneHTMLBuilder(Builder): if self.dark_highlighter: with open(path.join(self.outdir, '_static', 'pygments_dark.css'), 'w') as f: f.write(self.dark_highlighter.get_stylesheet()) - self.add_css_file('pygments_dark.css', - media='(prefers-color-scheme: dark)', - id='pygments_dark_css') - - def create_pygments_aux_style_files(self) -> None: - """create a style file for pygments.""" - for style, (highlighter, css_selector) in self.aux_highlighters.items(): - filename = 'pygments-%s.css' % style - with open(path.join(self.outdir, '_static', filename), 'w') as f: - f.write(highlighter.get_stylesheet(css_selector=css_selector)) def copy_translation_js(self) -> None: """Copy a JavaScript file for translations.""" @@ -802,7 +786,6 @@ class StandaloneHTMLBuilder(Builder): context.update(self.indexer.context_for_searchtool()) self.create_pygments_style_file() - self.create_pygments_aux_style_files() self.copy_translation_js() self.copy_stemmer_js() self.copy_theme_static_files(context) From a9cc5bf6f36fded951b58f6a3a4270cd1a04b69f Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 14:54:31 +0100 Subject: [PATCH 21/27] Add docs --- doc/theming.rst | 6 ++++++ doc/usage/configuration.rst | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/doc/theming.rst b/doc/theming.rst index e2ef1fcf2..50b5f8eaa 100644 --- a/doc/theming.rst +++ b/doc/theming.rst @@ -63,6 +63,12 @@ Python :mod:`ConfigParser` module) and has the following structure: highlighting. This can be overridden by the user in the :confval:`pygments_style` config value. +* The **pygments_dark_style** setting gives the name of a Pygments style to use + for highlighting when the CSS media query ``(prefers-color-scheme: dark)`` + evaluates to true. It is injected into the page using + :meth:`~Sphinx.add_css_file()`. This can be overriden by the user in the + :confval:`html_pygments_dark_style` config value. + * The **sidebars** setting gives the comma separated list of sidebar templates for constructing sidebars. This can be overridden by the user in the :confval:`html_sidebars` config value. diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index e5ab9c7c0..5149bd931 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -939,6 +939,13 @@ that use Sphinx's HTMLWriter class. The image file will be copied to the ``_static`` directory of the output HTML, but only if the file does not already exist there. +.. confval:: html_pygments_dark_style + + The style name to use for an additional Pygments stylesheet similar to + :confval:`pygments_style` . If not set and the theme defines a default + ``'pygments_dark_style'`` it will be used, otherwise no additional style + will be generated. See :doc:`/usage/theming` for further information. + .. confval:: html_css_files A list of CSS files. The entry must be a *filename* string or a tuple From 6314419bc54dec517e32220a72157c8d778bc66f Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 15:10:25 +0100 Subject: [PATCH 22/27] Add tests for pygments_dark_style options --- tests/test_build_html.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 39cb3bf71..f0da3ce30 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -1525,6 +1525,21 @@ def test_html_pygments_for_classic_theme(app): assert style.__name__ == 'SphinxStyle' +@pytest.mark.sphinx('html', testroot='basic', + confoverrides={'html_pygments_dark_style': 'monokai'}) +def test_html_pygments_style_manually(app): + style = app.builder.highlighter.formatter_args.get('style') + assert style.__name__ == 'MonokaiStyle' + + +@pytest.mark.sphinx('html', testroot='basic', + confoverrides={'html_theme_options.pygments_dark_style': + 'solarized-dark'}) +def test_html_pygments_for_classic_theme(app): + style = app.builder.highlighter.formatter_args.get('style') + assert style.__name__ == 'SolarizedDarkStyle' + + @pytest.mark.sphinx(testroot='basic', srcdir='validate_html_extra_path') def test_validate_html_extra_path(app): (app.confdir / '_static').makedirs() From 81fe5f847784c1200ce94cf5bf7327d8d22d0b9a Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 18:46:02 +0100 Subject: [PATCH 23/27] Remove html_pygments_dark_style option --- doc/theming.rst | 3 +-- doc/usage/configuration.rst | 7 ------- sphinx/builders/html/__init__.py | 5 +---- .../test_theme/test-theme/theme.conf | 1 + tests/test_build_html.py | 16 +++------------- tests/test_theming.py | 16 ++++++++++++++++ 6 files changed, 22 insertions(+), 26 deletions(-) diff --git a/doc/theming.rst b/doc/theming.rst index 50b5f8eaa..6a154affd 100644 --- a/doc/theming.rst +++ b/doc/theming.rst @@ -66,8 +66,7 @@ Python :mod:`ConfigParser` module) and has the following structure: * The **pygments_dark_style** setting gives the name of a Pygments style to use for highlighting when the CSS media query ``(prefers-color-scheme: dark)`` evaluates to true. It is injected into the page using - :meth:`~Sphinx.add_css_file()`. This can be overriden by the user in the - :confval:`html_pygments_dark_style` config value. + :meth:`~Sphinx.add_css_file()`. * The **sidebars** setting gives the comma separated list of sidebar templates for constructing sidebars. This can be overridden by the user in the diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index 5149bd931..e5ab9c7c0 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -939,13 +939,6 @@ that use Sphinx's HTMLWriter class. The image file will be copied to the ``_static`` directory of the output HTML, but only if the file does not already exist there. -.. confval:: html_pygments_dark_style - - The style name to use for an additional Pygments stylesheet similar to - :confval:`pygments_style` . If not set and the theme defines a default - ``'pygments_dark_style'`` it will be used, otherwise no additional style - will be generated. See :doc:`/usage/theming` for further information. - .. confval:: html_css_files A list of CSS files. The entry must be a *filename* string or a tuple diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 40085776e..0ce5da82b 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -271,9 +271,7 @@ class StandaloneHTMLBuilder(Builder): style = 'sphinx' self.highlighter = PygmentsBridge('html', style) - if self.config.html_pygments_dark_style is not None: - dark_style = self.config.html_pygments_dark_style - elif self.theme: + if self.theme: dark_style = self.theme.get_config('theme', 'pygments_dark_style', None) else: dark_style = None @@ -1200,7 +1198,6 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_config_value('html_style', None, 'html', [str]) app.add_config_value('html_logo', None, 'html', [str]) app.add_config_value('html_favicon', None, 'html', [str]) - app.add_config_value('html_pygments_dark_style', None, 'html', [str]) app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_js_files', [], 'html') app.add_config_value('html_static_path', [], 'html') diff --git a/tests/roots/test-theming/test_theme/test-theme/theme.conf b/tests/roots/test-theming/test_theme/test-theme/theme.conf index b7518bc9c..522adf1f8 100644 --- a/tests/roots/test-theming/test_theme/test-theme/theme.conf +++ b/tests/roots/test-theming/test_theme/test-theme/theme.conf @@ -1,3 +1,4 @@ [theme] inherit = classic sidebars = globaltoc.html, searchbox.html +pygments_dark = monokai \ No newline at end of file diff --git a/tests/test_build_html.py b/tests/test_build_html.py index f0da3ce30..a0072ca18 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -1525,19 +1525,9 @@ def test_html_pygments_for_classic_theme(app): assert style.__name__ == 'SphinxStyle' -@pytest.mark.sphinx('html', testroot='basic', - confoverrides={'html_pygments_dark_style': 'monokai'}) -def test_html_pygments_style_manually(app): - style = app.builder.highlighter.formatter_args.get('style') - assert style.__name__ == 'MonokaiStyle' - - -@pytest.mark.sphinx('html', testroot='basic', - confoverrides={'html_theme_options.pygments_dark_style': - 'solarized-dark'}) -def test_html_pygments_for_classic_theme(app): - style = app.builder.highlighter.formatter_args.get('style') - assert style.__name__ == 'SolarizedDarkStyle' +@pytest.mark.sphinx('html', testroot='basic') +def test_html_dark_pygments_style_default(app): + assert app.builder.dark_highlighter is None @pytest.mark.sphinx(testroot='basic', srcdir='validate_html_extra_path') diff --git a/tests/test_theming.py b/tests/test_theming.py index 7854cbd00..58402dcf9 100644 --- a/tests/test_theming.py +++ b/tests/test_theming.py @@ -117,6 +117,22 @@ def test_staticfiles(app, status, warning): assert '' in result +@pytest.mark.sphinx(testroot='theming', + confoverrides={'html_theme': 'test-theme'}) +def test_staticfiles(app, status, warning): + style = app.builder.highlighter.formatter_args.get('style') + assert style.__name__ == 'MonokaiStyle' + + app.build() + assert (app.outdir / '_static' / 'pygments_dark.css').exists() + + result = (app.outdir / 'index.html').read_text() + assert '' in result + assert ('') in result + + @pytest.mark.sphinx(testroot='theming') def test_theme_sidebars(app, status, warning): app.build() From 44fbd39ab2e41c2992f596f373b3f1020820f809 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 18:49:25 +0100 Subject: [PATCH 24/27] Fix checking wrong highlighter --- tests/test_theming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_theming.py b/tests/test_theming.py index 58402dcf9..ff5f57154 100644 --- a/tests/test_theming.py +++ b/tests/test_theming.py @@ -120,7 +120,7 @@ def test_staticfiles(app, status, warning): @pytest.mark.sphinx(testroot='theming', confoverrides={'html_theme': 'test-theme'}) def test_staticfiles(app, status, warning): - style = app.builder.highlighter.formatter_args.get('style') + style = app.builder.dark_highlighter.formatter_args.get('style') assert style.__name__ == 'MonokaiStyle' app.build() From f655835338fef746c5da2b481cd6794d6d84a3d2 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 19:46:37 +0100 Subject: [PATCH 25/27] Fix tests --- .../test-theming/test_theme/test-theme/theme.conf | 2 +- tests/test_theming.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/roots/test-theming/test_theme/test-theme/theme.conf b/tests/roots/test-theming/test_theme/test-theme/theme.conf index 522adf1f8..2ad2c33e1 100644 --- a/tests/roots/test-theming/test_theme/test-theme/theme.conf +++ b/tests/roots/test-theming/test_theme/test-theme/theme.conf @@ -1,4 +1,4 @@ [theme] inherit = classic sidebars = globaltoc.html, searchbox.html -pygments_dark = monokai \ No newline at end of file +pygments_dark_style = monokai diff --git a/tests/test_theming.py b/tests/test_theming.py index ff5f57154..affaf5408 100644 --- a/tests/test_theming.py +++ b/tests/test_theming.py @@ -11,8 +11,8 @@ import os import alabaster -import pytest +import pytest from sphinx.theming import ThemeError @@ -119,7 +119,7 @@ def test_staticfiles(app, status, warning): @pytest.mark.sphinx(testroot='theming', confoverrides={'html_theme': 'test-theme'}) -def test_staticfiles(app, status, warning): +def test_dark_style(app, status, warning): style = app.builder.dark_highlighter.formatter_args.get('style') assert style.__name__ == 'MonokaiStyle' @@ -127,10 +127,10 @@ def test_staticfiles(app, status, warning): assert (app.outdir / '_static' / 'pygments_dark.css').exists() result = (app.outdir / 'index.html').read_text() - assert '' in result - assert ('') in result + assert '' in result + assert ('') in result @pytest.mark.sphinx(testroot='theming') From bfee516aa743d3e843278ae4852d26c75937c985 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sat, 14 Mar 2020 19:56:20 +0100 Subject: [PATCH 26/27] Remove unused import --- sphinx/builders/html/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 0ce5da82b..e8db723f7 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -15,7 +15,7 @@ import sys import warnings from hashlib import md5 from os import path -from typing import Any, Dict, IO, Iterable, Iterator, List, Set, Tuple, Union +from typing import Any, Dict, IO, Iterable, Iterator, List, Set, Tuple from docutils import nodes from docutils.core import publish_parts From 37557d7552682da3629d6fe049fcbe53359e5031 Mon Sep 17 00:00:00 2001 From: Septatrix <24257556+Septatrix@users.noreply.github.com> Date: Sun, 15 Mar 2020 10:40:10 +0100 Subject: [PATCH 27/27] Revert change in highlighting --- sphinx/highlighting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 3f8601b61..4dc9ce543 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -10,7 +10,7 @@ from functools import partial from importlib import import_module -from typing import Any, Dict, Union, Iterable +from typing import Any, Dict from pygments import highlight from pygments.filters import ErrorToken @@ -157,9 +157,9 @@ class PygmentsBridge: # MEMO: this is done to escape Unicode chars with non-Unicode engines return texescape.hlescape(hlsource, self.latex_engine) - def get_stylesheet(self, *, css_selector: Union[str, Iterable[str]] = '.highlight') -> str: + def get_stylesheet(self) -> str: formatter = self.get_formatter() if self.dest == 'html': - return formatter.get_style_defs(css_selector) + return formatter.get_style_defs('.highlight') else: return formatter.get_style_defs() + _LATEX_ADD_STYLES