Skip to content

Commit 16c346f

Browse files
docs: Enable switching different versions (#4110) (#4111)
Co-authored-by: Dongdong Tian <[email protected]>
1 parent b291951 commit 16c346f

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

.github/ISSUE_TEMPLATE/release_checklist.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ assignees: ''
2626
- [ ] update changelog
2727
- [ ] update INSTALL.md
2828
- [ ] check if there are any warnings when build the documentation
29+
- [ ] add one new entry in `doc/rst/_static/version_switch.js` if it's a minor release
2930
- [ ] check/set values in `cmake/ConfigDefault.cmake`
3031
- [ ] `GMT_VERSION_YEAR` is current year
3132
- [ ] `GMT_PACKAGE_VERSION_*` is correctly set

doc/rst/_static/version_switch.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 = "docs.generic-mapping-tools.org";
8+
// var doc_url = "0.0.0.0:8000"; // for local testing only
9+
var url_re = new RegExp(doc_url + "\\/(dev|latest|(\\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+
'6.1': '6.1',
16+
'6.0': '6.0',
17+
'5.4': '5.4',
18+
'4.5': '4',
19+
};
20+
21+
function build_select(current_version, current_release) {
22+
var buf = ['<select>'];
23+
24+
$.each(all_versions, function(version, title) {
25+
buf.push('<option value="' + version + '"');
26+
if (version == current_version) {
27+
buf.push(' selected="selected">');
28+
if (version == "latest" || version == "dev") {
29+
buf.push(title + ' (' + current_release + ')');
30+
} else {
31+
buf.push(current_version);
32+
}
33+
} else {
34+
buf.push('>' + title);
35+
}
36+
buf.push('</option>');
37+
});
38+
39+
buf.push('</select>');
40+
return buf.join('');
41+
}
42+
43+
function patch_url(url, new_version) {
44+
return url.replace(url_re, doc_url + '/' + new_version + '/');
45+
}
46+
47+
function on_switch() {
48+
var selected = $(this).children('option:selected').attr('value');
49+
50+
var url = window.location.href,
51+
new_url = patch_url(url, selected);
52+
console.log("OK");
53+
console.log(new_url);
54+
console.log(url);
55+
56+
if (new_url != url) {
57+
// check beforehand if url exists, else redirect to version's start page
58+
$.ajax({
59+
url: new_url,
60+
success: function() {
61+
window.location.href = new_url;
62+
},
63+
error: function() {
64+
window.location.href = 'http://' + doc_url + '/' + selected;
65+
}
66+
});
67+
}
68+
}
69+
70+
$(document).ready(function() {
71+
var match = url_re.exec(window.location.href);
72+
if (match) {
73+
var release = DOCUMENTATION_OPTIONS.VERSION;
74+
var version = match[1];
75+
var select = build_select(version, release);
76+
$('.version_switch_note').html('Or, select a version from the drop-down menu above.');
77+
$('.version').html(select);
78+
$('.version select').bind('change', on_switch);
79+
}
80+
});
81+
})();

doc/rst/_templates/layout.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{# Import the theme's layout. #}
22
{% extends "!layout.html" %}
33

4+
{%- block extrahead %}
5+
{# Enable versions switch #}
6+
{% if enable_versions_switch %}
7+
<!-- Point to the *latest* version switcher. This will allow the latest versions to appear on older documentation. -->
8+
<script type="text/javascript" src="/latest/_static/version_switch.js"></script>
9+
{% endif %}
10+
{% endblock %}
11+
12+
413
{% block menu %}
514
{{ super() }}
615

doc/rst/conf.py.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ html_context = {
6565
"github_repo": "gmt",
6666
"github_version": github_version,
6767
"theme_vcs_pageview_mode": "edit",
68-
"conf_py_path": "/doc/rst/source/"
68+
"conf_py_path": "/doc/rst/source/",
69+
# Enable versions switch on Azure Pipelines.
70+
"enable_versions_switch": True if os.getenv("BUILD_SOURCEBRANCHNAME") else False,
6971
}
7072
# favicon of the docs
7173
html_favicon = "_static/favicon.png"

0 commit comments

Comments
 (0)