Skip to content

Commit 23d5683

Browse files
authored
Fixed relative links to example and tests (#589)
1 parent 210100e commit 23d5683

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 200
3+
exclude = .git,__pycache__
4+
ignore = D100,D103

.pylintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pylint.messages_control]
2+
disable = missing-module-docstring,missing-function-docstring,missing-class-docstring,line-too-long,too-many-instance-attributes,too-few-public-methods,logging-fstring-interpolation,raise-missing-from,too-many-function-args

scripts/docs-collator/AbstractRenderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def _pre_rendering_fixes(self, repo, module_download_dir):
2020
content = io.read_file_to_string(readme_yaml_file)
2121
content = rendering.remove_targets_md(content)
2222
content = rendering.rename_name(repo, content)
23-
content = rendering.fix_links_to_examples(repo, content)
2423
io.save_string_to_file(readme_yaml_file, content)
2524

2625
def _post_rendering_fixes(self, repo, readme_md_file):
@@ -29,6 +28,7 @@ def _post_rendering_fixes(self, repo, readme_md_file):
2928
content = rendering.fix_custom_non_self_closing_tags_in_pre(content)
3029
content = rendering.fix_github_edit_url(content, repo)
3130
content = rendering.fix_sidebar_label(content, repo)
31+
content = rendering.replace_relative_links_with_github_links(repo, content)
3232
io.save_string_to_file(readme_md_file, content)
3333

3434
def _copy_extra_resources_for_docs(self, module_download_dir, module_docs_dir):

scripts/docs-collator/ModuleRenderer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ def __copy_extra_resources_for_submodules(self, repo, module_download_dir, modul
9696
rel_path = os.path.relpath(file, module_download_dir)
9797
dest_file = os.path.join(module_docs_dir, rel_path)
9898
submodule_name = os.path.basename(os.path.dirname(dest_file))
99+
submodule_readme_content = io.read_file_to_string(file)
100+
submodule_readme_content = rendering.replace_relative_links_with_github_links(repo, submodule_readme_content, os.path.dirname(rel_path))
99101

100102
content = SUBMODULE_TEMPLATE.render(label=submodule_name,
101103
title=submodule_name,
102104
description=submodule_name,
103105
github_edit_url=f"https://github.com/{repo.full_name}/blob/{repo.default_branch}/{rel_path}",
104-
content=io.read_file_to_string(file))
106+
content=submodule_readme_content)
105107
io.create_dirs(os.path.dirname(dest_file))
106108
io.save_string_to_file(dest_file, content)
107109

scripts/docs-collator/utils/rendering.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
SIDEBAR_LABEL_REGEX = re.compile('sidebar_label: .*', re.IGNORECASE)
66
CUSTOM_EDIT_URL_REGEX = re.compile('custom_edit_url: .*', re.IGNORECASE)
77
NAME_REGEX = re.compile('name: .*', re.IGNORECASE)
8+
RELATIVE_LINK_PATTERN = r"\]\((?!http[s]?://)([^)\s]+)\)"
89

910

1011
def fix_self_non_closing_br_tags(content):
@@ -61,11 +62,6 @@ def rename_name(repo, content):
6162
return NAME_REGEX.sub(f'name: {repo.name}', content)
6263

6364

64-
def fix_links_to_examples(repo, content):
65-
return re.sub(r"(\[examples/.*])\((examples/.*)\)",
66-
rf"\1(https://github.com/{repo.full_name}/tree/{repo.default_branch}/\2)", content)
67-
68-
6965
def parse_terraform_repo_name(name):
7066
name_items = name.split('-')
7167
provider = name_items[1]
@@ -79,3 +75,27 @@ def parse_github_action_repo_name(name):
7975
action_name = '-'.join(name_items[2:])
8076

8177
return action_name
78+
79+
80+
def replace_relative_links_with_github_links(repo, content, relative_path=None):
81+
links = re.findall(RELATIVE_LINK_PATTERN, content)
82+
83+
for link in links:
84+
# ignore links to images, anchors and emails
85+
if link.startswith('images/') or link.startswith('#') or link.startswith('mailto:'):
86+
continue
87+
88+
updated_link = link
89+
90+
# remove leading './' or '/'
91+
if link.startswith('./'):
92+
updated_link = updated_link.replace('./', '', 1)
93+
elif link.startswith('/'):
94+
updated_link = updated_link.replace('/', '', 1)
95+
96+
if relative_path:
97+
updated_link = f"{relative_path}/{updated_link}"
98+
99+
content = content.replace(f"]({link})", f"](https://github.com/{repo.full_name}/tree/{repo.default_branch}/{updated_link})")
100+
101+
return content

0 commit comments

Comments
 (0)