11import re
22
3+ TAG_REGEX = re .compile ('(<)([0-9a-zA-Z._-]+)(>)' , re .IGNORECASE )
4+ BR_REGEX = re .compile (re .escape ('<br>' ), re .IGNORECASE )
5+ SIDEBAR_LABEL_REGEX = re .compile ('sidebar_label: .*' , re .IGNORECASE )
6+ CUSTOM_EDIT_URL_REGEX = re .compile ('custom_edit_url: .*' , re .IGNORECASE )
7+ NAME_REGEX = re .compile ('name: .*' , re .IGNORECASE )
8+
39
410def fix_self_non_closing_br_tags (content ):
5- regex = re .compile (re .escape ('<br>' ), re .IGNORECASE )
6- return regex .sub ('<br/>' , content )
11+ return BR_REGEX .sub ('<br/>' , content )
712
813
914def fix_custom_non_self_closing_tags_in_pre (content ):
1015 lines = content .splitlines ()
11- tag_regex = re .compile ('(<)([0-9a-zA-Z._-]+)(>)' , re .IGNORECASE )
1216 fixed_lines = []
1317
1418 for line in lines :
@@ -20,7 +24,7 @@ def fix_custom_non_self_closing_tags_in_pre(content):
2024
2125 for group in groups :
2226 before = group
23- after = tag_regex .sub (r"<\2>" , group )
27+ after = TAG_REGEX .sub (r"<\2>" , group )
2428 line = line .replace (f'<pre>{ before } </pre>' , f'<pre>{ after } </pre>' )
2529
2630 fixed_lines .append (line )
@@ -29,15 +33,13 @@ def fix_custom_non_self_closing_tags_in_pre(content):
2933
3034
3135def fix_sidebar_label (content , repo ):
32- regex = re .compile ('sidebar_label: .*' , re .IGNORECASE )
3336 provider , module_name = parse_terraform_repo_name (repo .name )
34- return regex .sub (f'sidebar_label: { module_name } ' , content )
37+ return SIDEBAR_LABEL_REGEX .sub (f'sidebar_label: { module_name } ' , content )
3538
3639
3740def fix_github_edit_url (content , repo ):
38- regex = re .compile ('custom_edit_url: .*' , re .IGNORECASE )
3941 github_edit_url = f"custom_edit_url: https://github.com/{ repo .full_name } /blob/{ repo .default_branch } /README.yaml"
40- return regex .sub (github_edit_url , content )
42+ return CUSTOM_EDIT_URL_REGEX .sub (github_edit_url , content )
4143
4244
4345def remove_logo_from_the_bottom (content ):
@@ -56,8 +58,12 @@ def remove_prefix(string, prefix):
5658
5759
5860def rename_name (repo , content ):
59- regex = re .compile ('name: .*' , re .IGNORECASE )
60- return regex .sub (f'name: { repo .name } ' , content )
61+ return NAME_REGEX .sub (f'name: { repo .name } ' , content )
62+
63+
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 )
6167
6268
6369def parse_terraform_repo_name (name ):
0 commit comments