Skip to content

Commit ccfbd5b

Browse files
billsacksfritzt
authored andcommitted
Change html build to make versioned documentation with a dropdown menu
This changes the documentation theme to use the readthedocs theme, with some JavaScript that provides capabilities for a dropdown menu allowing you to select between different versions. This mimics the changes in ESMCI/cime#3439, which in turn was based on ESCOMP/CISM-wrapper#23.
1 parent 97469e8 commit ccfbd5b

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

doc/source/_static/pop_ver.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
$(document).ready(function() {
2+
/* For a URL that looks like
3+
https://blah.github.io/versions/VERSIONFOO/html/bar/index.html, set cur_version_dir to
4+
'VERSIONFOO' (i.e., the portion of the path following 'versions').
5+
*/
6+
var proj_end = document.baseURI.indexOf("versions") + 9;
7+
var end = document.baseURI.indexOf("/", proj_end);
8+
var cur_version_dir = document.baseURI.substring(proj_end, end);
9+
var mylist = $("#version-list");
10+
mylist.empty();
11+
$.getJSON(version_json_loc, function(data) {
12+
if (data.hasOwnProperty(cur_version_dir)) {
13+
/* First add the current version so that it appears first in the drop-down
14+
menu and starts as the selected element of the menu. If you click on the
15+
current version, you should stay at the current page.
16+
17+
The conditional around this block should generally be true, but we check it
18+
just in case the current version is missing from the versions.json file for
19+
some reason.
20+
*/
21+
cur_version_name = data[cur_version_dir];
22+
mylist.append($("<option>", {value: document.baseURI, text: cur_version_name}));
23+
}
24+
// Now add the other versions
25+
$.each(data, function(version_dir, version_name) {
26+
if (version_dir != cur_version_dir) {
27+
/* If you click on a different version, you should go to the root of the
28+
documentation for that version. This assumes paths like
29+
https://blah.github.io/versions/VERSIONFOO/html/bar/index.html. So we
30+
need to go up two levels from the URL_ROOT (html) to then go back down
31+
into the appropriate version's html directory.
32+
*/
33+
mylist.append($("<option>", {value: DOCUMENTATION_OPTIONS.URL_ROOT + '../../' + version_dir + '/html', text: version_name}));
34+
}
35+
});
36+
});
37+
});

doc/source/_templates/footer.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends "!footer.html" %}
2+
{% block extrafooter %}
3+
{{ super() }}
4+
<script>var version_json_loc = "{{ pathto('../../versions.json', 1) }}";</script>
5+
{% endblock %}

doc/source/_templates/layout.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% extends "!layout.html" %}
2+
3+
{% set script_files = script_files + ["_static/pop_ver.js"] %}

doc/source/conf.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
# add these directories to sys.path here. If the directory is relative to the
1717
# documentation root, use os.path.abspath to make it absolute, like shown here.
1818
#
19-
# import os
20-
# import sys
19+
import os
20+
import sys
21+
# Note that we need a specific version of sphinx_rtd_theme. This can be obtained with:
22+
# pip install git+https://github.com/esmci/sphinx_rtd_theme.git@version-dropdown-with-fixes
23+
import sphinx_rtd_theme
2124
# sys.path.insert(0, os.path.abspath('.'))
2225

2326

@@ -88,13 +91,20 @@
8891
# The theme to use for HTML and HTML Help pages. See the documentation for
8992
# a list of builtin themes.
9093
#
91-
html_theme = 'bizstyle'
94+
html_theme = 'sphinx_rtd_theme'
9295

9396
# Theme options are theme-specific and customize the look and feel of a theme
9497
# further. For a list of options available for each theme, see the
9598
# documentation.
9699
#
97-
# html_theme_options = {}
100+
# The 'versions' option needs to have at least two versions to work, but it doesn't need
101+
# to have all versions: others will be added dynamically. Note that this maps from version
102+
# names to html links. The current version can link to the current location (i.e., do
103+
# nothing). For the other version, we just add a place-holder; its name and value are
104+
# unimportant because these versions will get replaced dynamically.
105+
html_theme_options = {}
106+
html_theme_options['versions'] = {version: ''}
107+
html_theme_options['versions']['[placeholder]'] = ''
98108

99109
# Add any paths that contain custom static files (such as style sheets) here,
100110
# relative to this directory. They are copied after the builtin static files,

0 commit comments

Comments
 (0)