Skip to content

Commit a57685e

Browse files
authored
Enable switching different versions of documentation (#621)
* Enable switching different versions of documentation How to test this PR locally ``` # check out this branch git checkout doc-switcher # change doc_url to 0.0.0.0:8000 in doc/_static/version_switcher.js # build the documentation cd doc make cd .. # checkout the gh-pages branch git checkout gh-pages # replace the "dev" documentation rm -r dev cp -r doc/_build/html dev # run a simple http server python -m http.server # open http://0.0.0.0:8000/dev/ in your web browser ```
1 parent 4e4dbbc commit a57685e

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

.github/ISSUE_TEMPLATE/release_checklist.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ assignees: ''
1313
**Before release**:
1414
- [ ] Reserve a DOI on [Zenodo](https://zenodo.org) by clicking on "New Version"
1515
- [ ] Update Changelog
16+
- [ ] Add a new entry in `doc/_static/version_switch.js` for documentation switcher
1617

1718
**Release**:
1819
- [ ] Go to [GitHub Release](https://github.com/GenericMappingTools/pygmt/releases) and make a release, this will automatically create a tag too

doc/_static/version_switch.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2013 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
2+
// File originates from the cpython source found in Doc/tools/sphinxext/static/version_switch.js
3+
4+
(function() {
5+
'use strict';
6+
7+
var doc_url = "www.pygmt.org";
8+
//var doc_url = "0.0.0.0:8000"; // for local testing only
9+
var url_re = new RegExp(doc_url + "\\/(dev|latest|(v\\d+\\.\\d+\\.\\d+))\\/");
10+
// List all versions.
11+
// Add one entry "version: title" for any minor releases
12+
var all_versions = {
13+
'latest': 'latest',
14+
'dev': 'dev',
15+
'v0.2.0': 'v0.2.0',
16+
'v0.1.2': 'v0.1.2',
17+
'v0.1.1': 'v0.1.1',
18+
'v0.1.0': 'v0.1.0',
19+
'0.0.1a0': 'v0.0.1a0',
20+
};
21+
22+
function build_select(current_version, current_release) {
23+
var buf = ['<select>'];
24+
25+
$.each(all_versions, function(version, title) {
26+
buf.push('<option value="' + version + '"');
27+
if (version == current_version) {
28+
buf.push(' selected="selected">');
29+
if (version == "latest" || version == "dev") {
30+
buf.push(title + ' (' + current_release + ')');
31+
} else {
32+
buf.push(current_version);
33+
}
34+
} else {
35+
buf.push('>' + title);
36+
}
37+
buf.push('</option>');
38+
});
39+
40+
buf.push('</select>');
41+
return buf.join('');
42+
}
43+
44+
function patch_url(url, new_version) {
45+
return url.replace(url_re, doc_url + '/' + new_version + '/');
46+
}
47+
48+
function on_switch() {
49+
var selected = $(this).children('option:selected').attr('value');
50+
51+
var url = window.location.href,
52+
new_url = patch_url(url, selected);
53+
54+
if (new_url != url) {
55+
// check beforehand if url exists, else redirect to version's start page
56+
$.ajax({
57+
url: new_url,
58+
success: function() {
59+
window.location.href = new_url;
60+
},
61+
error: function() {
62+
window.location.href = 'http://' + doc_url + '/' + selected;
63+
}
64+
});
65+
}
66+
}
67+
68+
$(document).ready(function() {
69+
var match = url_re.exec(window.location.href);
70+
if (match) {
71+
var release = DOCUMENTATION_OPTIONS.VERSION;
72+
var version = match[1];
73+
var select = build_select(version, release);
74+
$('.version_switch_note').html('Or, select a version from the drop-down menu above.');
75+
$('.version').html(select);
76+
$('.version select').bind('change', on_switch);
77+
}
78+
});
79+
})();

doc/_templates/layout.html

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
ga('set', 'anonymizeIp', true);
1616
ga('send', 'pageview');
1717
</script>
18+
19+
<!-- Documentation switcher -->
20+
<!-- Point to the *dev* version switcher. This will allow the latest versions to appear on older documentation. -->
21+
<script type="text/javascript" src="/dev/_static/version_switch.js"></script>
1822
{% endblock %}
1923

2024

doc/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
version = "dev"
104104
else:
105105
version = __version__
106+
release = __version__
106107

107108
# These enable substitutions using |variable| in the rst files
108109
rst_epilog = """

0 commit comments

Comments
 (0)