From 99fbd44e20efe53d663e3fe4356ceb6b9d72f85d Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 3 Apr 2018 23:45:57 +0900 Subject: [PATCH] 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 14beb65d792..d089fd3e674 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -1036,7 +1036,6 @@ def add_stylesheet(self, filename, **kwargs): 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 @@ def add_stylesheet(self, filename, **kwargs): '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 a001bedfc19..c42669675fc 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 @@ def init(self): 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 @@ def init_highlighter(self): 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 @@ def init(self): 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 ab4cfce70dc..dc42219aebf 100644 --- a/sphinx/registry.py +++ b/sphinx/registry.py @@ -65,6 +65,9 @@ def __init__(self): #: 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 @@ def add_autodoc_attrgetter(self, typ, attrgetter): # 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)