Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +93 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels wrong. We're taking an absolute path and making it relative. Did you see any examples of where this would be required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it is the other way around. We are taking relative link and replacing them with absolute path links to github:

So this examples/complete gets replaced with https://github.com/cloudposse/terraform-aws-elastic-beanstalk-environment/tree/main/examples/complete

Complete log or what gets replaced with what can be seen here: https://gist.github.com/zdmytriv/3e42f497539b16a786710e01e067fbd7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i got what you mean. There are two modules that have full path.

We can't have absolute paths neither on GitHub nor in docs. Github renders README.html from README.md by making absolute path as relative path to root folder of the repo. This is what we do as well in docs.

I think it is fine to have it like this because we will never run into link that will have absolute path like '/cloudposse/terrraform-aws-sso/...' in README.md.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's try it out.


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