Move CSS properties to a different variable

This commit is contained in:
Jellby
2016-03-06 13:49:57 +01:00
parent a3551a811b
commit ea219a04ed
5 changed files with 41 additions and 37 deletions

View File

@@ -206,17 +206,20 @@ Overriding works like this::
{% set css_files = css_files + ["_static/mystyle.css"] %} {% set css_files = css_files + ["_static/mystyle.css"] %}
.. versionchanged:: 1.4 .. data:: css_props
Optionally, ``alternate`` and/or ``title`` attributes can be provided by
supplying a Python dictionary, in which case the filename is given in the
``filename`` key::
{% set css_files = css_files + [{"filename":"_static/mystyle.css", "alternate":False, "title":"Default"}] %} .. versionadded:: 1.4
The default is no title and ``alternate=False``, but if only ``title`` is An optional dict where you can specify ``alternate`` and/or ``title``
given, the default is ``alternate=True``. If ``alternate`` is ``True``, it attributes for the css files. The keys are the css filenames, the values
will be translated to ``rel="alternate stylesheet"``, otherwise it will be are dicts themselves:
``rel="stylesheet"``.
{% 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 Helper Functions
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

View File

@@ -745,16 +745,17 @@ class Sphinx(object):
def add_stylesheet(self, filename, alternate=None, title=None): def add_stylesheet(self, filename, alternate=None, title=None):
self.debug('[app] adding stylesheet: %r', filename) self.debug('[app] adding stylesheet: %r', filename)
from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.builders.html import StandaloneHTMLBuilder
item = {} props = {}
if alternate is not None: if alternate is not None:
item['alternate'] = bool(alternate) props['alternate'] = bool(alternate)
if title is not None: if title is not None:
item['title'] = title props['title'] = title
if '://' in filename: if '://' in filename:
item['filename'] = filename fname = filename
else: else:
item['filename'] = posixpath.join('_static', filename) fname = posixpath.join('_static', filename)
StandaloneHTMLBuilder.css_files.append(item) StandaloneHTMLBuilder.css_files.append(fname)
StandaloneHTMLBuilder.css_props[fname] = props
def add_latex_package(self, packagename, options=None): def add_latex_package(self, packagename, options=None):
self.debug('[app] adding latex package: %r', packagename) self.debug('[app] adding latex package: %r', packagename)

View File

@@ -84,8 +84,9 @@ class StandaloneHTMLBuilder(Builder):
# This is a class attribute because it is mutated by Sphinx.add_javascript. # This is a class attribute because it is mutated by Sphinx.add_javascript.
script_files = ['_static/jquery.js', '_static/underscore.js', script_files = ['_static/jquery.js', '_static/underscore.js',
'_static/doctools.js'] '_static/doctools.js']
# Dito for this one. # Ditto for these ones (Sphinx.add_stylesheet).
css_files = [] css_files = []
css_props = {}
default_sidebars = ['localtoc.html', 'relations.html', default_sidebars = ['localtoc.html', 'relations.html',
'sourcelink.html', 'searchbox.html'] 'sourcelink.html', 'searchbox.html']
@@ -345,6 +346,7 @@ class StandaloneHTMLBuilder(Builder):
script_files = self.script_files, script_files = self.script_files,
language = self.config.language, language = self.config.language,
css_files = self.css_files, css_files = self.css_files,
css_props = self.css_props,
sphinx_version = __display_version__, sphinx_version = __display_version__,
style = stylename, style = stylename,
rellinks = rellinks, rellinks = rellinks,

View File

@@ -102,26 +102,24 @@
{%- macro css() %} {%- macro css() %}
<link rel="stylesheet" href="{{ pathto('_static/' + style, 1) }}" type="text/css" /> <link rel="stylesheet" href="{{ pathto('_static/' + style, 1) }}" type="text/css" />
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', 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 %} {%- for cssfile in css_files %}
{%- if cssfile.filename is defined %} {%- if css_props[cssfile] is not defined %}
{%- set filename = cssfile.filename %} {%- set alt = False %}
{%- set csstitle = undefined %}
{%- else %} {%- else %}
{%- set filename = cssfile %} {%- set csstitle = css_props[cssfile].title %}
{%- endif %} {%- set alt = (csstitle is defined) %}
{%- if cssfile.alternate is defined %} {%- if css_props[cssfile].alternate is defined %}
{%- set alt = cssfile.alternate %} {%- set alt = css_props[cssfile].alternate %}
{%- endif %}
{%- if cssfile.title is defined and cssfile.title is string %}
{%- if cssfile.alternate is not defined %}
{%- set alt = True %}
{%- endif %} {%- endif %}
<link rel="{% if alt %}alternate {% endif %}stylesheet" href="{{ pathto(filename, 1) }}" type="text/css" title="{{ cssfile.title }}" /> {%- endif %}
{%- if alt %}
{%- set rel = "alternate stylesheet" %}
{%- else %} {%- else %}
{%- if cssfile.alternate is not defined %} {%- set rel = "stylesheet" %}
{%- set alt = False %}
{%- endif %}
<link rel="{% if alt %}alternate {% endif %}stylesheet" href="{{ pathto(filename, 1) }}" type="text/css" />
{%- endif %} {%- endif %}
<link rel="{{ rel }}" href="{{ pathto(cssfile, 1) }}" type="text/css"{% if csstitle is defined %} title="{{ csstitle }}"{% endif %} />
{%- endfor %} {%- endfor %}
{%- endmacro %} {%- endmacro %}

View File

@@ -1,7 +1,7 @@
{% extends "!layout.html" %} {% extends "!layout.html" %}
{% set css_files = css_files + ["_static/more_persistent.css", {% 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"] %}
{"filename": "_static/more_persistent2.css"}, {% set _dummy = css_props.update(
{"filename": "_static/more_default.css", "title": "Default", "alternate": False}, {"_static/more_default.css": {"title": "Default", "alternate": False},
{"filename": "_static/more_alternate1.css", "title": "Alternate"}, "_static/more_alternate1.css": {"title": "Alternate"},
{"filename": "_static/more_alternate2.css", "alternate": True}] %} "_static/more_alternate2.css": {"alternate": True} }
) %}