Skip to content

Commit 7f530d8

Browse files
committed
Validate mkdocs.yml config on values that we manipulate
Make sure that the values we will manipulate from the mkdocs.yml file the correct type we need them to be. If we found a problem with them, we report a proper message to user to debug it by themselves.
1 parent 9563f0a commit 7f530d8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

readthedocs/doc_builder/backends/mkdocs.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def load_yaml_config(self):
8484
"""
8585
Load a YAML config.
8686
87-
Raise BuildEnvironmentError if failed due to syntax errors.
87+
:raises: ``MkDocsYAMLParseError`` if failed due to syntax errors.
8888
"""
8989
try:
9090
return yaml.safe_load(
@@ -105,20 +105,40 @@ def load_yaml_config(self):
105105
)
106106

107107
def append_conf(self, **__):
108-
"""Set mkdocs config values."""
108+
"""
109+
Set mkdocs config values.
110+
111+
:raises: ``MkDocsYAMLParseError`` if failed due to known type errors
112+
(i.e. expecting a list and a string is found).
113+
"""
109114
if not self.yaml_file:
110115
self.yaml_file = os.path.join(self.root_path, 'mkdocs.yml')
111116

112117
user_config = self.load_yaml_config()
113118

114119
# Handle custom docs dirs
115120
user_docs_dir = user_config.get('docs_dir')
121+
if not isinstance(user_docs_dir, str):
122+
raise MkDocsYAMLParseError(
123+
MkDocsYAMLParseError.INVALID_DOCS_DIR_CONFIG,
124+
)
125+
116126
docs_dir = self.docs_dir(docs_dir=user_docs_dir)
117127
self.create_index(extension='md')
118128
user_config['docs_dir'] = docs_dir
119129

120130
# Set mkdocs config values
121131
static_url = get_absolute_static_url()
132+
133+
for config in ('extra_css', 'extra_javascript'):
134+
user_value = user_config.get(config)
135+
if not isinstance(user_value, (type(None), list)):
136+
raise MkDocsYAMLParseError(
137+
MkDocsYAMLParseError.INVALID_EXTRA_CONFIG.format(
138+
config=config,
139+
),
140+
)
141+
122142
user_config.setdefault('extra_javascript', []).extend([
123143
'readthedocs-data.js',
124144
'%score/js/readthedocs-doc-embed.js' % static_url,

readthedocs/doc_builder/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,13 @@ class MkDocsYAMLParseError(BuildEnvironmentError):
5959
GENERIC_WITH_PARSE_EXCEPTION = ugettext_noop(
6060
'Problem parsing MkDocs YAML configuration. {exception}',
6161
)
62+
63+
INVALID_DOCS_DIR_CONFIG = ugettext_noop(
64+
'The "docs_dir" config from your MkDocs YAML config file has to be a '
65+
'string with relative or absolute path.',
66+
)
67+
68+
INVALID_EXTRA_CONFIG = ugettext_noop(
69+
'The "{config}" config from your MkDocs YAML config file has to be a '
70+
'a list of relative paths.',
71+
)

0 commit comments

Comments
 (0)