-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
51 lines (40 loc) · 1.63 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
json_file_path = 'html_tags.json'
readme_format_path = 'README-format.md'
readme_output_path = 'README.md'
def read_json(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
def read_template(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def sanitize_text(text, isText):
"""Replace '|' with '/', '\n' with ' ' to avoid issues in table formatting."""
if isText:
if '<' in text:
text = text.replace('<', '<')
if '>' in text:
text = text.replace('>', '>')
return text.replace('|', '\\|').replace('\n', ' ')
def generate_table(tags):
table_header = "| Tag Name | Brief Description | Learn More Link |\n"
table_header += "|---------------|---------------------------|---------------------------------|\n"
table_rows = ""
for tag in tags:
name = sanitize_text(tag['name'], False)
brief = sanitize_text(tag['brief'], True)
link = tag['link']
table_rows += f"| `{name}` | {brief} | [Learn More]({link}) |\n"
return table_header + table_rows
def write_readme(output_path, template, table):
readme_content = template.replace("{table}", table)
with open(output_path, 'w', encoding='utf-8') as file:
file.write(readme_content)
def main():
tags = read_json(json_file_path)
template = read_template(readme_format_path)
table = generate_table(tags)
write_readme(readme_output_path, template, table)
print(f"README.md has been successfully updated!")
if __name__ == "__main__":
main()