Skip to content

Commit c2b4c7e

Browse files
committed
Add a version selector dropdown to the main menu
This adds a version selection dropdown to the menu. It is used to redirect on change to the version URL. The ``versions`` theme option is added and is used to add a mapping of version slugs to URLs. Currently, this is going to be used in pydoc.io. The implementation will change the javascript slightly, to accept json data instead of a blob of HTML. This will currently be disabled on Read the Docs, as we are rethinking what the version selection menu looks like.
1 parent b3225c5 commit c2b4c7e

File tree

12 files changed

+6413
-23
lines changed

12 files changed

+6413
-23
lines changed

README.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Read the Docs Sphinx Theme
1414
**************************
1515

16-
.. contents::
16+
.. contents::
1717

1818
View a working demo_ over on readthedocs.org_.
1919

@@ -82,23 +82,43 @@ file of this repository, and can be defined in your project's ``conf.py`` via
8282
'collapse_navigation': False,
8383
'display_version': False,
8484
'navigation_depth': 3,
85+
'versions': {
86+
'1.0': 'https://example.com/1.0/',
87+
'2.0': 'https://example.com/2.0/',
88+
}
8589
}
8690
8791
The following options are available:
8892

89-
* ``canonical_url`` This will specify a `canonical url <https://en.wikipedia.org/wiki/Canonical_link_element>`__
90-
to let search engines know they should give higher ranking to latest version of the docs.
91-
The url points to the root of the documentation and requires a trailing slash.
93+
``canonical_url``
94+
This will specify a `canonical url
95+
<https://en.wikipedia.org/wiki/Canonical_link_element>`__ to let search
96+
engines know they should give higher ranking to latest version of the
97+
docs. The url points to the root of the documentation and requires a
98+
trailing slash.
99+
100+
``versions``
101+
This option should be defined as a dictionary of versions, where the key
102+
is the version slug and the value is the URL. This will enable a version
103+
dropdown at the top of the menu.
104+
105+
.. note::
106+
This is disabled on Read the Docs currently
92107

93108
Page-level configuration
94109
------------------------
95110

96111
Pages support metadata that changes how the theme renders.
97112
You can currently add the following:
98113

99-
* ``:github_url:`` This will force the "Edit on GitHub" to the configured URL
100-
* ``:bitbucket_url:`` This will force the "Edit on Bitbucket" to the configured URL
101-
* ``:gitlab_url:`` This will force the "Edit on GitLab" to the configured URL
114+
``:github_url:``
115+
This will force the "Edit on GitHub" to the configured URL
116+
117+
``:bitbucket_url:``
118+
This will force the "Edit on Bitbucket" to the configured URL
119+
120+
``:gitlab_url:``
121+
This will force the "Edit on GitLab" to the configured URL
102122

103123
Changelog
104124
=========
@@ -119,6 +139,7 @@ master
119139
* Fix padding on field lists
120140
* Add setuptools entry point allowing to use ``sphinx_rtd_theme`` as
121141
Sphinx ``html_theme`` directly.
142+
* Add version dropdown select
122143

123144
v0.2.4
124145
------

demo_docs/source/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
html_theme_options = {
112112
# 'sticky_navigation': True # Set to False to disable the sticky nav while scrolling.
113113
# 'logo_only': True, # if we have a html_logo below, this shows /only/ the logo with no title text
114+
'versions': {
115+
'1.0': '',
116+
'2.0': '',
117+
'2.1-beta-with-a-long-name': '',
118+
}
114119
}
115120

116121
# Add any paths that contain custom themes here, relative to this directory.

js/theme.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ function ThemeNav () {
8888
});
8989
link.prepend(expand);
9090
});
91+
92+
// Add version selection URL change
93+
$('#version-list').on('change', function () {
94+
self.onVersionChange(this.value);
95+
});
9196
};
9297

9398
nav.reset = function () {
@@ -141,6 +146,10 @@ function ThemeNav () {
141146
this.docHeight = $(document).height();
142147
};
143148

149+
nav.onVersionChange = function (url) {
150+
this.win[0].location = url;
151+
};
152+
144153
nav.hashChange = function () {
145154
this.linkScroll = true;
146155
this.win.one('hashchange', function () {

sass/_theme_layout.sass

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
padding: $base-line-height / 6 $base-line-height / 4
198198
margin-bottom: $gutter / 2
199199
+font-smooth
200-
&:hover
200+
&:hover, &:active, &:focus
201201
background: rgba(255,255,255,.1)
202202
img.logo
203203
display: block // display on its own line all the time
@@ -214,8 +214,44 @@
214214
margin-top: -1 * ($gutter / 4)
215215
margin-bottom: $gutter / 2
216216
font-weight: normal
217-
color: rgba(255,255,255,.3)
218-
217+
color: rgba(255, 255, 255, .3)
218+
> div.version-dropdown
219+
position: relative
220+
display: inline-block
221+
> select
222+
max-width: 8em
223+
padding: .1em 1.5em .1em .5em
224+
text-align-last: center
225+
background: none
226+
border: none
227+
border-radius: 0em
228+
box-shadow: none
229+
font-family: $base-font-family
230+
font-size: 1em
231+
font-weight: normal
232+
color: rgba(255, 255, 255, .3)
233+
// Allow for padding
234+
appearance: none
235+
-webkit-appearance: none
236+
-moz-appearance: none
237+
&:hover, &:active, &:focus
238+
background: rgba(255, 255, 255, .1)
239+
color: rgba(255, 255, 255, .5)
240+
&:after
241+
content: "\f0d7"
242+
font-size: 1em
243+
line-height: 1.2em
244+
font-family: FontAwesome
245+
padding: .1em .5em
246+
position: absolute
247+
right: 0
248+
top: 0
249+
z-index: 1
250+
text-align: center
251+
width: 1.5em
252+
height: 100%
253+
pointer-events: none
254+
box-sizing: border-box
219255

220256
.wy-nav .wy-menu-vertical
221257
header

sphinx_rtd_theme/layout.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,20 @@
113113
{% endif %}
114114
{% if nav_version %}
115115
<div class="version">
116-
{{ nav_version }}
116+
{% if not READTHEDOCS and theme_versions|length > 1 %}
117+
<div class="version-dropdown">
118+
<select class="version-list" id="version-list">
119+
<option value=''>{{ nav_version }}</option>
120+
{% for key, val in theme_versions.items() %}
121+
{% if key != nav_version %}
122+
<option value="{{ val }}">{{ key }}</option>
123+
{% endif %}
124+
{% endfor %}
125+
</select>
126+
</div>
127+
{% else %}
128+
{{ nav_version }}
129+
{% endif %}
117130
</div>
118131
{% endif %}
119132
{% endif %}

0 commit comments

Comments
 (0)