Skip to content

Commit

Permalink
Fixed relative links to example and tests (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
zdmytriv authored Jul 19, 2023
1 parent 210100e commit 23d5683
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 200
exclude = .git,__pycache__
ignore = D100,D103
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pylint.messages_control]
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
2 changes: 1 addition & 1 deletion scripts/docs-collator/AbstractRenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def _pre_rendering_fixes(self, repo, module_download_dir):
content = io.read_file_to_string(readme_yaml_file)
content = rendering.remove_targets_md(content)
content = rendering.rename_name(repo, content)
content = rendering.fix_links_to_examples(repo, content)
io.save_string_to_file(readme_yaml_file, content)

def _post_rendering_fixes(self, repo, readme_md_file):
Expand All @@ -29,6 +28,7 @@ def _post_rendering_fixes(self, repo, readme_md_file):
content = rendering.fix_custom_non_self_closing_tags_in_pre(content)
content = rendering.fix_github_edit_url(content, repo)
content = rendering.fix_sidebar_label(content, repo)
content = rendering.replace_relative_links_with_github_links(repo, content)
io.save_string_to_file(readme_md_file, content)

def _copy_extra_resources_for_docs(self, module_download_dir, module_docs_dir):
Expand Down
4 changes: 3 additions & 1 deletion scripts/docs-collator/ModuleRenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ def __copy_extra_resources_for_submodules(self, repo, module_download_dir, modul
rel_path = os.path.relpath(file, module_download_dir)
dest_file = os.path.join(module_docs_dir, rel_path)
submodule_name = os.path.basename(os.path.dirname(dest_file))
submodule_readme_content = io.read_file_to_string(file)
submodule_readme_content = rendering.replace_relative_links_with_github_links(repo, submodule_readme_content, os.path.dirname(rel_path))

content = SUBMODULE_TEMPLATE.render(label=submodule_name,
title=submodule_name,
description=submodule_name,
github_edit_url=f"https://github.com/{repo.full_name}/blob/{repo.default_branch}/{rel_path}",
content=io.read_file_to_string(file))
content=submodule_readme_content)
io.create_dirs(os.path.dirname(dest_file))
io.save_string_to_file(dest_file, content)

Expand Down
30 changes: 25 additions & 5 deletions scripts/docs-collator/utils/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
SIDEBAR_LABEL_REGEX = re.compile('sidebar_label: .*', re.IGNORECASE)
CUSTOM_EDIT_URL_REGEX = re.compile('custom_edit_url: .*', re.IGNORECASE)
NAME_REGEX = re.compile('name: .*', re.IGNORECASE)
RELATIVE_LINK_PATTERN = r"\]\((?!http[s]?://)([^)\s]+)\)"


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


def fix_links_to_examples(repo, content):
return re.sub(r"(\[examples/.*])\((examples/.*)\)",
rf"\1(https://github.com/{repo.full_name}/tree/{repo.default_branch}/\2)", content)


def parse_terraform_repo_name(name):
name_items = name.split('-')
provider = name_items[1]
Expand All @@ -79,3 +75,27 @@ def parse_github_action_repo_name(name):
action_name = '-'.join(name_items[2:])

return action_name


def replace_relative_links_with_github_links(repo, content, relative_path=None):
links = re.findall(RELATIVE_LINK_PATTERN, content)

for link in links:
# ignore links to images, anchors and emails
if link.startswith('images/') or link.startswith('#') or link.startswith('mailto:'):
continue

updated_link = link

# remove leading './' or '/'
if link.startswith('./'):
updated_link = updated_link.replace('./', '', 1)
elif link.startswith('/'):
updated_link = updated_link.replace('/', '', 1)

if relative_path:
updated_link = f"{relative_path}/{updated_link}"

content = content.replace(f"]({link})", f"](https://github.com/{repo.full_name}/tree/{repo.default_branch}/{updated_link})")

return content

0 comments on commit 23d5683

Please sign in to comment.