forked from vizzuhq/ipyvizzu-story
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_files.py
157 lines (116 loc) · 4.37 KB
/
gen_files.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
"""Generate the code reference pages and navigation."""
# pylint: disable=too-few-public-methods
from pathlib import Path
from typing import Union, List
import re
import yaml
import mkdocs_gen_files # type: ignore
class MkdocsConfig:
"""A class for working with mkdocs configuration."""
@staticmethod
def load() -> dict:
"""
A method for loading mkdocs configuration.
Returns:
A dictionary that contains the mkdocs configuration.
"""
with open(Path(__file__).parent / "mkdocs.yml", "rt", encoding="utf8") as f_yml:
return yaml.load(f_yml, Loader=yaml.FullLoader)
class Index:
"""A class for creating index file from README."""
@staticmethod
def generate(readme: Path, site: str, ipynbs: List[str]) -> None:
"""
A method for generating the index file.
Args:
readme: README.md path.
site: Site url.
ipynbs: List of html links that are ipynb files.
"""
with open(readme, "rt", encoding="utf8") as f_readme:
content = f_readme.read()
for match in re.finditer(
rf"\[([^]]*)\]\(({site}/)([^]]*)(.html)([^]]*)?\)",
content,
):
if match[0] in ipynbs:
content = content.replace(
match[0], f"[{match[1]}]({match[3]}.ipynb{match[5]})"
)
else:
content = content.replace(
match[0], f"[{match[1]}]({match[3]}.md{match[5]})"
)
content = content.replace(f"{site}/", "")
with mkdocs_gen_files.open("index.md", "w") as f_index:
f_index.write(content)
class SectionIndex:
"""A class for creating section index files."""
@staticmethod
def _write_index_file(file: str, toc: list) -> None:
"""
A method for writing table of contents into a section index file.
Args:
file: The section index file.
toc: Items of the table of contents.
"""
with mkdocs_gen_files.open(file, "w") as f_index:
for item in toc:
for key in item:
link = Path(item[key]).relative_to(Path(file).parent)
f_index.write(f"* [{key}]({link})\n")
@staticmethod
def generate(nav_item: Union[list, dict, str]) -> None:
"""
A method for creating section indices for the navigation.
Args:
nav_item: Part of the navigation.
"""
if isinstance(nav_item, list):
if (
nav_item
and isinstance(nav_item[0], str)
and nav_item[0].endswith("index.md")
):
SectionIndex._write_index_file(file=nav_item[0], toc=nav_item[1:])
for item in nav_item:
SectionIndex.generate(nav_item=item)
elif isinstance(nav_item, dict):
for key in nav_item:
SectionIndex.generate(nav_item=nav_item[key])
class Api:
"""A class for creating api code reference."""
@staticmethod
def generate(folder: str) -> None:
"""
A method for generate api code reference.
Args:
folder: API destination folder.
"""
for path in sorted(Path("src").rglob("*.py")):
module_path = path.relative_to("src").with_suffix("")
doc_path = path.relative_to("src").with_suffix(".md")
full_doc_path = Path(folder, doc_path)
parts = tuple(module_path.parts)
if parts[-1] == "__init__":
parts = parts[:-1]
doc_path = doc_path.with_name("index.md")
full_doc_path = full_doc_path.with_name("index.md")
elif parts[-1] == "__main__":
continue
with mkdocs_gen_files.open(full_doc_path, "w") as f_md:
item = ".".join(parts)
f_md.write(f"::: {item}")
mkdocs_gen_files.set_edit_path(full_doc_path, path)
config = MkdocsConfig.load()
site_url = config["site_url"]
if site_url.endswith("/"):
site_url = site_url[:-1]
index_ipynbs = [f"[HTML]({site_url}/examples/complex/complex.html)"]
SectionIndex.generate(nav_item=config["nav"])
Api.generate("api")
Index.generate(
readme=Path(__file__).parent / ".." / ".." / "README.md",
site=site_url,
ipynbs=index_ipynbs,
)