From ea219a04edb44eb91ae4b6a5b7275a444595d8f5 Mon Sep 17 00:00:00 2001 From: Jellby Date: Sun, 6 Mar 2016 13:49:57 +0100 Subject: [PATCH] Move CSS properties to a different variable --- doc/templating.rst | 21 ++++++++------ sphinx/application.py | 13 +++++---- sphinx/builders/html.py | 4 ++- sphinx/themes/basic/layout.html | 28 +++++++++---------- .../test-stylesheets/_templates/layout.html | 12 ++++---- 5 files changed, 41 insertions(+), 37 deletions(-) diff --git a/doc/templating.rst b/doc/templating.rst index 0ab005a8e..26930fde2 100644 --- a/doc/templating.rst +++ b/doc/templating.rst @@ -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 ~~~~~~~~~~~~~~~~ diff --git a/sphinx/application.py b/sphinx/application.py index 4465113d7..0c8450f7a 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -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) diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 367e28b8d..76e4106ec 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -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, diff --git a/sphinx/themes/basic/layout.html b/sphinx/themes/basic/layout.html index ca856649b..a489a7502 100644 --- a/sphinx/themes/basic/layout.html +++ b/sphinx/themes/basic/layout.html @@ -102,26 +102,24 @@ {%- macro 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 %} - + {%- endif %} + {%- if alt %} + {%- set rel = "alternate stylesheet" %} {%- else %} - {%- if cssfile.alternate is not defined %} - {%- set alt = False %} - {%- endif %} - + {%- set rel = "stylesheet" %} {%- endif %} + {%- endfor %} {%- endmacro %} diff --git a/tests/roots/test-stylesheets/_templates/layout.html b/tests/roots/test-stylesheets/_templates/layout.html index 3e561619b..97ade552e 100644 --- a/tests/roots/test-stylesheets/_templates/layout.html +++ b/tests/roots/test-stylesheets/_templates/layout.html @@ -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} } +) %}