Skip to content

Enable switching different versions of documentation #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/release_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ assignees: ''
**Before release**:
- [ ] Reserve a DOI on [Zenodo](https://zenodo.org) by clicking on "New Version"
- [ ] Update Changelog
- [ ] Add a new entry in `doc/_static/version_switch.js` for documentation switcher

**Release**:
- [ ] Go to [GitHub Release](https://github.com/GenericMappingTools/pygmt/releases) and make a release, this will automatically create a tag too
Expand Down
79 changes: 79 additions & 0 deletions doc/_static/version_switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2013 PSF. Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
// File originates from the cpython source found in Doc/tools/sphinxext/static/version_switch.js

(function() {
'use strict';

var doc_url = "www.pygmt.org";
//var doc_url = "0.0.0.0:8000"; // for local testing only
var url_re = new RegExp(doc_url + "\\/(dev|latest|(v\\d+\\.\\d+\\.\\d+))\\/");
// List all versions.
// Add one entry "version: title" for any minor releases
var all_versions = {
'latest': 'latest',
'dev': 'dev',
'v0.2.0': 'v0.2.0',
'v0.1.2': 'v0.1.2',
'v0.1.1': 'v0.1.1',
'v0.1.0': 'v0.1.0',
'0.0.1a0': 'v0.0.1a0',
};

function build_select(current_version, current_release) {
var buf = ['<select>'];

$.each(all_versions, function(version, title) {
buf.push('<option value="' + version + '"');
if (version == current_version) {
buf.push(' selected="selected">');
if (version == "latest" || version == "dev") {
buf.push(title + ' (' + current_release + ')');
} else {
buf.push(current_version);
}
} else {
buf.push('>' + title);
}
buf.push('</option>');
});

buf.push('</select>');
return buf.join('');
}

function patch_url(url, new_version) {
return url.replace(url_re, doc_url + '/' + new_version + '/');
}

function on_switch() {
var selected = $(this).children('option:selected').attr('value');

var url = window.location.href,
new_url = patch_url(url, selected);

if (new_url != url) {
// check beforehand if url exists, else redirect to version's start page
$.ajax({
url: new_url,
success: function() {
window.location.href = new_url;
},
error: function() {
window.location.href = 'http://' + doc_url + '/' + selected;
}
});
}
}

$(document).ready(function() {
var match = url_re.exec(window.location.href);
if (match) {
var release = DOCUMENTATION_OPTIONS.VERSION;
var version = match[1];
var select = build_select(version, release);
$('.version_switch_note').html('Or, select a version from the drop-down menu above.');
$('.version').html(select);
$('.version select').bind('change', on_switch);
}
});
})();
4 changes: 4 additions & 0 deletions doc/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>

<!-- Documentation switcher -->
<!-- Point to the *dev* version switcher. This will allow the latest versions to appear on older documentation. -->
<script type="text/javascript" src="/dev/_static/version_switch.js"></script>
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a frontend/javascript expert, and I don't expect you to work on this. But, just pointing out that the dropdown isn't super-accessible. I managed to tab-tab-tab my way to the version changer dropdown (with the benefit of sight), and change to a different version of the documentation by pressing up/down with my arrow keys (only once though, it doesn't seem to allow scrolling through the list of options).

Guess my point here is that we should keep the section at https://www.pygmt.org/v0.2.0/index.html#documentation-for-other-versions for now just for a11y reasons, until we get a good developer that knows this stuff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's open an issue and see if someone with good web knowledge can help us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes are online now, and you can try it at https://www.pygmt.org.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay 🎉! I see you've made the change directly at ae2aee5. Good work 👍

{% endblock %}


Expand Down
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
version = "dev"
else:
version = __version__
release = __version__

# These enable substitutions using |variable| in the rst files
rst_epilog = """
Expand Down