Skip to content

Commit

Permalink
Handling overridden css_files/html_static_path.
Browse files Browse the repository at this point in the history
Users setting those two variables in their conf.py broke SCVersioning.
Handling this by injecting SCVersioning objects during html-page-context
Sphinx API hook.

Fixes #23
  • Loading branch information
Robpol86 committed Sep 3, 2016
1 parent 9c427e5 commit 9b8b280
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Changelog

This project adheres to `Semantic Versioning <http://semver.org/>`_.

Unreleased
----------

Fixed
* banner.css being overridden by conf.py: https://github.com/Robpol86/sphinxcontrib-versioning/issues/23

2.1.3 - 2016-08-24
------------------

Expand Down
12 changes: 9 additions & 3 deletions sphinxcontrib/versioning/sphinx_.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Interface with Sphinx."""

from __future__ import print_function

import logging
import multiprocessing
import os
Expand All @@ -18,6 +16,7 @@
from sphinxcontrib.versioning.versions import Versions

SC_VERSIONING_VERSIONS = list() # Updated after forking.
STATIC_DIR = os.path.join(os.path.dirname(__file__), '_static')


class EventHandlers(object):
Expand Down Expand Up @@ -116,6 +115,13 @@ def html_page_context(cls, app, pagename, templatename, context, doctree):
if cls.SHOW_BANNER and 'body' in context:
parsed = app.builder.templates.render('banner.html', context)
context['body'] = parsed + context['body']
# Handle overridden css_files.
css_files = context.setdefault('css_files', list())
if '_static/banner.css' not in css_files:
css_files.append('_static/banner.css')
# Handle overridden html_static_path.
if STATIC_DIR not in app.config.html_static_path:
app.config.html_static_path.append(STATIC_DIR)


def setup(app):
Expand All @@ -130,7 +136,7 @@ def setup(app):
app.add_config_value('sphinxcontrib_versioning_versions', SC_VERSIONING_VERSIONS, 'html')

# Needed for banner.
app.config.html_static_path.append(os.path.join(os.path.dirname(__file__), '_static'))
app.config.html_static_path.append(STATIC_DIR)
app.add_stylesheet('banner.css')

# Tell Sphinx which config values can be set by the user.
Expand Down
31 changes: 31 additions & 0 deletions tests/test__main__/test_main_build_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,37 @@ def test_banner(banner, local_docs, run, disable_banner):
'an old version of Python. The main version is stable')


def test_banner_css_override(banner, local_docs, run):
"""Test the banner CSS being present even if user overrides html_context['css_files'].
:param banner: conftest fixture.
:param local_docs: conftest fixture.
:param run: conftest fixture.
"""
local_docs.join('conf.py').write("html_context = {'css_files': ['_static/theme_overrides.css']}\n", mode='a')
local_docs.join('conf.py').write("html_static_path = ['_static']\n", mode='a')
run(local_docs, ['git', 'commit', '-am', 'Setting override.'])
run(local_docs, ['git', 'checkout', '-b', 'other', 'master'])
run(local_docs, ['git', 'push', 'origin', 'master', 'other'])

# Run.
destination = local_docs.ensure_dir('..', 'destination')
output = run(local_docs, ['sphinx-versioning', 'build', '.', str(destination), '--show-banner'])
assert 'Traceback' not in output
assert 'Disabling banner.' not in output
assert 'Banner main ref is: master' in output

# Check banner.
banner(destination.join('master', 'contents.html'), None) # No banner in main ref.
banner(destination.join('other', 'contents.html'), '../master/contents.html',
'the development version of Python. The main version is master')

# Check CSS.
contents = destination.join('other', 'contents.html').read()
assert 'rel="stylesheet" href="_static/banner.css"' in contents
assert destination.join('other', '_static', 'banner.css').check(file=True)


def test_error_bad_path(tmpdir, run):
"""Test handling of bad paths.
Expand Down

0 comments on commit 9b8b280

Please sign in to comment.