From c5d6942b88a5902a843ba41c74cb01c402413e48 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 3 Apr 2018 11:32:20 +0900 Subject: [PATCH 1/5] add_stylesheet() allows additional attributes --- CHANGES | 1 + sphinx/application.py | 36 ++++++++++++++++++++++++--------- sphinx/builders/html.py | 26 +++++++++++++++++++----- sphinx/themes/basic/layout.html | 4 ++-- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 02a817b22..42af691ae 100644 --- a/CHANGES +++ b/CHANGES @@ -58,6 +58,7 @@ Features added * LaTeX: new key ``'fvset'`` for :confval:`latex_elements`. For XeLaTeX/LuaLaTeX its default sets ``fanvyvrb`` to use normal, not small, fontsize in code-blocks (refs: #4793) +* ``app.add_stylesheet()`` allows additional attributes Bugs fixed ---------- diff --git a/sphinx/application.py b/sphinx/application.py index d6d66c925..14beb65d7 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -1001,13 +1001,27 @@ class Sphinx(object): StandaloneHTMLBuilder.script_files.append( posixpath.join('_static', filename)) - def add_stylesheet(self, filename, alternate=False, title=None): - # type: (unicode, bool, unicode) -> None + def add_stylesheet(self, filename, **kwargs): + # type: (unicode, **unicode) -> None """Register a stylesheet to include in the HTML output. Add *filename* to the list of CSS files that the default HTML template - will include. Like for :meth:`add_javascript`, the filename must be - relative to the HTML static path, or a full URI with scheme. + will include. The filename must be relative to the HTML static path, + or a full URI with scheme. The keyword arguments are also accepted for + attributes of ```` tag. + + Example:: + + app.add_stylesheet('custom.css') + # => + + app.add_stylesheet('print.css', media='print') + # => + + app.add_stylesheet('fancy.css', rel='alternate stylesheet', title='fancy') + # => .. versionadded:: 1.0 @@ -1017,16 +1031,20 @@ class Sphinx(object): arguments. The default is no title and *alternate* = ``False``. For more information, refer to the `documentation `__. + + .. versionchanged:: 1.8 + Allows keyword arguments as attributes of link tag. """ logger.debug('[app] adding stylesheet: %r', filename) from sphinx.builders.html import StandaloneHTMLBuilder, Stylesheet if '://' not in filename: filename = posixpath.join('_static', filename) - if alternate: - rel = u'alternate stylesheet' - else: - rel = u'stylesheet' - css = Stylesheet(filename, title, rel) # type: ignore + if kwargs.pop('alternate', None): + warnings.warn('The alternate option for app.add_stylesheet() is deprecated. ' + 'Please use rel="alternate stylesheet" option instead.', + RemovedInSphinx30Warning) + kwargs['rel'] = 'alternate stylesheet' + css = Stylesheet(filename, **kwargs) StandaloneHTMLBuilder.css_files.append(css) def add_latex_package(self, packagename, options=None): diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 257b35353..a001bedfc 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -50,6 +50,7 @@ from sphinx.util.matching import patmatch, Matcher, DOTFILES from sphinx.util.nodes import inline_all_toctrees from sphinx.util.osutil import SEP, os_path, relative_uri, ensuredir, \ movefile, copyfile +from sphinx.util.pycompat import htmlescape from sphinx.writers.html import HTMLWriter, HTMLTranslator if False: @@ -101,7 +102,7 @@ class CSSContainer(list): if isinstance(obj, Stylesheet): super(CSSContainer, self).append(obj) else: - super(CSSContainer, self).append(Stylesheet(obj, None, 'stylesheet')) # type: ignore # NOQA + super(CSSContainer, self).append(Stylesheet(obj)) def insert(self, index, obj): # type: (int, Union[unicode, Stylesheet]) -> None @@ -111,7 +112,7 @@ class CSSContainer(list): if isinstance(obj, Stylesheet): super(CSSContainer, self).insert(index, obj) else: - super(CSSContainer, self).insert(index, Stylesheet(obj, None, 'stylesheet')) # type: ignore # NOQA + super(CSSContainer, self).insert(index, Stylesheet(obj)) def extend(self, other): # type: ignore # type: (List[Union[unicode, Stylesheet]]) -> None @@ -144,12 +145,18 @@ class Stylesheet(text_type): its filename (str). """ - def __new__(cls, filename, title, rel): + attributes = None # type: Dict[unicode, unicode] + filename = None # type: unicode + + def __new__(cls, filename, *args, **attributes): # type: (unicode, unicode, unicode) -> None self = text_type.__new__(cls, filename) # type: ignore self.filename = filename - self.title = title - self.rel = rel + self.attributes = attributes + self.attributes.setdefault('rel', 'stylesheet') + if args: # old style arguments (rel, title) + self.attributes['rel'] = args[0] + self.attributes['title'] = args[1] return self @@ -988,6 +995,15 @@ class StandaloneHTMLBuilder(Builder): return uri ctx['pathto'] = pathto + def css_tag(css): + # type: (Stylesheet) -> unicode + attrs = ['%s="%s"' % (key, htmlescape(value, True)) + for key, value in css.attributes.items() + if value is not None] + attrs.append('href="%s"' % pathto(css.filename, resource=True)) + return '' % ' '.join(attrs) + ctx['css_tag'] = css_tag + def hasdoc(name): # type: (unicode) -> bool if name in self.env.all_docs: diff --git a/sphinx/themes/basic/layout.html b/sphinx/themes/basic/layout.html index 68c7d9e51..a25a18b90 100644 --- a/sphinx/themes/basic/layout.html +++ b/sphinx/themes/basic/layout.html @@ -97,8 +97,8 @@ {%- for css in css_files %} - {%- if css|attr("rel") %} - + {%- if css|attr("filename") %} + {{ css_tag(css) }} {%- else %} {%- endif %} From 99fbd44e20efe53d663e3fe4356ceb6b9d72f85d Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 3 Apr 2018 23:45:57 +0900 Subject: [PATCH 2/5] Store stylesheets as an instance variable of HTML builder So far, CSS files are stored as a class variable of HTML builder. Not to have status globally, this changes it to an instance varable. --- sphinx/application.py | 4 +--- sphinx/builders/html.py | 16 ++++++++++++++-- sphinx/registry.py | 6 ++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/sphinx/application.py b/sphinx/application.py index 14beb65d7..d089fd3e6 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -1036,7 +1036,6 @@ class Sphinx(object): Allows keyword arguments as attributes of link tag. """ logger.debug('[app] adding stylesheet: %r', filename) - from sphinx.builders.html import StandaloneHTMLBuilder, Stylesheet if '://' not in filename: filename = posixpath.join('_static', filename) if kwargs.pop('alternate', None): @@ -1044,8 +1043,7 @@ class Sphinx(object): 'Please use rel="alternate stylesheet" option instead.', RemovedInSphinx30Warning) kwargs['rel'] = 'alternate stylesheet' - css = Stylesheet(filename, **kwargs) - StandaloneHTMLBuilder.css_files.append(css) + self.registry.add_css_files(filename, **kwargs) def add_latex_package(self, packagename, options=None): # type: (unicode, unicode) -> None diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index a001bedfc..c42669675 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -248,8 +248,6 @@ class StandaloneHTMLBuilder(Builder): # This is a class attribute because it is mutated by Sphinx.add_javascript. script_files = ['_static/jquery.js', '_static/underscore.js', '_static/doctools.js'] # type: List[unicode] - # Ditto for this one (Sphinx.add_stylesheet). - css_files = CSSContainer() # type: List[Dict[unicode, unicode]] imgpath = None # type: unicode domain_indices = [] # type: List[Tuple[unicode, Type[Index], List[Tuple[unicode, List[List[Union[unicode, int]]]]], bool]] # NOQA @@ -257,6 +255,13 @@ class StandaloneHTMLBuilder(Builder): # cached publisher object for snippets _publisher = None + def __init__(self, app): + # type: (Sphinx) -> None + super(StandaloneHTMLBuilder, self).__init__(app) + + # CSS files + self.css_files = CSSContainer() # type: List[Dict[unicode, unicode]] + def init(self): # type: () -> None self.build_info = self.create_build_info() @@ -269,6 +274,7 @@ class StandaloneHTMLBuilder(Builder): self.init_templates() self.init_highlighter() + self.init_css_files() if self.config.html_file_suffix is not None: self.out_suffix = self.config.html_file_suffix @@ -331,6 +337,11 @@ class StandaloneHTMLBuilder(Builder): self.highlighter = PygmentsBridge('html', style, self.config.trim_doctest_flags) + def init_css_files(self): + # type: () -> None + for filename, attrs in self.app.registry.css_files: + self.css_files.append(Stylesheet(filename, **attrs)) # type: ignore + @property def default_translator_class(self): # type: () -> nodes.NodeVisitor @@ -1332,6 +1343,7 @@ class SerializingHTMLBuilder(StandaloneHTMLBuilder): self.templates = None # no template bridge necessary self.init_templates() self.init_highlighter() + self.init_css_files() self.use_index = self.get_builder_config('use_index', 'html') def get_target_uri(self, docname, typ=None): diff --git a/sphinx/registry.py b/sphinx/registry.py index ab4cfce70..dc42219ae 100644 --- a/sphinx/registry.py +++ b/sphinx/registry.py @@ -65,6 +65,9 @@ class SphinxComponentRegistry(object): #: autodoc documenters; a dict of documenter name -> documenter class self.documenters = {} # type: Dict[unicode, Type[Documenter]] + #: css_files; a list of tuple of filename and attributes + self.css_files = [] # type: List[Tuple[unicode, Dict[unicode, unicode]]] + #: domains; a dict of domain name -> domain class self.domains = {} # type: Dict[unicode, Type[Domain]] @@ -412,6 +415,9 @@ class SphinxComponentRegistry(object): # type: (Type, Callable[[Any, unicode, Any], Any]) -> None self.autodoc_attrgettrs[typ] = attrgetter + def add_css_files(self, filename, **attributes): + self.css_files.append((filename, attributes)) + def add_latex_package(self, name, options): # type: (unicode, unicode) -> None logger.debug('[app] adding latex package: %r', name) From 3afc72fba41f945610263be79d14c95d21478248 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 3 Apr 2018 23:55:39 +0900 Subject: [PATCH 3/5] Rename add_stylesheet() to add_css_file() --- CHANGES | 2 +- doc/extdev/index.rst | 5 +++++ sphinx/application.py | 38 +++++++++++++++++++++++++++----------- sphinx/deprecation.py | 4 ++++ 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CHANGES b/CHANGES index 42af691ae..0788e1c77 100644 --- a/CHANGES +++ b/CHANGES @@ -35,6 +35,7 @@ Deprecated * #2157: helper function ``warn()`` for HTML themes is deprecated * ``env._nitpick_ignore`` is deprecated * ``app.override_domain()`` is deprecated +* ``app.add_stylesheet()`` is deprecated For more details, see `deprecation APIs list `_ @@ -58,7 +59,6 @@ Features added * LaTeX: new key ``'fvset'`` for :confval:`latex_elements`. For XeLaTeX/LuaLaTeX its default sets ``fanvyvrb`` to use normal, not small, fontsize in code-blocks (refs: #4793) -* ``app.add_stylesheet()`` allows additional attributes Bugs fixed ---------- diff --git a/doc/extdev/index.rst b/doc/extdev/index.rst index 78414cd09..a4776275f 100644 --- a/doc/extdev/index.rst +++ b/doc/extdev/index.rst @@ -113,6 +113,11 @@ The following is a list of deprecated interface. - (will be) Removed - Alternatives + * - :meth:`~sphinx.application.Sphinx.add_stylesheet()` + - 1.8 + - 4.0 + - :meth:`~sphinx.application.Sphinx.add_css_file()` + * - ``sphinx.application.Sphinx.override_domain()`` - 1.8 - 3.0 diff --git a/sphinx/application.py b/sphinx/application.py index d089fd3e6..d19744cf7 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -27,7 +27,9 @@ from six.moves import cStringIO import sphinx from sphinx import package_dir, locale from sphinx.config import Config -from sphinx.deprecation import RemovedInSphinx20Warning, RemovedInSphinx30Warning +from sphinx.deprecation import ( + RemovedInSphinx20Warning, RemovedInSphinx30Warning, RemovedInSphinx40Warning +) from sphinx.environment import BuildEnvironment from sphinx.errors import ( ApplicationError, ConfigError, ExtensionError, VersionRequirementError @@ -1001,7 +1003,7 @@ class Sphinx(object): StandaloneHTMLBuilder.script_files.append( posixpath.join('_static', filename)) - def add_stylesheet(self, filename, **kwargs): + def add_css_file(self, filename, **kwargs): # type: (unicode, **unicode) -> None """Register a stylesheet to include in the HTML output. @@ -1012,14 +1014,14 @@ class Sphinx(object): Example:: - app.add_stylesheet('custom.css') + app.add_css_file('custom.css') # => - app.add_stylesheet('print.css', media='print') + app.add_css_file('print.css', media='print') # => - app.add_stylesheet('fancy.css', rel='alternate stylesheet', title='fancy') + app.add_css_file('fancy.css', rel='alternate stylesheet', title='fancy') # => @@ -1033,18 +1035,32 @@ class Sphinx(object): `__. .. versionchanged:: 1.8 - Allows keyword arguments as attributes of link tag. + Renamed from ``app.add_stylesheet()``. + And it allows keyword arguments as attributes of link tag. """ logger.debug('[app] adding stylesheet: %r', filename) if '://' not in filename: filename = posixpath.join('_static', filename) - if kwargs.pop('alternate', None): - warnings.warn('The alternate option for app.add_stylesheet() is deprecated. ' - 'Please use rel="alternate stylesheet" option instead.', - RemovedInSphinx30Warning) - kwargs['rel'] = 'alternate stylesheet' self.registry.add_css_files(filename, **kwargs) + def add_stylesheet(self, filename, alternate=False, title=None): + # type: (unicode, bool, unicode) -> None + """An alias of :meth:`add_css_file`.""" + warnings.warn('The app.add_stylesheet() is deprecated. ' + 'Please use app.add_css_file() instead.', + RemovedInSphinx40Warning) + + attributes = {} # type: Dict[unicode, unicode] + if alternate: + attributes['rel'] = 'alternate stylesheet' + else: + attributes['rel'] = 'stylesheet' + + if title: + attributes['title'] = title + + self.add_css_file(filename, **attributes) + def add_latex_package(self, packagename, options=None): # type: (unicode, unicode) -> None r"""Register a package to include in the LaTeX source code. diff --git a/sphinx/deprecation.py b/sphinx/deprecation.py index 1b0e862f0..d4067055a 100644 --- a/sphinx/deprecation.py +++ b/sphinx/deprecation.py @@ -33,6 +33,10 @@ class RemovedInSphinx30Warning(PendingDeprecationWarning): pass +class RemovedInSphinx40Warning(PendingDeprecationWarning): + pass + + RemovedInNextVersionWarning = RemovedInSphinx18Warning From 5efecd2150d615aaa6b788d6591dff5791150c3c Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 8 Feb 2018 21:46:59 +0900 Subject: [PATCH 4/5] Add :confval:`html_css_files` --- CHANGES | 1 + doc/config.rst | 19 +++++++++++--- sphinx/builders/html.py | 26 ++++++++++++++++--- tests/roots/test-html_assets/conf.py | 2 ++ .../test-html_assets/static/js/custom.js | 0 tests/test_build_html.py | 13 +++++++--- 6 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 tests/roots/test-html_assets/static/js/custom.js diff --git a/CHANGES b/CHANGES index 0788e1c77..11cdf4ea7 100644 --- a/CHANGES +++ b/CHANGES @@ -59,6 +59,7 @@ Features added * LaTeX: new key ``'fvset'`` for :confval:`latex_elements`. For XeLaTeX/LuaLaTeX its default sets ``fanvyvrb`` to use normal, not small, fontsize in code-blocks (refs: #4793) +* Add :confval:`html_css_files` for adding CSS files from configuration Bugs fixed ---------- diff --git a/doc/config.rst b/doc/config.rst index 04f3c2d7c..97e43e645 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -59,9 +59,6 @@ Important points to note: Note that the current builder tag is not available in ``conf.py``, as it is created *after* the builder is initialized. -.. seealso:: Additional configurations, such as adding stylesheets, - javascripts, builders, etc. can be made through the :doc:`/extdev/appapi`. - General configuration --------------------- @@ -846,6 +843,22 @@ 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_css_files + + A list of CSS files. The entry must be a *filename* string or a tuple + containing the *filename* string and the *attributes* dictionary. The + *filename* must be relative to the :confval:`html_static_path`, or a full URI + with scheme like ``http://example.org/style.css``. The *attributes* is used + for attributes of ```` tag. It defaults to an empty list. + + Example:: + + html_css_files = ['custom.css' + 'https://example.com/css/custom.css', + ('print.css', {'media': 'print'})] + + .. versionadded:: 1.8 + .. confval:: html_static_path A list of paths that contain custom static files (such as style diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index c42669675..25538ac17 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -154,6 +154,7 @@ class Stylesheet(text_type): self.filename = filename self.attributes = attributes self.attributes.setdefault('rel', 'stylesheet') + self.attributes.setdefault('type', 'text/css') if args: # old style arguments (rel, title) self.attributes['rel'] = args[0] self.attributes['title'] = args[1] @@ -342,6 +343,22 @@ class StandaloneHTMLBuilder(Builder): for filename, attrs in self.app.registry.css_files: self.css_files.append(Stylesheet(filename, **attrs)) # type: ignore + for entry in self.get_builder_config('css_files', 'html'): + if isinstance(entry, string_types): + filename = entry + attrs = {} + else: + try: + filename, attrs = entry + except (TypeError, ValueError): + logger.warning('invalid css_file: %r', entry) + continue + + if '://' not in filename: + filename = path.join('_static', filename) + + self.css_files.append(Stylesheet(filename, **attrs)) # type: ignore + @property def default_translator_class(self): # type: () -> nodes.NodeVisitor @@ -1008,9 +1025,11 @@ class StandaloneHTMLBuilder(Builder): def css_tag(css): # type: (Stylesheet) -> unicode - attrs = ['%s="%s"' % (key, htmlescape(value, True)) - for key, value in css.attributes.items() - if value is not None] + attrs = [] + for key in sorted(css.attributes): + value = css.attributes[key] + if value is not None: + attrs.append('%s="%s"' % (key, htmlescape(value, True))) attrs.append('href="%s"' % pathto(css.filename, resource=True)) return '' % ' '.join(attrs) ctx['css_tag'] = css_tag @@ -1468,6 +1487,7 @@ def setup(app): app.add_config_value('html_style', None, 'html', string_classes) app.add_config_value('html_logo', None, 'html', string_classes) app.add_config_value('html_favicon', None, 'html', string_classes) + app.add_config_value('html_css_files', [], 'html') app.add_config_value('html_static_path', [], 'html') app.add_config_value('html_extra_path', [], 'html') app.add_config_value('html_last_updated_fmt', None, 'html', string_classes) diff --git a/tests/roots/test-html_assets/conf.py b/tests/roots/test-html_assets/conf.py index a17e417a3..c61f0b42c 100644 --- a/tests/roots/test-html_assets/conf.py +++ b/tests/roots/test-html_assets/conf.py @@ -6,4 +6,6 @@ version = '1.4.4' html_static_path = ['static', 'subdir'] html_extra_path = ['extra', 'subdir'] +html_css_files = ['css/style.css', + ('https://example.com/custom.css', {'title': 'title', 'media': 'print'})] exclude_patterns = ['**/_build', '**/.htpasswd'] diff --git a/tests/roots/test-html_assets/static/js/custom.js b/tests/roots/test-html_assets/static/js/custom.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 1ee5eb473..655feec03 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -1094,9 +1094,10 @@ def test_html_assets(app): assert not (app.outdir / '_static' / '.htpasswd').exists() assert (app.outdir / '_static' / 'API.html').exists() assert (app.outdir / '_static' / 'API.html').text() == 'Sphinx-1.4.4' - assert (app.outdir / '_static' / 'css/style.css').exists() + assert (app.outdir / '_static' / 'css' / 'style.css').exists() + assert (app.outdir / '_static' / 'js' / 'custom.js').exists() assert (app.outdir / '_static' / 'rimg.png').exists() - assert not (app.outdir / '_static' / '_build/index.html').exists() + assert not (app.outdir / '_static' / '_build' / 'index.html').exists() assert (app.outdir / '_static' / 'background.png').exists() assert not (app.outdir / '_static' / 'subdir' / '.htaccess').exists() assert not (app.outdir / '_static' / 'subdir' / '.htpasswd').exists() @@ -1107,11 +1108,17 @@ def test_html_assets(app): assert (app.outdir / 'API.html_t').exists() assert (app.outdir / 'css/style.css').exists() assert (app.outdir / 'rimg.png').exists() - assert not (app.outdir / '_build/index.html').exists() + assert not (app.outdir / '_build' / 'index.html').exists() assert (app.outdir / 'background.png').exists() assert (app.outdir / 'subdir' / '.htaccess').exists() assert not (app.outdir / 'subdir' / '.htpasswd').exists() + # html_css_files + content = (app.outdir / 'index.html').text() + assert '' in content + assert ('' in content) + @pytest.mark.sphinx('html', testroot='basic', confoverrides={'html_copy_source': False}) def test_html_copy_source(app): From bd2967f1d2e95bcbf06894d4a83814b701f2ab0c Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 11 Feb 2018 23:14:54 +0900 Subject: [PATCH 5/5] Add :confval:`epub_css_files` --- CHANGES | 3 ++- doc/config.rst | 8 ++++++++ sphinx/builders/epub3.py | 1 + tests/test_build_epub.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 11cdf4ea7..5012b3eea 100644 --- a/CHANGES +++ b/CHANGES @@ -59,7 +59,8 @@ Features added * LaTeX: new key ``'fvset'`` for :confval:`latex_elements`. For XeLaTeX/LuaLaTeX its default sets ``fanvyvrb`` to use normal, not small, fontsize in code-blocks (refs: #4793) -* Add :confval:`html_css_files` for adding CSS files from configuration +* Add :confval:`html_css_files` and :confval:`epub_css_files` for adding CSS + files from configuration Bugs fixed ---------- diff --git a/doc/config.rst b/doc/config.rst index 97e43e645..46958cde6 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -1524,6 +1524,14 @@ the `Dublin Core metadata `_. .. versionadded:: 1.1 +.. confval:: epub_css_files + + A list of CSS files. The entry must be a *filename* string or a tuple + containing the *filename* string and the *attributes* dictionary. For more + information, see :confval:`html_css_files`. + + .. versionadded:: 1.8 + .. confval:: epub_guide Meta data for the guide element of :file:`content.opf`. This is a diff --git a/sphinx/builders/epub3.py b/sphinx/builders/epub3.py index 752a3990d..895d2f5f5 100644 --- a/sphinx/builders/epub3.py +++ b/sphinx/builders/epub3.py @@ -247,6 +247,7 @@ def setup(app): app.add_config_value('epub_guide', (), 'env') app.add_config_value('epub_pre_files', [], 'env') app.add_config_value('epub_post_files', [], 'env') + app.add_config_value('epub_css_files', lambda config: config.html_css_files, 'epub') app.add_config_value('epub_exclude_files', [], 'env') app.add_config_value('epub_tocdepth', 3, 'env') app.add_config_value('epub_tocdup', True, 'env') diff --git a/tests/test_build_epub.py b/tests/test_build_epub.py index 2f09f6d5a..24450089b 100644 --- a/tests/test_build_epub.py +++ b/tests/test_build_epub.py @@ -317,6 +317,34 @@ def test_epub_writing_mode(app): assert 'writing-mode: vertical-rl;' in css +@pytest.mark.sphinx('epub', testroot='html_assets') +def test_epub_assets(app): + app.builder.build_all() + + # epub_sytlesheets (same as html_css_files) + content = (app.outdir / 'index.xhtml').text() + assert ('' + in content) + assert ('' in content) + + +@pytest.mark.sphinx('epub', testroot='html_assets', + confoverrides={'epub_css_files': ['css/epub.css']}) +def test_epub_css_files(app): + app.builder.build_all() + + # epub_css_files + content = (app.outdir / 'index.xhtml').text() + assert '' in content + + # files in html_css_files are not outputed + assert ('' + not in content) + assert ('' not in content) + + @pytest.mark.sphinx('epub') def test_run_epubcheck(app): app.build()