2017-01-19 03:16:06 -05:00
|
|
|
# based on http://protips.readthedocs.io/link-roles.html
|
|
|
|
|
2018-09-07 07:46:50 -04:00
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
2018-01-17 14:01:36 -05:00
|
|
|
import re
|
2018-03-15 14:28:58 -04:00
|
|
|
import os
|
2019-11-28 16:56:53 -05:00
|
|
|
import subprocess
|
2017-01-19 03:16:06 -05:00
|
|
|
from docutils import nodes
|
2020-04-17 02:29:39 -04:00
|
|
|
from collections import namedtuple
|
2017-01-19 03:16:06 -05:00
|
|
|
|
2018-12-01 03:25:08 -05:00
|
|
|
|
2017-01-19 03:16:06 -05:00
|
|
|
def get_github_rev():
|
2020-02-04 01:36:23 -05:00
|
|
|
path = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode('utf-8')
|
2019-11-28 16:56:53 -05:00
|
|
|
try:
|
2020-02-04 01:36:23 -05:00
|
|
|
tag = subprocess.check_output(['git', 'describe', '--exact-match']).strip().decode('utf-8')
|
2019-11-28 16:56:53 -05:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
tag = None
|
2018-09-07 07:46:50 -04:00
|
|
|
print('Git commit ID: ', path)
|
2019-11-28 16:56:53 -05:00
|
|
|
if tag:
|
2018-09-07 07:46:50 -04:00
|
|
|
print('Git tag: ', tag)
|
2019-11-28 16:56:53 -05:00
|
|
|
return tag
|
2017-01-19 03:16:06 -05:00
|
|
|
return path
|
|
|
|
|
2018-12-01 03:25:08 -05:00
|
|
|
|
2020-04-17 02:29:39 -04:00
|
|
|
# Creates a dict of all submodules with the format {submodule_path : (url relative to git root), commit)}
|
|
|
|
def get_submodules():
|
|
|
|
git_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).strip().decode('utf-8')
|
|
|
|
gitmodules_file = os.path.join(git_root, '.gitmodules')
|
|
|
|
|
|
|
|
submodules = subprocess.check_output(['git', 'submodule', 'status']).strip().decode('utf-8').split('\n')
|
|
|
|
|
|
|
|
submodule_dict = {}
|
|
|
|
Submodule = namedtuple('Submodule', 'url rev')
|
|
|
|
for sub in submodules:
|
|
|
|
sub_info = sub.lstrip().split(' ')
|
|
|
|
|
|
|
|
# Get short hash, 7 digits
|
|
|
|
rev = sub_info[0].lstrip('-')[0:7]
|
|
|
|
path = sub_info[1].lstrip('./')
|
|
|
|
|
|
|
|
config_key_arg = "submodule.{}.url".format(path)
|
|
|
|
rel_url = subprocess.check_output(['git', 'config', '--file', gitmodules_file, '--get', config_key_arg]).decode('utf-8').lstrip('./').rstrip('\n')
|
|
|
|
|
|
|
|
submodule_dict[path] = Submodule(rel_url, rev)
|
|
|
|
|
|
|
|
return submodule_dict
|
|
|
|
|
|
|
|
|
2020-03-22 19:26:00 -04:00
|
|
|
def url_join(*url_parts):
|
|
|
|
""" Make a URL out of multiple components, assume first part is the https:// part and
|
|
|
|
anything else is a path component """
|
|
|
|
result = "/".join(url_parts)
|
|
|
|
result = re.sub(r"([^:])//+", r"\1/", result) # remove any // that isn't in the https:// part
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2017-01-19 03:16:06 -05:00
|
|
|
def setup(app):
|
|
|
|
rev = get_github_rev()
|
2020-04-17 02:29:39 -04:00
|
|
|
submods = get_submodules()
|
2018-03-15 14:28:58 -04:00
|
|
|
|
|
|
|
# links to files or folders on the GitHub
|
2020-04-17 02:29:39 -04:00
|
|
|
app.add_role('idf', github_link('tree', rev, submods, '/', app.config))
|
|
|
|
app.add_role('idf_file', github_link('blob', rev, submods, '/', app.config))
|
|
|
|
app.add_role('idf_raw', github_link('raw', rev, submods, '/', app.config))
|
|
|
|
app.add_role('component', github_link('tree', rev, submods, '/components/', app.config))
|
|
|
|
app.add_role('component_file', github_link('blob', rev, submods, '/components/', app.config))
|
|
|
|
app.add_role('component_raw', github_link('raw', rev, submods, '/components/', app.config))
|
|
|
|
app.add_role('example', github_link('tree', rev, submods, '/examples/', app.config))
|
|
|
|
app.add_role('example_file', github_link('blob', rev, submods, '/examples/', app.config))
|
|
|
|
app.add_role('example_raw', github_link('raw', rev, submods, '/examples/', app.config))
|
2017-01-19 03:16:06 -05:00
|
|
|
|
2018-03-15 14:28:58 -04:00
|
|
|
# link to the current documentation file in specific language version
|
2020-03-05 05:05:23 -05:00
|
|
|
app.add_role('link_to_translation', link_to_translation(app.config))
|
2018-03-15 14:28:58 -04:00
|
|
|
|
2020-04-17 02:29:39 -04:00
|
|
|
return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.4'}
|
2019-11-28 00:00:24 -05:00
|
|
|
|
2018-12-01 03:25:08 -05:00
|
|
|
|
2020-04-17 02:29:39 -04:00
|
|
|
def github_link(link_type, idf_rev, submods, root_path, app_config):
|
2017-01-19 03:16:06 -05:00
|
|
|
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
|
2020-02-05 21:28:59 -05:00
|
|
|
msgs = []
|
2020-04-17 02:29:39 -04:00
|
|
|
BASE_URL = 'https://github.com/'
|
|
|
|
IDF_REPO = "espressif/esp-idf"
|
2020-02-05 21:28:59 -05:00
|
|
|
|
|
|
|
def warning(msg):
|
|
|
|
system_msg = inliner.reporter.warning(msg)
|
|
|
|
system_msg.line = lineno
|
|
|
|
msgs.append(system_msg)
|
|
|
|
|
2020-04-17 02:29:39 -04:00
|
|
|
# Redirects to submodule repo if path is a submodule, else default to IDF repo
|
|
|
|
def redirect_submodule(path, submods, rev):
|
|
|
|
for key, value in submods.items():
|
|
|
|
if path.lstrip('/').startswith(key):
|
|
|
|
return value.url.replace('.git', ''), value.rev, re.sub('^/{}/'.format(key), '', path)
|
|
|
|
|
|
|
|
return IDF_REPO, rev, path
|
2020-02-05 21:28:59 -05:00
|
|
|
|
|
|
|
# search for a named link (:label<path>) with descriptive label vs a plain URL
|
|
|
|
m = re.search(r'(.*)\s*<(.*)>', text)
|
2018-01-17 14:01:36 -05:00
|
|
|
if m:
|
|
|
|
link_text = m.group(1)
|
|
|
|
link = m.group(2)
|
|
|
|
else:
|
|
|
|
link_text = text
|
|
|
|
link = text
|
2020-02-05 21:28:59 -05:00
|
|
|
|
|
|
|
rel_path = root_path + link
|
|
|
|
abs_path = os.path.join(app_config.idf_path, rel_path.lstrip('/'))
|
2020-04-17 02:29:39 -04:00
|
|
|
|
|
|
|
repo, repo_rev, rel_path = redirect_submodule(rel_path, submods, idf_rev)
|
|
|
|
|
2020-02-05 21:28:59 -05:00
|
|
|
line_no = None
|
2020-04-17 02:29:39 -04:00
|
|
|
url = url_join(BASE_URL, repo, link_type, repo_rev, rel_path)
|
2020-02-05 21:28:59 -05:00
|
|
|
|
|
|
|
if '#L' in abs_path:
|
|
|
|
# drop any URL line number from the file, line numbers take the form #Lnnn or #Lnnn-Lnnn for a range
|
|
|
|
abs_path, line_no = abs_path.split('#L')
|
|
|
|
line_no = re.search(r'^(\d+)(?:-L(\d+))?', line_no)
|
|
|
|
if line_no is None:
|
|
|
|
warning("Line number anchor in URL %s doesn't seem to be valid" % link)
|
|
|
|
else:
|
|
|
|
line_no = tuple(int(l) for l in line_no.groups() if l) # tuple of (nnn,) or (nnn, NNN) for ranges
|
|
|
|
elif '#' in abs_path: # drop any other anchor from the line
|
|
|
|
abs_path = abs_path.split('#')[0]
|
|
|
|
warning("URL %s seems to contain an unusable anchor after the #, only line numbers are supported" % link)
|
|
|
|
|
|
|
|
is_dir = (link_type == 'tree')
|
|
|
|
|
|
|
|
if not os.path.exists(abs_path):
|
|
|
|
warning("IDF path %s does not appear to exist (absolute path %s)" % (rel_path, abs_path))
|
|
|
|
elif is_dir and not os.path.isdir(abs_path):
|
|
|
|
# note these "wrong type" warnings are not strictly needed as GitHub will apply a redirect,
|
|
|
|
# but the may become important in the future (plus make for cleaner links)
|
|
|
|
warning("IDF path %s is not a directory but role :%s: is for linking to a directory, try :%s_file:" % (rel_path, name, name))
|
|
|
|
elif not is_dir and os.path.isdir(abs_path):
|
|
|
|
warning("IDF path %s is a directory but role :%s: is for linking to a file" % (rel_path, name))
|
|
|
|
|
|
|
|
# check the line number is valid
|
|
|
|
if line_no:
|
|
|
|
if is_dir:
|
|
|
|
warning("URL %s contains a line number anchor but role :%s: is for linking to a directory" % (rel_path, name, name))
|
|
|
|
elif os.path.exists(abs_path) and not os.path.isdir(abs_path):
|
|
|
|
with open(abs_path, "r") as f:
|
|
|
|
lines = len(f.readlines())
|
|
|
|
if any(True for l in line_no if l > lines):
|
|
|
|
warning("URL %s specifies a range larger than file (file has %d lines)" % (rel_path, lines))
|
|
|
|
|
|
|
|
if tuple(sorted(line_no)) != line_no: # second line number comes before first one!
|
|
|
|
warning("URL %s specifies a backwards line number range" % rel_path)
|
|
|
|
|
2018-01-17 14:01:36 -05:00
|
|
|
node = nodes.reference(rawtext, link_text, refuri=url, **options)
|
2020-02-05 21:28:59 -05:00
|
|
|
return [node], msgs
|
2017-01-19 03:16:06 -05:00
|
|
|
return role
|
2018-03-15 14:28:58 -04:00
|
|
|
|
2018-12-01 03:25:08 -05:00
|
|
|
|
2020-03-05 05:05:23 -05:00
|
|
|
def link_to_translation(config):
|
2018-03-15 14:28:58 -04:00
|
|
|
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)
|
2020-03-05 05:05:23 -05:00
|
|
|
return_path = '../' * doc_path.count('/') # path back to the root from 'docname'
|
|
|
|
# then take off 3 more paths for language/release/targetname and build the new URL
|
|
|
|
url = "{}.html".format(os.path.join(return_path, '../../..', language, config.release,
|
|
|
|
config.idf_target, docname))
|
2018-03-15 14:28:58 -04:00
|
|
|
node = nodes.reference(rawtext, link_text, refuri=url, **options)
|
|
|
|
return [node], []
|
|
|
|
return role
|