Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keras-team
GitHub Repository: keras-team/keras-io
Path: blob/master/scripts/autogen_utils.py
7921 views
1
import re
2
import string
3
import markdown
4
import copy
5
import pathlib
6
import os
7
8
import pasta
9
from pasta.base import scope
10
11
12
def save_file(path, content):
13
parent = pathlib.Path(path).parent
14
if not os.path.exists(parent):
15
os.makedirs(parent)
16
f = open(path, "w", encoding="utf8")
17
f.write(content)
18
f.close()
19
20
21
def process_outline_title(title):
22
title = re.sub(r"`(.*?)`", r"<code>\1</code>", title)
23
title = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", title)
24
return title
25
26
27
def turn_title_into_id(title):
28
title = title.lower()
29
title = title.replace("&amp", "amp")
30
title = title.replace("&", "amp")
31
title = title.replace("<code>", "")
32
title = title.replace("</code>", "")
33
title = title.translate(str.maketrans("", "", string.punctuation))
34
title = title.replace(" ", "-")
35
return title
36
37
38
def make_outline(md_source):
39
lines = md_source.split("\n")
40
outline = []
41
in_code_block = False
42
for line in lines:
43
if line.startswith("```"):
44
in_code_block = not in_code_block
45
if in_code_block:
46
continue
47
if line.startswith("# "):
48
title = line[2:]
49
title = process_outline_title(title)
50
outline.append(
51
{
52
"title": title,
53
"url": "#" + turn_title_into_id(title),
54
"depth": 1,
55
}
56
)
57
if line.startswith("## "):
58
title = line[3:]
59
title = process_outline_title(title)
60
outline.append(
61
{
62
"title": title,
63
"url": "#" + turn_title_into_id(title),
64
"depth": 2,
65
}
66
)
67
if line.startswith("### "):
68
title = line[4:]
69
title = process_outline_title(title)
70
outline.append(
71
{
72
"title": title,
73
"url": "#" + turn_title_into_id(title),
74
"depth": 3,
75
}
76
)
77
return outline
78
79
80
def render_markdown_to_html(md_content):
81
return markdown.markdown(
82
md_content,
83
extensions=[
84
"fenced_code",
85
"tables",
86
"codehilite",
87
"mdx_truly_sane_lists",
88
"smarty",
89
"pymdownx.arithmatex",
90
],
91
extension_configs={
92
"codehilite": {
93
"guess_lang": False,
94
},
95
"smarty": {
96
"smart_dashes": True,
97
"smart_quotes": False,
98
"smart_angled_quotes": False,
99
"smart_ellipses": False,
100
},
101
"pymdownx.arithmatex": {
102
"generic": True,
103
},
104
},
105
)
106
107
108
def set_active_flag_in_nav_entry(entry, relative_url):
109
entry = copy.copy(entry)
110
if relative_url.startswith(entry["relative_url"]):
111
entry["active"] = True
112
else:
113
entry["active"] = False
114
children = [
115
set_active_flag_in_nav_entry(child, relative_url)
116
for child in entry.get("children", [])
117
]
118
entry["children"] = children
119
return entry
120
121
122
def find_all_symbols(path, packages):
123
"""Parse a Python file and find all symbols from specific packages.
124
125
Args:
126
path: the path to the Python file
127
packages: collection of Python package, for instance ["keras"]
128
129
Returns:
130
a collection of symbols found in the Python code, for instance
131
["keras", "keras.layers", "keras.layers.Dense"]
132
"""
133
with open(path, "r") as src_file:
134
src = src_file.read()
135
136
try:
137
tree = pasta.parse(src)
138
except Exception:
139
return None
140
s = scope.analyze(tree)
141
symbols = set()
142
143
def explore(path, attrs):
144
symbols.add(path)
145
for name, attr in attrs.items():
146
new_path = path + "." + name
147
explore(new_path, attr.attrs)
148
149
for package in packages:
150
refs = s.external_references.get(package, [])
151
for ref in refs:
152
if ref.name_ref is not None:
153
explore(package, ref.name_ref.attrs)
154
155
return symbols
156
157