This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdate_nodes_md.py
183 lines (138 loc) · 5.02 KB
/
update_nodes_md.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
EXAMPLE_SECTION_START = "## Examples"
EXAMPLE_SECTION_END = "<SectionBreak />"
AUTOGEN_FOLDER = "a1-[autogen]"
def with_light_mode(node_name: str, example_title: str):
return f"""
import Example1 from './examples/EX1/example.md';
import App1 from '!!raw-loader!./examples/EX1/app.json';
import appImg from './examples/EX1/app.jpeg'
import appImgLight from './examples/EX1/app_light.jpeg'
import outputImg from './examples/EX1/output.jpeg'
import outputLight from './examples/EX1/output_light.jpeg'
### {example_title.strip() if example_title.strip() != "" else f"{node_name} example"}
<AppDisplay
nodeLabel='{node_name}'
appImg={{appImg}}
appLight={{appImgLight}}
outputLight={{outputLight}}
outputImg={{outputImg}}
>
{{App1}}
</AppDisplay>
<Example1 />
"""
def without_light_mode(node_name: str, example_title: str):
return f"""
import Example1 from './examples/EX1/example.md';
import App1 from '!!raw-loader!./examples/EX1/app.json';
import appImg from './examples/EX1/app.jpeg'
import outputImg from './examples/EX1/output.jpeg'
### {example_title.strip() if example_title.strip() != "" else f"{node_name} example"}
<AppDisplay
nodeLabel='{node_name}'
appImg={{appImg}}
outputImg={{outputImg}}
>
{{App1}}
</AppDisplay>
<Example1 />
"""
def with_app_only(node_name: str, example_title:str):
return f"""
import Example1 from './examples/EX1/example.md';
import App1 from '!!raw-loader!./examples/EX1/app.json';
### {example_title.strip() if example_title.strip() != "" else f"{node_name} example"}
<AppDisplay
nodeLabel='{node_name}'
appImg={{''}}
outputImg={{''}}
>
{{App1}}
</AppDisplay>
<Example1 />
"""
def get_node_files() -> list[str]:
node_files = []
for root, dirs, files in os.walk("docs/nodes"):
for file in files:
if file.endswith(".md") and AUTOGEN_FOLDER in dirs:
# this is a nodes md file
node_files.append(os.path.join(root, file).replace("\\", "/"))
return node_files
def get_content(file_path: str):
with open(file_path, "r") as f:
content = f.read()
return content
def find_and_extract_content(content: str, start_text: str, end_text: str):
start_index = content.find(start_text)
end_index = content.find(end_text, start_index)
if start_index != -1 and end_index != -1:
extracted_content = content[start_index + len(start_text) : end_index]
return start_index, end_index, extracted_content
else:
return -1, -1, " "
def has_light_mode_img(node_dir: str):
example_dir = os.path.join(node_dir, "examples/EX1")
return all(
os.path.exists(os.path.join(example_dir, img))
for img in ["app_light.jpeg", "output_light.jpeg"]
)
def has_example_image(node_dir: str):
example_dir = os.path.join(node_dir, "examples/EX1")
return all(
os.path.exists(os.path.join(example_dir, img))
for img in ["app.jpeg", "output.jpeg"]
)
def has_example_app(node_dir: str):
example_dir = os.path.join(node_dir, "examples/EX1")
return os.path.exists(os.path.join(example_dir, "app.json"))
def extract_example_title(content: str):
heading_char = "###"
start_index = content.find(heading_char)
if start_index != -1:
return content[
start_index + len(heading_char) : content.find("\n", start_index)
]
return ""
def get_example_section(node_name: str, node_path: str, example_section: str):
node_dir = os.path.dirname(node_path)
has_img = has_example_image(node_dir=node_dir)
has_app = has_example_app(node_dir=node_dir)
example_heading = extract_example_title(example_section)
if has_img:
has_light_mode = has_light_mode_img(node_dir=node_dir)
if has_light_mode:
print(f"{node_name} has light mode images!")
example = with_light_mode(
node_name=node_name, example_title=example_heading
)
return example
else:
print(f"{node_name} does not have light mode images!")
example = without_light_mode(
node_name=node_name, example_title=example_heading
)
return example
elif has_app:
example = with_app_only(node_name=node_name, example_title=example_heading)
return example
return example_section
def write_file(file_path: str, content: str):
with open(file_path, "w") as f:
f.write(content)
def process_md_files():
nodes = get_node_files()
for node in nodes:
md_content = get_content(node)
_, _, ex_section = find_and_extract_content(
md_content, EXAMPLE_SECTION_START, EXAMPLE_SECTION_END
)
node_name = node.split("/")[-1].replace(".md", "")
example_section = get_example_section(
node_name=node_name, node_path=node, example_section=ex_section
)
with_example = md_content.replace(ex_section, example_section)
write_file(node, with_example)
if __name__ == "__main__":
process_md_files()