Rewrite and simplify stylesheet handling

Thanks to tk0miya's comment, I learnt one can add stuff to template
blocks, that allows a much simpler stylesheet configuration, considering
that changes at the template level will be more... well, low-level.
Hopefully this is now acceptable.
This commit is contained in:
Jellby
2017-04-15 17:07:33 +02:00
parent 1cfed262d2
commit e59a8028d7
9 changed files with 38 additions and 70 deletions

View File

@@ -310,9 +310,11 @@ package.
.. versionadded:: 1.0
.. versionchanged:: 1.4
.. versionchanged:: 1.6
Optional ``alternate`` and/or ``title`` attributes can be supplied with
the *alternate* (of boolean type) and *title* (a string) arguments.
the *alternate* (of boolean type) and *title* (a string) arguments. The
default is no title and *alternate*=``False`` (see `this explanation
<https://developer.mozilla.org/en-US/docs/Web/CSS/Alternative_style_sheets>`_).
.. method:: Sphinx.add_latex_package(packagename, options=None)

View File

@@ -200,27 +200,6 @@ Overriding works like this::
{% set script_files = script_files + ["_static/myscript.js"] %}
.. data:: css_files
Similar to :data:`script_files`, for CSS files::
{% set css_files = css_files + ["_static/mystyle.css"] %}
.. data:: css_props
.. versionadded:: 1.6
An optional dict where you can specify ``alternate`` and/or ``title``
attributes for the css files. The keys are the css filenames, the values
are dicts themselves:
{% set _dummy = css_props.update( {"_static/mystyle.css": {"alternate":False, "title":"Default"} }) %}
The default is no title and ``alternate=False``, but if only ``title`` is
given, the default is ``alternate=True``. If ``alternate`` is ``True``, it
will be translated to ``rel="alternate stylesheet"``, otherwise it will be
``rel="stylesheet"``.
Helper Functions
~~~~~~~~~~~~~~~~

View File

@@ -724,21 +724,18 @@ class Sphinx(object):
StandaloneHTMLBuilder.script_files.append(
posixpath.join('_static', filename))
def add_stylesheet(self, filename, alternate=None, title=None):
# type: (unicode, unicode, unicode) -> None
def add_stylesheet(self, filename, alternate=False, title=None):
# type: (unicode, bool, unicode) -> None
logger.debug('[app] adding stylesheet: %r', filename)
from sphinx.builders.html import StandaloneHTMLBuilder
props = {} # type: Dict[unicode, Union[unicode, bool]]
if alternate is not None:
props['alternate'] = bool(alternate)
if title is not None:
props['title'] = title
if '://' in filename:
fname = filename
else:
fname = posixpath.join('_static', filename)
StandaloneHTMLBuilder.css_files.append(fname)
StandaloneHTMLBuilder.css_props[fname] = props
props = {'rel': 'stylesheet',
'filename': filename,
'title': title} # type: Dict[unicode, unicode]
if '://' not in filename:
props['filename'] = posixpath.join('_static', filename)
if alternate:
props['rel'] = 'alternate stylesheet'
StandaloneHTMLBuilder.css_files.append(props)
def add_latex_package(self, packagename, options=None):
# type: (unicode, unicode) -> None

View File

@@ -113,9 +113,8 @@ 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 these ones (Sphinx.add_stylesheet).
css_files = [] # type: List[unicode]
css_props = {} # type: Dict[unicode, Dict[unicode, Union[unicode, bool]]]
# Ditto for this one (Sphinx.add_stylesheet).
css_files = [] # 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
@@ -389,7 +388,6 @@ class StandaloneHTMLBuilder(Builder):
script_files = self.script_files,
language = self.config.language,
css_files = self.css_files,
css_props = self.css_props,
sphinx_version = __display_version__,
style = stylename,
rellinks = rellinks,

View File

@@ -105,24 +105,8 @@
{%- macro css() %}
<link rel="stylesheet" href="{{ pathto('_static/' + style, 1) }}" type="text/css" />
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', 1) }}" type="text/css" />
{#- Process the custom css files, with their alternate and title properties if available #}
{%- for cssfile in css_files %}
{%- if css_props[cssfile] is not defined %}
{%- set alt = False %}
{%- set csstitle = undefined %}
{%- else %}
{%- set csstitle = css_props[cssfile].title %}
{%- set alt = (csstitle is defined) %}
{%- if css_props[cssfile].alternate is defined %}
{%- set alt = css_props[cssfile].alternate %}
{%- endif %}
{%- endif %}
{%- if alt %}
{%- set rel = "alternate stylesheet" %}
{%- else %}
{%- set rel = "stylesheet" %}
{%- endif %}
<link rel="{{ rel }}" href="{{ pathto(cssfile, 1) }}" type="text/css"{% if csstitle is defined %} title="{{ csstitle }}"{% endif %} />
{%- for css in css_files %}
<link rel="{{ css.rel }}" href="{{ pathto(css.filename, 1) }}" type="text/css"{% if css.title is not none %} title="{{ css.title }}"{% endif %} />
{%- endfor %}
{%- endmacro %}
@@ -133,9 +117,13 @@
{%- block htmltitle %}
<title>{{ title|striptags|e }}{{ titlesuffix }}</title>
{%- endblock %}
{%- block csss %}
{{ css() }}
{%- endblock %}
{%- if not embedded %}
{%- block scripts %}
{{ script() }}
{%- endblock %}
{%- if use_opensearch %}
<link rel="search" type="application/opensearchdescription+xml"
title="{% trans docstitle=docstitle|e %}Search within {{ docstitle }}{% endtrans %}"

View File

@@ -10,7 +10,10 @@
#}
{%- extends "basic/layout.html" %}
{% set script_files = script_files + ['_static/theme_extras.js'] %}
{% set css_files = css_files + ['_static/print.css'] %}
{%- block csss %}
{{ super() }}
<link rel="stylesheet" href="_static/print.css" type="text/css" />
{%- endblock %}
{# do not display relbars #}
{% block relbar1 %}{% endblock %}
{% block relbar2 %}{% endblock %}

View File

@@ -1,7 +1,8 @@
{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/more_persistent.css", "_static/more_persistent2.css", "_static/more_default.css", "_static/more_alternate1.css", "_static/more_alternate2.css"] %}
{% set _dummy = css_props.update(
{"_static/more_default.css": {"title": "Default", "alternate": False},
"_static/more_alternate1.css": {"title": "Alternate"},
"_static/more_alternate2.css": {"alternate": True} }
) %}
{%- block csss %}
{{ super() }}
<link rel="stylesheet" href="_static/more_persistent.css" type="text/css" />
<link rel="stylesheet" href="_static/more_default.css" type="text/css" title="Default" />
<link rel="alternate stylesheet" href="_static/more_alternate1.css" type="text/css" title="Alternate" />
<link rel="alternate stylesheet" href="_static/more_alternate2.css" type="text/css" />
{%- endblock %}

View File

@@ -6,7 +6,7 @@ templates_path = ['_templates']
def setup(app):
app.add_stylesheet('persistent.css')
app.add_stylesheet('default.css', title="Default", alternate=False)
app.add_stylesheet('alternate1.css', title="Alternate")
app.add_stylesheet('default.css', title="Default")
app.add_stylesheet('alternate1.css', title="Alternate", alternate=True)
app.add_stylesheet('alternate2.css', alternate=True)

View File

@@ -1181,7 +1181,7 @@ def test_html_inventory(app):
'http://example.com/index.html',
'The basic Sphinx documentation for testing')
@pytest.mark.sphinx('html', testroot='directives-raw')
def test_html_raw_directive(app, status, warning):
app.builder.build_all()
@@ -1194,8 +1194,8 @@ def test_html_raw_directive(app, status, warning):
# with substitution
assert '<p>HTML: abc def ghi</p>' in result
assert '<p>LaTeX: abc ghi</p>' in result
@pytest.mark.parametrize("fname,expect", flat_dict({
'index.html': [
(".//link[@href='_static/persistent.css']"