Skip to content

Commit 4450ec6

Browse files
authored
Merge pull request #4556 from rtfd/davidfischer/mkdocs-upgrade-again
Updates and simplification for mkdocs
2 parents f1a3fc8 + 4d82d8c commit 4450ec6

File tree

9 files changed

+47
-133
lines changed

9 files changed

+47
-133
lines changed

media/css/readthedocs-doc-embed.css

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
display: block;
88

99
bottom: 50px;
10+
11+
/* Workaround for mkdocs which set a specific height for this element */
12+
height: auto;
1013
}
1114

1215
.rst-other-versions {

readthedocs/core/static-src/core/js/doc-embed/footer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ function injectFooter(data) {
77

88
// If the theme looks like ours, update the existing badge
99
// otherwise throw a a full one into the page.
10-
if (config.is_rtd_like_theme()) {
10+
// Do not inject for mkdocs even for the RTD theme
11+
if (config.is_sphinx_builder() && config.is_rtd_like_theme()) {
1112
$("div.rst-other-versions").html(data['html']);
1213
} else {
1314
$("body").append(data['html']);

readthedocs/core/static-src/core/js/doc-embed/rtd-data.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var constants = require('./constants');
88

99
var configMethods = {
1010
is_rtd_like_theme: function () {
11+
// Returns true for the Read the Docs theme on both sphinx and mkdocs
1112
if ($('div.rst-other-versions').length === 1) {
1213
// Crappy heuristic, but people change the theme name
1314
// So we have to do some duck typing.
@@ -30,7 +31,7 @@ var configMethods = {
3031
},
3132

3233
is_mkdocs_builder: function () {
33-
return (!('builder' in this) || this.builder === 'mkdocs');
34+
return ('builder' in this && this.builder === 'mkdocs');
3435
},
3536

3637
get_theme_name: function () {

readthedocs/core/static-src/core/js/doc-embed/sphinx.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function init() {
2929
/// Inject the Read the Docs Sphinx theme code
3030
/// This is necessary on older versions of the RTD theme (<0.4.0)
3131
/// and on themes other then the RTD theme (used for the version menu)
32-
if ((rtd.builder === undefined || rtd.builder === 'sphinx') && window.SphinxRtdTheme === undefined) {
32+
if (window.SphinxRtdTheme === undefined) {
3333
sphinx_theme = require('sphinx-rtd-theme'); // eslint-disable-line global-require
3434

3535
var theme = sphinx_theme.ThemeNav;

readthedocs/doc_builder/backends/mkdocs.py

+4-41
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,8 @@ class BaseMkdocs(BaseBuilder):
3939

4040
"""Mkdocs builder."""
4141

42-
use_theme = True
43-
44-
# The default theme for mkdocs (outside of RTD) is the 'mkdocs' theme
45-
# For RTD, our default is the 'readthedocs' theme
46-
READTHEDOCS_THEME_NAME = 'readthedocs'
47-
48-
# Overrides for the 'readthedocs' theme that include
49-
# search utilities and version selector
50-
READTHEDOCS_TEMPLATE_OVERRIDE_DIR = (
51-
'%s/readthedocs/templates/mkdocs/readthedocs' % settings.SITE_ROOT
52-
)
42+
# The default theme for mkdocs is the 'mkdocs' theme
43+
DEFAULT_THEME_NAME = 'mkdocs'
5344

5445
def __init__(self, *args, **kwargs):
5546
super(BaseMkdocs, self).__init__(*args, **kwargs)
@@ -133,11 +124,6 @@ def append_conf(self, **__):
133124
# This supports using RTD's privacy improvements around analytics
134125
user_config['google_analytics'] = None
135126

136-
# If using the readthedocs theme, apply the readthedocs.org overrides
137-
# These use a global readthedocs search
138-
# and customize the version selector.
139-
self.apply_theme_override(user_config)
140-
141127
# Write the modified mkdocs configuration
142128
yaml.safe_dump(
143129
user_config,
@@ -198,8 +184,6 @@ def build(self):
198184
'--site-dir', self.build_dir,
199185
'--config-file', self.yaml_file,
200186
]
201-
if self.use_theme:
202-
build_command.extend(['--theme', 'readthedocs'])
203187
cmd_ret = self.run(
204188
*build_command,
205189
cwd=checkout_path,
@@ -220,7 +204,7 @@ def get_theme_name(self, mkdocs_config):
220204
theme_setting = mkdocs_config.get('theme')
221205
if isinstance(theme_setting, dict):
222206
# Full nested theme config (the new configuration)
223-
return theme_setting.get('name') or self.READTHEDOCS_THEME_NAME
207+
return theme_setting.get('name') or self.DEFAULT_THEME_NAME
224208

225209
if theme_setting:
226210
# A string which is the name of the theme
@@ -231,27 +215,7 @@ def get_theme_name(self, mkdocs_config):
231215
# Use the name of the directory in this project's custom theme directory
232216
return theme_dir.rstrip('/').split('/')[-1]
233217

234-
return self.READTHEDOCS_THEME_NAME
235-
236-
def apply_theme_override(self, mkdocs_config):
237-
"""
238-
Apply theme overrides for the RTD theme (modifies the ``mkdocs_config`` parameter)
239-
240-
In v0.17.0, the theme configuration switched
241-
from two separate configs (both optional) to a nested directive.
242-
How to override the theme depends on whether the new or old configuration
243-
is used.
244-
245-
:see: http://www.mkdocs.org/about/release-notes/#theme-customization-1164
246-
"""
247-
if self.get_theme_name(mkdocs_config) == self.READTHEDOCS_THEME_NAME:
248-
# Overriding the theme is only necessary
249-
# if the 'readthedocs' theme is used.
250-
theme_setting = mkdocs_config.get('theme')
251-
if isinstance(theme_setting, dict):
252-
theme_setting['custom_dir'] = self.READTHEDOCS_TEMPLATE_OVERRIDE_DIR
253-
else:
254-
mkdocs_config['theme_dir'] = self.READTHEDOCS_TEMPLATE_OVERRIDE_DIR
218+
return self.DEFAULT_THEME_NAME
255219

256220

257221
class MkdocsHTML(BaseMkdocs):
@@ -264,4 +228,3 @@ class MkdocsJSON(BaseMkdocs):
264228
type = 'mkdocs_json'
265229
builder = 'json'
266230
build_dir = '_build/json'
267-
use_theme = False

readthedocs/doc_builder/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, build_env, python_env, force=False):
4646
self.python_env = python_env
4747
self.version = build_env.version
4848
self.project = build_env.project
49-
self.config = python_env.config
49+
self.config = python_env.config if python_env else None
5050
self._force = force
5151
self.target = self.project.artifact_path(
5252
version=self.version.slug, type_=self.type)

readthedocs/rtd_tests/tests/test_doc_builder.py

+34-75
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,40 @@ def setUp(self):
150150
self.build_env.project = self.project
151151
self.build_env.version = self.version
152152

153+
def test_get_theme_name(self):
154+
builder = MkdocsHTML(
155+
build_env=self.build_env,
156+
python_env=None
157+
)
158+
159+
# The default theme is mkdocs but in mkdocs>=1.0, theme is required
160+
self.assertEqual(builder.get_theme_name({}), 'mkdocs')
161+
162+
# mkdocs<0.17 syntax
163+
config = {
164+
'theme': 'readthedocs',
165+
}
166+
self.assertEqual(builder.get_theme_name(config), 'readthedocs')
167+
168+
# mkdocs>=0.17 syntax
169+
config = {
170+
'theme': {
171+
'name': 'test_theme',
172+
},
173+
}
174+
self.assertEqual(builder.get_theme_name(config), 'test_theme')
175+
176+
# No theme but just a directory
177+
config = {
178+
'theme_dir': '/path/to/mydir',
179+
}
180+
self.assertEqual(builder.get_theme_name(config), 'mydir')
181+
config = {
182+
'theme_dir': '/path/to/mydir/',
183+
}
184+
self.assertEqual(builder.get_theme_name(config), 'mydir')
185+
186+
153187
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
154188
@patch('readthedocs.projects.models.Project.checkout_path')
155189
def test_append_conf_create_yaml(self, checkout_path, run):
@@ -258,81 +292,6 @@ def test_append_conf_existing_yaml_on_root(self, checkout_path, run):
258292
'mkdocs'
259293
)
260294

261-
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
262-
@patch('readthedocs.projects.models.Project.checkout_path')
263-
def test_override_theme_new_style(self, checkout_path, run):
264-
tmpdir = tempfile.mkdtemp()
265-
os.mkdir(os.path.join(tmpdir, 'docs'))
266-
yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
267-
yaml.safe_dump(
268-
{
269-
'theme': {
270-
'name': 'readthedocs',
271-
},
272-
'site_name': 'mkdocs',
273-
'docs_dir': 'docs',
274-
},
275-
open(yaml_file, 'w')
276-
)
277-
checkout_path.return_value = tmpdir
278-
279-
python_env = Virtualenv(
280-
version=self.version,
281-
build_env=self.build_env,
282-
config=None,
283-
)
284-
self.searchbuilder = MkdocsHTML(
285-
build_env=self.build_env,
286-
python_env=python_env,
287-
)
288-
self.searchbuilder.append_conf()
289-
290-
run.assert_called_with('cat', 'mkdocs.yml', cwd=mock.ANY)
291-
292-
config = yaml.safe_load(open(yaml_file))
293-
self.assertEqual(
294-
config['theme'],
295-
{
296-
'name': 'readthedocs',
297-
'custom_dir': BaseMkdocs.READTHEDOCS_TEMPLATE_OVERRIDE_DIR
298-
}
299-
)
300-
301-
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
302-
@patch('readthedocs.projects.models.Project.checkout_path')
303-
def test_override_theme_old_style(self, checkout_path, run):
304-
tmpdir = tempfile.mkdtemp()
305-
os.mkdir(os.path.join(tmpdir, 'docs'))
306-
yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
307-
yaml.safe_dump(
308-
{
309-
'theme': 'readthedocs',
310-
'site_name': 'mkdocs',
311-
'docs_dir': 'docs',
312-
},
313-
open(yaml_file, 'w')
314-
)
315-
checkout_path.return_value = tmpdir
316-
317-
python_env = Virtualenv(
318-
version=self.version,
319-
build_env=self.build_env,
320-
config=None,
321-
)
322-
self.searchbuilder = MkdocsHTML(
323-
build_env=self.build_env,
324-
python_env=python_env,
325-
)
326-
self.searchbuilder.append_conf()
327-
328-
run.assert_called_with('cat', 'mkdocs.yml', cwd=mock.ANY)
329-
330-
config = yaml.safe_load(open(yaml_file))
331-
self.assertEqual(
332-
config['theme_dir'],
333-
BaseMkdocs.READTHEDOCS_TEMPLATE_OVERRIDE_DIR
334-
)
335-
336295
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
337296
@patch('readthedocs.projects.models.Project.checkout_path')
338297
def test_dont_override_theme(self, checkout_path, run):

readthedocs/templates/mkdocs/readthedocs/searchbox.html

-5
This file was deleted.

readthedocs/templates/mkdocs/readthedocs/versions.html

-8
This file was deleted.

0 commit comments

Comments
 (0)