Skip to content

Commit

Permalink
Store stylesheets as an instance variable of HTML builder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tk0miya committed Apr 6, 2018
1 parent c5d6942 commit 99fbd44
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 1 addition & 3 deletions sphinx/application.py
Expand Up @@ -1036,16 +1036,14 @@ 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):
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)
self.registry.add_css_files(filename, **kwargs)

def add_latex_package(self, packagename, options=None):
# type: (unicode, unicode) -> None
Expand Down
16 changes: 14 additions & 2 deletions sphinx/builders/html.py
Expand Up @@ -248,15 +248,20 @@ 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

# 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()
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions sphinx/registry.py
Expand Up @@ -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]]

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 99fbd44

Please sign in to comment.