Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 380ce65

Browse files
committed
#8040: add a version switcher to the documentation. Patch by Yury Selivanov.
1 parent 252cd0e commit 380ce65

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

Doc/tools/sphinxext/layout.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
<li><img src="{{ pathto('_static/py.png', 1) }}" alt=""
44
style="vertical-align: middle; margin-top: -1px"/></li>
55
<li><a href="http://www.python.org/">Python</a>{{ reldelim1 }}</li>
6-
<li><a href="{{ pathto('index') }}">{{ shorttitle }}</a>{{ reldelim1 }}</li>
6+
<li>
7+
{%- if versionswitcher is defined %}
8+
<span class="version_switcher_placeholder">{{ release }}</span>
9+
<a href="{{ pathto('index') }}">Documentation</a>{{ reldelim1 }}
10+
{%- else %}
11+
<a href="{{ pathto('index') }}">{{ shorttitle }}</a>{{ reldelim1 }}
12+
{%- endif %}
13+
</li>
714
{% endblock %}
815
{% block extrahead %}
916
<link rel="shortcut icon" type="image/png" href="{{ pathto('_static/py.png', 1) }}" />
1017
{% if not embedded %}<script type="text/javascript" src="{{ pathto('_static/copybutton.js', 1) }}"></script>{% endif %}
18+
{% if versionswitcher is defined and not embedded %}<script type="text/javascript" src="{{ pathto('_static/version_switch.js', 1) }}"></script>{% endif %}
1119
{{ super() }}
1220
{% endblock %}
1321
{% block footer %}
1422
<div class="footer">
1523
&copy; <a href="{{ pathto('copyright') }}">Copyright</a> {{ copyright|e }}.
1624
<br />
17-
The Python Software Foundation is a non-profit corporation.
25+
The Python Software Foundation is a non-profit corporation.
1826
<a href="http://www.python.org/psf/donations/">Please donate.</a>
1927
<br />
2028
Last updated on {{ last_updated|e }}.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
(function() {
2+
'use strict';
3+
4+
var all_versions = {
5+
'3.4': 'dev (3.4)',
6+
'3.3': '3.3',
7+
'3.2': '3.2',
8+
'2.7': '2.7',
9+
'2.6': '2.6'
10+
};
11+
12+
function build_select(current_version, current_release) {
13+
var buf = ['<select>'];
14+
15+
$.each(all_versions, function(version, title) {
16+
buf.push('<option value="' + version + '"');
17+
if (version == current_version)
18+
buf.push(' selected="selected">' + current_release + '</option>');
19+
else
20+
buf.push('>' + title + '</option>');
21+
});
22+
23+
buf.push('</select>');
24+
return buf.join('');
25+
}
26+
27+
function patch_url(url, new_version) {
28+
var url_re = /\.org\/(\d|py3k|dev|((release\/)?\d\.\d[\w\d\.]*))\//,
29+
new_url = url.replace(url_re, '.org/' + new_version + '/');
30+
31+
if (new_url == url && !new_url.match(url_re)) {
32+
// python 2 url without version?
33+
new_url = url.replace(/\.org\//, '.org/' + new_version + '/');
34+
}
35+
return new_url;
36+
}
37+
38+
function on_switch() {
39+
var selected = $(this).children('option:selected').attr('value');
40+
41+
var url = window.location.href,
42+
new_url = patch_url(url, selected);
43+
44+
if (new_url != url) {
45+
// check beforehand if url exists, else redirect to version's start page
46+
$.get(new_url, function() {
47+
window.location.href = new_url;
48+
}).error(function() {
49+
window.location.href = 'http://docs.python.org/' + selected;
50+
});
51+
}
52+
}
53+
54+
$(document).ready(function() {
55+
var select = build_select(DOCUMENTATION_OPTIONS.VERSION,
56+
DOCUMENTATION_OPTIONS.RELEASE);
57+
$('.version_switcher_placeholder').html(select);
58+
$('.version_switcher_placeholder select').bind('change', on_switch);
59+
});
60+
})();

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ Build
636636
Documentation
637637
-------------
638638

639+
- Issue #8040: added a version switcher to the documentation. Patch by
640+
Yury Selivanov.
641+
639642
- Issue #16115: Improve subprocess.Popen() documentation around args, shell,
640643
and executable arguments.
641644

0 commit comments

Comments
 (0)