17
17
18
18
log = logging .getLogger (__name__ )
19
19
20
- TEMPLATE_DIR = '%s/readthedocs/templates/mkdocs/readthedocs' % settings .SITE_ROOT
21
- OVERRIDE_TEMPLATE_DIR = '%s/readthedocs/templates/mkdocs/overrides' % settings .SITE_ROOT
22
-
23
20
24
21
def get_absolute_media_url ():
25
22
"""
@@ -42,6 +39,16 @@ class BaseMkdocs(BaseBuilder):
42
39
43
40
use_theme = True
44
41
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
+
45
52
def __init__ (self , * args , ** kwargs ):
46
53
super (BaseMkdocs , self ).__init__ (* args , ** kwargs )
47
54
self .old_artifact_path = os .path .join (
@@ -96,10 +103,6 @@ def append_conf(self, **__):
96
103
'%scss/readthedocs-doc-embed.css' % media_url ,
97
104
])
98
105
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
-
103
106
docs_path = os .path .join (self .root_path , docs_dir )
104
107
105
108
# RTD javascript writing
@@ -111,20 +114,18 @@ def append_conf(self, **__):
111
114
# This supports using RTD's privacy improvements around analytics
112
115
user_config ['google_analytics' ] = None
113
116
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
115
122
yaml .safe_dump (
116
123
user_config ,
117
124
open (os .path .join (self .root_path , 'mkdocs.yml' ), 'w' )
118
125
)
119
126
120
127
def generate_rtd_data (self , docs_dir , mkdocs_config ):
121
128
"""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
-
128
129
# Use the analytics code from mkdocs.yml if it isn't set already by Read the Docs
129
130
analytics_code = self .version .project .analytics_code
130
131
if not analytics_code and mkdocs_config .get ('google_analytics' ):
@@ -138,7 +139,7 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
138
139
'language' : self .version .project .language ,
139
140
'programming_language' : self .version .project .programming_language ,
140
141
'page' : None ,
141
- 'theme' : theme_name ,
142
+ 'theme' : self . get_theme_name ( mkdocs_config ) ,
142
143
'builder' : "mkdocs" ,
143
144
'docroot' : docs_dir ,
144
145
'source_suffix' : ".md" ,
@@ -176,6 +177,51 @@ def build(self):
176
177
)
177
178
return cmd_ret .successful
178
179
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
+
179
225
180
226
class MkdocsHTML (BaseMkdocs ):
181
227
type = 'mkdocs'
@@ -188,15 +234,3 @@ class MkdocsJSON(BaseMkdocs):
188
234
builder = 'json'
189
235
build_dir = '_build/json'
190
236
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 ()
0 commit comments