-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathgensidebar.py
85 lines (68 loc) · 2.96 KB
/
gensidebar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Generates sidebar/toctree.
Generates the common sidebar/toctree for the sphinx/ReadTheDocs
documentation of the ESMValTool and its subprojects.
"""
import os
def _write_if_changed(fname, contents):
"""Write/update file only if changed."""
try:
with open(fname, "r", encoding="utf-8") as stream:
old_contents = stream.read()
except IOError:
old_contents = ""
if old_contents != contents:
with open(fname, "w", encoding="utf-8") as stream:
stream.write(contents)
def generate_sidebar(conf, conf_api):
"""Generate sidebar.
Generate sidebar for ReadTheDocs with links to subprojects and
superprojects accordingly.
"""
# determine 'latest' or 'stable'
# if not conf.do_gen:
do_gen = os.environ.get("SIDEBAR", None) == "1" or conf["on_rtd"]
lines = ["", ".. DO NOT MODIFY! THIS PAGE IS AUTOGENERATED!", ""]
def _toctree():
lines.extend([".. toctree::", " :maxdepth: 1", ""])
def _endl():
lines.append("")
def _write(project, desc, link, mapping=conf["intersphinx_mapping"]):
if project != conf_api:
if do_gen:
args = desc, mapping[project][0], link
lines.append(" %s <%s%s.html>" % args)
else:
args = desc, link
lines.append(" %s <%s>" % args)
def _header(project, text):
if project == conf_api or do_gen:
lines.extend([".. toctree::", " :maxdepth: 2"])
lines.extend([" :caption: %s" % text, ""])
#
# Specify the sidebar contents here
#
_header("esmvaltool", "ESMValTool")
_write("esmvaltool", "Introduction", "introduction")
_write("esmvaltool", "ESMValTool Functionalities", "functionalities")
_write("esmvaltool", "Getting started", "quickstart/index")
_write("esmvaltool", "Gallery", "gallery")
_write("esmvaltool", "Available recipes", "recipes/index")
_write("esmvaltool", "Obtaining input data", "input")
_write("esmvaltool", "Making a recipe or diagnostic", "develop/index")
_write("esmvaltool", "Contributing to the community", "community/index")
_write("esmvaltool", "Utilities", "utils")
_write("esmvaltool", "Diagnostics API Reference", "api/esmvaltool")
_write("esmvaltool", "Frequently Asked Questions", "faq")
_write("esmvaltool", "Changelog", "changelog")
_endl()
_header("esmvalcore", "ESMValCore")
_write("esmvalcore", "Getting started", "quickstart/index")
_write("esmvalcore", "Example notebooks", "example-notebooks")
_write("esmvalcore", "The recipe format", "recipe/index")
_write("esmvalcore", "Diagnostic script interfaces", "interfaces")
_write("esmvalcore", "Development", "develop/index")
_write("esmvalcore", "Contributing", "contributing")
_write("esmvalcore", "ESMValCore API Reference", "api/esmvalcore")
_write("esmvalcore", "Changelog", "changelog")
_endl()
_write_if_changed("_sidebar.rst.inc", "\n".join(lines))