Merge pull request #4953 from tk0miya/refactor_html_css_files

refactor: Convert the form of html_css_files
This commit is contained in:
Takeshi KOMIYA 2018-05-12 22:38:36 +09:00 committed by GitHub
commit 417ad035c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 17 deletions

View File

@ -1057,8 +1057,6 @@ class Sphinx(object):
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)
self.registry.add_css_files(filename, **kwargs)
def add_stylesheet(self, filename, alternate=False, title=None):

View File

@ -13,6 +13,8 @@
from collections import namedtuple
from os import path
from six import string_types
from sphinx import package_dir
from sphinx.builders import _epub_base
from sphinx.config import string_classes, ENUM
@ -24,9 +26,10 @@ from sphinx.util.osutil import make_filename
if False:
# For type annotation
from typing import Any, Dict, Iterable, List # NOQA
from typing import Any, Dict, Iterable, List, Tuple # NOQA
from docutils import nodes # NOQA
from sphinx.application import Sphinx # NOQA
from sphinx.config import Config # NOQA
logger = logging.getLogger(__name__)
@ -226,6 +229,24 @@ class Epub3Builder(_epub_base.EpubBuilder):
self.files.append(outname)
def convert_epub_css_files(app, config):
# type: (Sphinx, Config) -> None
"""This converts string styled epub_css_files to tuple styled one."""
epub_css_files = [] # type: List[Tuple[unicode, Dict]]
for entry in config.epub_css_files:
if isinstance(entry, string_types):
epub_css_files.append((entry, {}))
else:
try:
filename, attrs = entry
epub_css_files.append((filename, attrs))
except Exception:
logger.warning(__('invalid css_file: %r, ignored'), entry)
continue
config.epub_css_files = epub_css_files # type: ignore
def setup(app):
# type: (Sphinx) -> Dict[unicode, Any]
app.add_builder(Epub3Builder)
@ -261,6 +282,9 @@ def setup(app):
app.add_config_value('epub_writing_mode', 'horizontal', 'epub',
ENUM('horizontal', 'vertical'))
# event handlers
app.connect('config-inited', convert_epub_css_files)
return {
'version': 'builtin',
'parallel_read_safe': True,

View File

@ -341,23 +341,17 @@ class StandaloneHTMLBuilder(Builder):
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
self.add_css_file(filename, **attrs)
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
for filename, attrs in self.get_builder_config('css_files', 'html'):
self.add_css_file(filename, **attrs)
if '://' not in filename:
filename = posixpath.join('_static', filename)
def add_css_file(self, filename, **kwargs):
# type: (unicode, **unicode) -> None
if '://' not in filename:
filename = posixpath.join('_static', filename)
self.css_files.append(Stylesheet(filename, **attrs)) # type: ignore
self.css_files.append(Stylesheet(filename, **kwargs)) # type: ignore
@property
def default_translator_class(self):
@ -1467,6 +1461,24 @@ class JSONHTMLBuilder(SerializingHTMLBuilder):
SerializingHTMLBuilder.init(self)
def convert_html_css_files(app, config):
# type: (Sphinx, Config) -> None
"""This converts string styled html_css_files to tuple styled one."""
html_css_files = [] # type: List[Tuple[unicode, Dict]]
for entry in config.html_css_files:
if isinstance(entry, string_types):
html_css_files.append((entry, {}))
else:
try:
filename, attrs = entry
html_css_files.append((filename, attrs))
except Exception:
logger.warning(__('invalid css_file: %r, ignored'), entry)
continue
config.html_css_files = html_css_files # type: ignore
def setup(app):
# type: (Sphinx) -> Dict[unicode, Any]
# builders
@ -1515,6 +1527,9 @@ def setup(app):
app.add_config_value('html_scaled_image_link', True, 'html')
app.add_config_value('html_experimental_html5_writer', None, 'html')
# event handlers
app.connect('config-inited', convert_html_css_files)
return {
'version': 'builtin',
'parallel_read_safe': True,