-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathhook.py
177 lines (142 loc) · 5.59 KB
/
hook.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
import os
import re
import glob
import git
_INCLUDE_EXAMPLES_REGEX = re.compile(
r"""(?P<_includer_indent>[^\S\r\n]*){\s*%\s*include-examples\s*"(?P<example_name>[^")]+)"\s*%\s*}\s*""",
flags=re.VERBOSE | re.DOTALL,
)
_INCLUDE_EXAMPLE_REGEX = re.compile(
r"""(?P<_includer_indent>[^\S\r\n]*){\s*%\s*include-example\s*"(?P<example_path>[^")]+)"\s*%\s*}\s*""",
flags=re.VERBOSE | re.DOTALL,
)
_LINT_MAP = {
".py": "python",
".json": "json",
".yaml": "yaml",
".yml": "yaml",
".sh": "sh",
".md": "md",
}
_REPO_BASE = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
)
_EXAMPLES_BASE = os.path.abspath(os.path.join(_REPO_BASE, "examples"))
def sub_include_examples(match):
example_name = match.group("example_name")
indents_level0 = match.group("_includer_indent")
lines = []
lines.append(f"{indents_level0}???+ Example\n")
lines.append(f"{indents_level0}\n")
indents_level1 = indents_level0 + " "
for example_type, pretty_name in [("pipeline", "Pipeline"), ("dsl/v2", "DSL")]:
include_path = os.path.join(_EXAMPLES_BASE, example_type, example_name, "*.*")
lines.append(f'{indents_level1}=== "{pretty_name}"\n\n')
indents_level2 = f"{indents_level1} "
for name in glob.glob(include_path):
if name.endswith("README.md") or name.endswith("readme.md"):
lines.append(f"{indents_level2}```markdown\n")
with open(name) as f:
for line in f.readlines():
lines.append(f"{indents_level2}{line}")
lines.append(f"{indents_level2}\n")
lines.append(f"{indents_level2}```\n")
lines.append(f"{indents_level2}\n")
for file_name in glob.glob(include_path):
if file_name.endswith("README.md") or file_name.endswith("readme.md"):
continue
_, file_extension = os.path.splitext(file_name)
lint = _LINT_MAP.get(file_extension, "")
lines.append(
f'{indents_level2}??? Example "{os.path.basename(file_name)}"\n'
)
lines.append(f"{indents_level2} ```{lint}\n")
head = True
with open(file_name) as f:
for line in f.readlines():
# skip license
if head:
if line.strip() == "" or line.lstrip().startswith("#"):
continue
head = False
lines.append(f"{indents_level2} {line}")
lines.append(f"{indents_level2} \n")
lines.append(f"{indents_level2} ```\n")
lines.append(f"{indents_level2} \n")
return "".join(lines)
def sub_include_example(src_file_path):
def sub(match):
example_path = match.group("example_path")
indents_level0 = match.group("_includer_indent")
lines = []
lines.append(f"{indents_level0}\n")
lines.append(f'{indents_level0}??? Example "{example_path}"\n')
lines.append(f"{indents_level0}\n")
indents_level1 = indents_level0 + " "
abs_file_path = os.path.abspath(
os.path.join(src_file_path, os.pardir, example_path)
)
if os.path.exists(abs_file_path):
with open(abs_file_path) as f:
_, file_extension = os.path.splitext(abs_file_path)
lint = _LINT_MAP.get(file_extension, "")
lines.append(f"{indents_level1}```{lint}\n")
head = True
for line in f.readlines():
# skip license
if head:
if line.strip() == "" or line.lstrip().startswith("#"):
continue
head = False
lines.append(f"{indents_level1} {line}")
lines.append(f"{indents_level1}\n")
lines.append(f"{indents_level1}```\n")
lines.append(f"{indents_level1}\n")
return "".join(lines)
return sub
try:
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
url = repo.remote().url
if url.endswith(".git"):
url = url[:-4]
GITHUB_REPO = f"{url}/tree/{sha}"
except BaseException:
GITHUB_REPO = "https://github.com/FederatedAI/FATE/tree/master"
_DIR_URL_REGEX = re.compile(
r"""(?P<text>\[\s*:file_folder:[^\(]*\])\((?P<url>[^\)]+)\)""",
flags=re.VERBOSE | re.DOTALL,
)
def _fix_dir_url(src_path):
def _replace(match):
text = match.group("text")
url = match.group("url")
if not url.startswith("http"):
url_rel_to_repo_base = os.path.relpath(
os.path.abspath(os.path.join(src_path, os.path.pardir, url)), _REPO_BASE
)
url = f"{GITHUB_REPO}/{url_rel_to_repo_base}"
return f"{text}({url})"
return _replace
_COMMENT_REGEX = re.compile(
r"""[^\S\r\n]*<!--\s*mkdocs\s*\n(?P<_content>.*?)-->""",
flags=re.VERBOSE | re.DOTALL,
)
def _remove_comment(match):
content = match.group("_content")
return content
def on_page_markdown(markdown, page, **kwargs):
markdown = re.sub(_DIR_URL_REGEX, _fix_dir_url(page.file.abs_src_path), markdown)
# remove specific commnent
markdown = re.sub(_COMMENT_REGEX, _remove_comment, markdown)
markdown = re.sub(
_INCLUDE_EXAMPLES_REGEX,
sub_include_examples,
markdown,
)
markdown = re.sub(
_INCLUDE_EXAMPLE_REGEX,
sub_include_example(page.file.abs_src_path),
markdown,
)
return markdown