diff --git a/.flake8 b/.flake8 new file mode 100644 index 000000000..b432e1d67 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 200 +exclude = .git,__pycache__ +ignore = D100,D103 \ No newline at end of file diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 000000000..a59e3df1f --- /dev/null +++ b/.pylintrc @@ -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 \ No newline at end of file diff --git a/scripts/docs-collator/AbstractRenderer.py b/scripts/docs-collator/AbstractRenderer.py index 751bc012e..3cb3389e3 100644 --- a/scripts/docs-collator/AbstractRenderer.py +++ b/scripts/docs-collator/AbstractRenderer.py @@ -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): @@ -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): diff --git a/scripts/docs-collator/ModuleRenderer.py b/scripts/docs-collator/ModuleRenderer.py index eb2ac0ce8..88d6f8c83 100644 --- a/scripts/docs-collator/ModuleRenderer.py +++ b/scripts/docs-collator/ModuleRenderer.py @@ -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) diff --git a/scripts/docs-collator/utils/rendering.py b/scripts/docs-collator/utils/rendering.py index 5560ef08b..c8d4d717a 100644 --- a/scripts/docs-collator/utils/rendering.py +++ b/scripts/docs-collator/utils/rendering.py @@ -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): @@ -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] @@ -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