-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort.py
134 lines (106 loc) · 4.19 KB
/
sort.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import re
from collections import OrderedDict
from markdown_to_json.markdown_to_json import CMarkASTNester, Renderer
from markdown_to_json.vendor import CommonMark
README_FILENAME = "README.md"
CONTENT_START_TAG = "<!--CONTENT-->"
CONTENT_STOP_TAG = "<!--CONTENT-->"
def extract_unsorted_markdown_content(filename, content_start_tag, content_stop_tag):
"""Extracts from README file the markdown content to be sorted"""
with open(filename, "r") as f:
# The content to be sorted is between two specific tags
markdown_content = re.search(
f"{content_start_tag}(.*?){content_stop_tag}", f.read(), re.DOTALL
).group(1)
# A level-one markdown heading must be added to allow correct sorting
return "# Content\n" + markdown_content
def convert_markdown_to_json(markdown_content):
"""Convert the markdown content to a JSON object (OrderedDict)"""
nester = CMarkASTNester()
renderer = Renderer()
ast = CommonMark.DocParser().parse(markdown_content)
nested = nester.nest(ast)
rendered = renderer.stringify_dict(nested)
return rendered
def sort_json(json):
"""Recursively sorts a JSON object (OrderedDict)"""
res = OrderedDict()
for k, v in sorted(json.items()):
if isinstance(v, dict):
res[k] = sort_json(v)
elif isinstance(v, list):
res[k] = sorted(v)
else:
res[k] = v
return res
def to_markdown(json, markdown_content, level=1):
"""Recursively converts a JSON object to markdown"""
if isinstance(json, dict):
for k, v in json.items():
if level > 1:
markdown_content.append("")
markdown_content.append("#" * level + " " + k)
to_markdown(v, markdown_content, level=level + 1)
elif isinstance(json, list):
for v in json:
to_markdown(v, markdown_content, level=level + 1)
else:
markdown_content.append("- " + json)
def convert_json_to_markdown(json):
"""Converts the JSON object to markdown"""
markdown_content = []
to_markdown(json, markdown_content)
return "\n".join(markdown_content)
def replace_sorted_markdown(
filename, content_start_tag, content_stop_tag, markdown_content
):
"""Replaces sorted markdown content in README file"""
# Open the file for reading
with open(filename, "r") as f:
# Read the contents of the file
file_contents = f.read()
# # Use regular expressions to find the text between the tags
# pattern = re.compile(
# f"(?<={content_start_tag}).*?(?={content_stop_tag})", re.DOTALL
# )
# old_text = re.search(pattern, file_contents).group(0)
# Replace the old text with the new text
file_contents = re.sub(
f"{content_start_tag}.*?{content_stop_tag}",
f"{content_start_tag}\n{markdown_content}\n{content_stop_tag}",
file_contents,
flags=re.DOTALL,
)
# Open the file for writing and write the modified contents
with open(filename, "w") as f:
f.write(file_contents)
def main():
"""Main method"""
# Extract unsorted content from the README file
unsorted_markdown_content = extract_unsorted_markdown_content(
filename=README_FILENAME,
content_start_tag=CONTENT_START_TAG,
content_stop_tag=CONTENT_STOP_TAG,
)
# Convert unsorted markdown content to JSON object
json_content = convert_markdown_to_json(markdown_content=unsorted_markdown_content)
# Print unsorted JSON object
# print(json.dumps(json_content, indent=2))
# Sort JSON object
sorted_json_content = sort_json(json=json_content)
# Print sorted JSON object
# print(json.dumps(sorted_json_content, indent=2))
# Convert sorted JSON object to markdown content
sorted_markdown_content = convert_json_to_markdown(json=sorted_json_content)
# Print sorted markdown content
# print(sorted_markdown_content)
# Replace sorted markdown content in the README file
replace_sorted_markdown(
filename=README_FILENAME,
content_start_tag=CONTENT_START_TAG,
content_stop_tag=CONTENT_STOP_TAG,
markdown_content=sorted_markdown_content,
)
if __name__ == "__main__":
main()