-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathautogen_utils.py
More file actions
143 lines (124 loc) · 3.94 KB
/
autogen_utils.py
File metadata and controls
143 lines (124 loc) · 3.94 KB
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
import re
import string
import markdown
import copy
import pathlib
import os
def save_file(path, content):
parent = pathlib.Path(path).parent
if not os.path.exists(parent):
os.makedirs(parent)
f = open(path, "w", encoding="utf8")
f.write(content)
f.close()
def process_outline_title(title):
title = re.sub(r"`(.*?)`", r"<code>\1</code>", title)
title = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", title)
return title
def turn_title_into_id(title):
title = title.lower()
title = title.replace("&", "amp")
title = title.replace("&", "amp")
title = title.replace("<code>", "")
title = title.replace("</code>", "")
title = title.translate(str.maketrans("", "", string.punctuation))
title = title.replace(" ", "-")
return title
def make_outline(md_source):
lines = md_source.split("\n")
outline = []
in_code_block = False
for line in lines:
if line.startswith("```"):
in_code_block = not in_code_block
if in_code_block:
continue
if line.startswith("# "):
title = line[2:]
title = process_outline_title(title)
outline.append(
{
"title": title,
"url": "#" + turn_title_into_id(title),
"depth": 1,
}
)
if line.startswith("## "):
title = line[3:]
title = process_outline_title(title)
outline.append(
{
"title": title,
"url": "#" + turn_title_into_id(title),
"depth": 2,
}
)
if line.startswith("### "):
title = line[4:]
title = process_outline_title(title)
outline.append(
{
"title": title,
"url": "#" + turn_title_into_id(title),
"depth": 3,
}
)
return outline
def add_copy_buttons_to_code(html_content):
def add_button(match):
full_match = match.group(0)
if 'style="white-space:pre;overflow-x:auto' in full_match:
return full_match
if "┏━━━━━━" in full_match or "Total params:" in full_match:
return full_match
copy_button_html = (
'<div class="code__container">'
'<button class="code__copy--button">'
'<i class="icon--copy"></i>'
'<span class="code__copy--tooltip">Copy</span>'
'</button>'
)
return f'{copy_button_html}{full_match}</div>'
combined_pattern = r'(<div class="k-default-codeblock">.*?</div>)|(<pre[^>]*>.*?</pre>)'
def handle_match(m):
if m.group(1):
return m.group(1)
else:
return add_button(m)
return re.sub(combined_pattern, handle_match, html_content, flags=re.DOTALL)
def render_markdown_to_html(md_content):
html_content = markdown.markdown(
md_content,
extensions=[
"fenced_code",
"tables",
"codehilite",
"mdx_truly_sane_lists",
"smarty",
],
extension_configs={
"codehilite": {
"guess_lang": False,
},
"smarty": {
"smart_dashes": True,
"smart_quotes": False,
"smart_angled_quotes": False,
"smart_ellipses": False,
},
},
)
html_content = add_copy_buttons_to_code(html_content)
return html_content
def set_active_flag_in_nav_entry(entry, relative_url):
entry = copy.copy(entry)
if relative_url.startswith(entry["relative_url"]):
entry["active"] = True
else:
entry["active"] = False
children = [
set_active_flag_in_nav_entry(child, relative_url)
for child in entry.get("children", [])
]
entry["children"] = children
return entry