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"] %}
.. versionchanged:: 1.4
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::
.. data:: css_props
{% 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
given, the default is ``alternate=True``. If ``alternate`` is ``True``, it
will be translated to ``rel="alternate stylesheet"``, otherwise it will be
``rel="stylesheet"``.
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

@@ -745,16 +745,17 @@ class Sphinx(object):
def add_stylesheet(self, filename, alternate=None, title=None):
self.debug('[app] adding stylesheet: %r', filename)
from sphinx.builders.html import StandaloneHTMLBuilder
item = {}
props = {}
if alternate is not None:
item['alternate'] = bool(alternate)
props['alternate'] = bool(alternate)
if title is not None:
item['title'] = title
props['title'] = title
if '://' in filename:
item['filename'] = filename
fname = filename
else:
item['filename'] = posixpath.join('_static', filename)
StandaloneHTMLBuilder.css_files.append(item)
fname = posixpath.join('_static', filename)
StandaloneHTMLBuilder.css_files.append(fname)
StandaloneHTMLBuilder.css_props[fname] = props
def add_latex_package(self, packagename, options=None):
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.
script_files = ['_static/jquery.js', '_static/underscore.js',
'_static/doctools.js']
# Dito for this one.
# Ditto for these ones (Sphinx.add_stylesheet).
css_files = []
css_props = {}
default_sidebars = ['localtoc.html', 'relations.html',
'sourcelink.html', 'searchbox.html']
@@ -345,6 +346,7 @@ 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

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

View File

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