Skip to content

Commit 1ed10e8

Browse files
authored
MkDocs: allow None on extra_css/extra_javascript (#8976)
Currently we are failing the build if we find None values, but MkDocs allows them.
1 parent 7cf66f6 commit 1ed10e8

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

readthedocs/doc_builder/backends/mkdocs.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
.. _MkDocs: http://www.mkdocs.org/
55
"""
66

7-
import structlog
87
import os
98

9+
import structlog
1010
import yaml
1111
from django.conf import settings
1212
from django.template import loader as template_loader
@@ -149,35 +149,35 @@ def append_conf(self):
149149

150150
# Set mkdocs config values
151151
static_url = get_absolute_static_url()
152+
extra_assets = {
153+
'extra_javascript': [
154+
'readthedocs-data.js',
155+
f'{static_url}core/js/readthedocs-doc-embed.js',
156+
f'{static_url}javascript/readthedocs-analytics.js',
157+
],
158+
'extra_css': [
159+
f'{static_url}css/badge_only.css',
160+
f'{static_url}css/readthedocs-doc-embed.css',
161+
],
162+
}
152163

153-
for config in ('extra_css', 'extra_javascript'):
154-
user_value = user_config.get(config, [])
155-
if not isinstance(user_value, list):
164+
for config, extras in extra_assets.items():
165+
value = user_config.get(config, [])
166+
if value is None:
167+
value = []
168+
if not isinstance(value, list):
156169
raise MkDocsYAMLParseError(
157170
MkDocsYAMLParseError.INVALID_EXTRA_CONFIG.format(
158171
config=config,
159172
),
160173
)
161-
162-
extra_javascript_list = [
163-
'readthedocs-data.js',
164-
'%score/js/readthedocs-doc-embed.js' % static_url,
165-
'%sjavascript/readthedocs-analytics.js' % static_url,
166-
]
167-
extra_css_list = [
168-
'%scss/badge_only.css' % static_url,
169-
'%scss/readthedocs-doc-embed.css' % static_url,
170-
]
171-
172-
# Only add static file if the files are not already in the list
173-
user_config.setdefault('extra_javascript', []).extend(
174-
[js for js in extra_javascript_list if js not in user_config.get(
175-
'extra_javascript')]
176-
)
177-
user_config.setdefault('extra_css', []).extend(
178-
[css for css in extra_css_list if css not in user_config.get(
179-
'extra_css')]
180-
)
174+
# Add the static file only if isn't already in the list.
175+
value.extend([
176+
extra
177+
for extra in extras
178+
if extra not in value
179+
])
180+
user_config[config] = value
181181

182182
# The docs path is relative to the location
183183
# of the mkdocs configuration file.

readthedocs/rtd_tests/tests/test_doc_builder.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def test_append_conf_existing_yaml_on_root_with_invalid_setting(self, checkout_p
566566
yaml_contents = [
567567
{'docs_dir': ['docs']},
568568
{'extra_css': 'a string here'},
569-
{'extra_javascript': None},
569+
{'extra_javascript': ''},
570570
]
571571
for content in yaml_contents:
572572
yaml.safe_dump(
@@ -576,6 +576,50 @@ def test_append_conf_existing_yaml_on_root_with_invalid_setting(self, checkout_p
576576
with self.assertRaises(MkDocsYAMLParseError):
577577
self.searchbuilder.append_conf()
578578

579+
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
580+
@patch('readthedocs.projects.models.Project.checkout_path')
581+
def test_append_conf_and_none_values(self, checkout_path, run):
582+
tmpdir = tempfile.mkdtemp()
583+
os.mkdir(os.path.join(tmpdir, 'docs'))
584+
yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
585+
checkout_path.return_value = tmpdir
586+
587+
python_env = Virtualenv(
588+
version=self.version,
589+
build_env=self.build_env,
590+
config=None,
591+
)
592+
builder = MkdocsHTML(
593+
build_env=self.build_env,
594+
python_env=python_env,
595+
)
596+
597+
yaml.safe_dump(
598+
{
599+
'extra_css': None,
600+
'extra_javascript': None,
601+
},
602+
open(yaml_file, 'w'),
603+
)
604+
builder.append_conf()
605+
config = yaml_load_safely(open(yaml_file))
606+
607+
self.assertEqual(
608+
config['extra_css'],
609+
[
610+
'http://readthedocs.org/static/css/badge_only.css',
611+
'http://readthedocs.org/static/css/readthedocs-doc-embed.css',
612+
],
613+
)
614+
self.assertEqual(
615+
config['extra_javascript'],
616+
[
617+
'readthedocs-data.js',
618+
'http://readthedocs.org/static/core/js/readthedocs-doc-embed.js',
619+
'http://readthedocs.org/static/javascript/readthedocs-analytics.js',
620+
],
621+
)
622+
579623
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
580624
@patch('readthedocs.projects.models.Project.checkout_path')
581625
def test_dont_override_theme(self, checkout_path, run):

0 commit comments

Comments
 (0)