-
Notifications
You must be signed in to change notification settings - Fork 35
/
external_version_warning.py
72 lines (59 loc) · 2.09 KB
/
external_version_warning.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
from docutils import nodes
try:
# Available from Sphinx 1.6
from sphinx.util.logging import getLogger
except ImportError:
from logging import getLogger
log = getLogger(__name__)
def process_external_version_warning_banner(app, doctree, fromdocname):
"""
Add warning banner for external versions in every page.
If the version type is external this will show a warning banner
at the top of each page of the documentation.
"""
# Sphinx itself always emits this with a document node,
# but extensions can also call `resolve_references` with other types
# of nodes, we don't want to inject the banner in those.
# Details:
# - https://github.com/readthedocs/readthedocs-sphinx-ext/issues/113
# - https://github.com/readthedocs/readthedocs-sphinx-ext/pull/114
if not isinstance(doctree, nodes.document):
return
is_gitlab = app.config.html_context.get('display_gitlab')
name = 'merge request' if is_gitlab else 'pull request'
build_url = app.config.readthedocs_build_url
build_url_node = nodes.reference(
'',
'',
nodes.Text('was created '),
internal=False,
refuri=build_url,
)
pr_number = app.config.html_context.get('current_version')
pr_number = '#{number}'.format(number=pr_number)
vcs_url = app.config.readthedocs_vcs_url
vcs_url_node = nodes.reference(
'',
'',
nodes.Text(pr_number),
internal=False,
refuri=vcs_url,
)
children = [
nodes.Text('This page '),
build_url_node, # was created
nodes.Text('from a {name} ('.format(name=name)),
vcs_url_node, # #123
nodes.Text(').'),
]
prose = nodes.paragraph('', '', *children)
warning_node = nodes.warning(prose, prose)
doctree.insert(0, warning_node)
def setup(app):
app.connect('doctree-resolved', process_external_version_warning_banner)
# Settings
app.add_config_value('readthedocs_vcs_url', '', 'html')
app.add_config_value('readthedocs_build_url', '', 'html')
return {
'parallel_read_safe': True,
}