forked from espressif/esp-adf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link-roles.py
74 lines (62 loc) · 2.89 KB
/
link-roles.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
# based on http://protips.readthedocs.io/link-roles.html
from __future__ import print_function
from __future__ import unicode_literals
import re
import os
from docutils import nodes
from local_util import run_cmd_get_output
def get_github_rev():
path = run_cmd_get_output('git rev-parse --short HEAD')
tag = run_cmd_get_output('git describe --exact-match')
print('Git commit ID: ', path)
if len(tag):
print('Git tag: ', tag)
path = tag
return path
def setup(app):
rev = get_github_rev()
# links to files or folders on the GitHub
baseurl = 'https://github.com/espressif/esp-adf'
app.add_role('adf', autolink('{}/tree/{}/%s'.format(baseurl, rev)))
app.add_role('adf_file', autolink('{}/blob/{}/%s'.format(baseurl, rev)))
app.add_role('adf_raw', autolink('{}/raw/{}/%s'.format(baseurl, rev)))
app.add_role('component', autolink('{}/tree/{}/components/%s'.format(baseurl, rev)))
app.add_role('component_file', autolink('{}/blob/{}/components/%s'.format(baseurl, rev)))
app.add_role('component_raw', autolink('{}/raw/{}/components/%s'.format(baseurl, rev)))
app.add_role('example', autolink('{}/tree/{}/examples/%s'.format(baseurl, rev)))
app.add_role('example_file', autolink('{}/blob/{}/examples/%s'.format(baseurl, rev)))
app.add_role('example_raw', autolink('{}/raw/{}/examples/%s'.format(baseurl, rev)))
# link to the current documentation file in specific language version
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
if on_rtd:
# provide RTD specific commit identification to be included in the link
tag_rev = 'latest'
if (run_cmd_get_output('git rev-parse --short HEAD') != rev):
tag_rev = rev
else:
# if not on the RTD then provide generic identification
tag_rev = run_cmd_get_output('git describe --always')
app.add_role('link_to_translation', crosslink('%s../../%s/{}/%s.html'.format(tag_rev)))
def autolink(pattern):
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
m = re.search('(.*)\s*<(.*)>', text) # noqa: W605 - regular expression
if m:
link_text = m.group(1)
link = m.group(2)
else:
link_text = text
link = text
url = pattern % (link,)
node = nodes.reference(rawtext, link_text, refuri=url, **options)
return [node], []
return role
def crosslink(pattern):
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
(language, link_text) = text.split(':')
docname = inliner.document.settings.env.docname
doc_path = inliner.document.settings.env.doc2path(docname, None, None)
return_path = '../' * doc_path.count('/')
url = pattern % (return_path, language, docname)
node = nodes.reference(rawtext, link_text, refuri=url, **options)
return [node], []
return role