Skip to content

Commit 538413c

Browse files
committed
Update mkdocs to 0.17.3
1 parent 2bc8084 commit 538413c

File tree

3 files changed

+63
-31
lines changed

3 files changed

+63
-31
lines changed

readthedocs/doc_builder/backends/mkdocs.py

+61-27
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
log = logging.getLogger(__name__)
1919

20-
TEMPLATE_DIR = '%s/readthedocs/templates/mkdocs/readthedocs' % settings.SITE_ROOT
21-
OVERRIDE_TEMPLATE_DIR = '%s/readthedocs/templates/mkdocs/overrides' % settings.SITE_ROOT
22-
2320

2421
def get_absolute_media_url():
2522
"""
@@ -42,6 +39,16 @@ class BaseMkdocs(BaseBuilder):
4239

4340
use_theme = True
4441

42+
# The default theme for mkdocs (outside of RTD) is the 'mkdocs' theme
43+
# For RTD, our default is the 'readthedocs' theme
44+
READTHEDOCS_THEME_NAME = 'readthedocs'
45+
46+
# Overrides for the 'readthedocs' theme that include
47+
# search utilities and version selector
48+
READTHEDOCS_TEMPLATE_OVERRIDE_DIR = (
49+
'%s/readthedocs/templates/mkdocs/readthedocs' % settings.SITE_ROOT
50+
)
51+
4552
def __init__(self, *args, **kwargs):
4653
super(BaseMkdocs, self).__init__(*args, **kwargs)
4754
self.old_artifact_path = os.path.join(
@@ -96,10 +103,6 @@ def append_conf(self, **__):
96103
'%scss/readthedocs-doc-embed.css' % media_url,
97104
])
98105

99-
# Set our custom theme dir for mkdocs
100-
if 'theme_dir' not in user_config and self.use_theme:
101-
user_config['theme_dir'] = TEMPLATE_DIR
102-
103106
docs_path = os.path.join(self.root_path, docs_dir)
104107

105108
# RTD javascript writing
@@ -111,20 +114,18 @@ def append_conf(self, **__):
111114
# This supports using RTD's privacy improvements around analytics
112115
user_config['google_analytics'] = None
113116

114-
# Write the mkdocs configuration
117+
# If using the readthedocs theme, apply the readthedocs.org overrides
118+
# These use a global readthedocs search and customize the version selector
119+
self.apply_theme_override(user_config)
120+
121+
# Write the modified mkdocs configuration
115122
yaml.safe_dump(
116123
user_config,
117124
open(os.path.join(self.root_path, 'mkdocs.yml'), 'w')
118125
)
119126

120127
def generate_rtd_data(self, docs_dir, mkdocs_config):
121128
"""Generate template properties and render readthedocs-data.js."""
122-
# Get the theme name
123-
theme_name = 'readthedocs'
124-
theme_dir = mkdocs_config.get('theme_dir')
125-
if theme_dir:
126-
theme_name = theme_dir.rstrip('/').split('/')[-1]
127-
128129
# Use the analytics code from mkdocs.yml if it isn't set already by Read the Docs
129130
analytics_code = self.version.project.analytics_code
130131
if not analytics_code and mkdocs_config.get('google_analytics'):
@@ -138,7 +139,7 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
138139
'language': self.version.project.language,
139140
'programming_language': self.version.project.programming_language,
140141
'page': None,
141-
'theme': theme_name,
142+
'theme': self.get_theme_name(mkdocs_config),
142143
'builder': "mkdocs",
143144
'docroot': docs_dir,
144145
'source_suffix': ".md",
@@ -176,6 +177,51 @@ def build(self):
176177
)
177178
return cmd_ret.successful
178179

180+
def get_theme_name(self, mkdocs_config):
181+
"""
182+
Get the theme configuration in the mkdocs_config
183+
184+
In v0.17.0, the theme configuration switched
185+
from two separate configs (both optional) to a nested directive.
186+
187+
:see: http://www.mkdocs.org/about/release-notes/#theme-customization-1164
188+
:returns: the name of the theme RTD will use
189+
"""
190+
theme_setting = mkdocs_config.get('theme')
191+
if isinstance(theme_setting, dict):
192+
# Full nested theme config (the new configuration)
193+
return theme_setting.get('name') or self.READTHEDOCS_THEME_NAME
194+
195+
if theme_setting:
196+
# A string which is the name of the theme
197+
return theme_setting
198+
199+
theme_dir = mkdocs_config.get('theme_dir')
200+
if theme_dir:
201+
# Use the name of the directory in this project's custom theme directory
202+
return theme_dir.rstrip('/').split('/')[-1]
203+
204+
return self.READTHEDOCS_THEME_NAME
205+
206+
def apply_theme_override(self, mkdocs_config):
207+
"""
208+
Apply theme overrides for the RTD theme (modifies the ``mkdocs_config`` parameter)
209+
210+
In v0.17.0, the theme configuration switched
211+
from two separate configs (both optional) to a nested directive.
212+
How to override the theme depends on whether the new or old configuration
213+
is used.
214+
215+
:see: http://www.mkdocs.org/about/release-notes/#theme-customization-1164
216+
"""
217+
if self.get_theme_name(mkdocs_config) == self.READTHEDOCS_THEME_NAME:
218+
# Overriding the theme is only necessary if the 'readthedocs' theme is used
219+
theme_setting = mkdocs_config.get('theme')
220+
if isinstance(theme_setting, dict):
221+
theme_setting['custom_dir'] = self.READTHEDOCS_TEMPLATE_OVERRIDE_DIR
222+
else:
223+
mkdocs_config['theme_dir'] = self.READTHEDOCS_TEMPLATE_OVERRIDE_DIR
224+
179225

180226
class MkdocsHTML(BaseMkdocs):
181227
type = 'mkdocs'
@@ -188,15 +234,3 @@ class MkdocsJSON(BaseMkdocs):
188234
builder = 'json'
189235
build_dir = '_build/json'
190236
use_theme = False
191-
192-
def build(self):
193-
user_config = yaml.safe_load(
194-
open(os.path.join(self.root_path, 'mkdocs.yml'), 'r')
195-
)
196-
if user_config['theme_dir'] == TEMPLATE_DIR:
197-
del user_config['theme_dir']
198-
yaml.safe_dump(
199-
user_config,
200-
open(os.path.join(self.root_path, 'mkdocs.yml'), 'w')
201-
)
202-
return super(MkdocsJSON, self).build()

readthedocs/doc_builder/python_environments.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def install_core_requirements(self):
235235
]
236236

237237
if self.project.documentation_type == 'mkdocs':
238-
requirements.append('mkdocs==0.15.0')
238+
requirements.append('mkdocs==0.17.3')
239239
else:
240240
# We will assume semver here and only automate up to the next
241241
# backward incompatible release: 2.x

requirements/pip.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ sphinx_rtd_theme==0.2.5b1
1111

1212
Pygments==2.2.0
1313

14-
# latest compatible version with our code
15-
# https://github.com/rtfd/readthedocs.org/pull/2916#discussion_r172991757
16-
mkdocs==0.15.0
14+
mkdocs==0.17.3
1715

1816
django==1.9.13
1917
six==1.11.0

0 commit comments

Comments
 (0)