-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathto_docusaurus.py
More file actions
executable file
·175 lines (126 loc) · 4.48 KB
/
to_docusaurus.py
File metadata and controls
executable file
·175 lines (126 loc) · 4.48 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
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
#!/usr/bin/env python3
"""
Takes the output from Sphinx, clean it and send it to Docusaurus.
1. Get four main modules from _build/html/
- Extract only the 'body' html and store it as a md file under
./website/docs/api-{module_name}.md
2. Get all files under _build/html/api/
- Extract 'body' html and store it as a md file under
./website/docs/api/{filenames}.md
3. Update 'sidebars.json' with the new markdown files
- Update the 'api' section.
- Add each module under a sub-directory.
"""
"""
Takes all relevant html files from the html output sphinx folder, parse it with Beautifulsoup, remove unnecessary html data (such as <head>) and
save a markdown file.
"""
from bs4 import BeautifulSoup
import glob
from pathlib import Path
from typing import List
import re
import json
"""
PARAMETERS
"""
MODULES = ["preprocessing", "nlp", "representation", "visualization"]
ROOT_HTML_DIRECTORY = "./_build/html"
ROOT_MD_DIRECTORY = "../website/docs/"
SIDEBARS_FILEPATH = "../website/sidebars.json"
"""
Helper functions
"""
def get_content(soup):
return soup.find("main").find("div")
def add_docusaurus_metadata(content: str, id: str, title: str, hide_title) -> str:
"""
Add docusaurus metadata into content.
"""
return f"---\nid: {id}\ntitle: {title}\nhide_title: {hide_title}\n---\n\n" + content
def fix_href(soup, module: str):
"""
Fix internal href to be compatible with docusaurus.
"""
for a in soup.find_all("a", {"class": "reference internal"}, href=True):
a["href"] = re.sub("^texthero\.", f"/docs/{module}/", a["href"])
a["href"] = a["href"].lower()
return soup
def to_md(
in_html_filepath: str, out_md_filepath: str, id: str, title: str, hide_title: str
) -> None:
"""
Convert Sphinx-generated html to md.
Parameters
----------
in_html_filepath : str
input html file. Example: ./_build/html/preprocessing.html
out_md_filepath : str
output html file. Example: ../website/docs/preprocessing.md
id : str
Docusaurus document id
title : str
Docusaurus title id
hide_title : str ("true" or "false")
Whether to hide title in Docusaurus.
"""
with open(in_html_filepath, "r") as f:
soup = BeautifulSoup(f.read(), "html.parser")
body = get_content(soup)
with open(out_md_filepath, "w") as f:
content = add_docusaurus_metadata(str(body), id, title, hide_title)
f.write(content)
def get_html(module: str) -> List[str]:
"""Return all html files on the html/module folder"""
files = glob.glob(f"./html/{module}/*.html")
# remove ./html/module
return [f.replace(f"./html/{module}/texthero.", "") for f in files]
def get_prettified_module_name(module_name: str):
"""
Return a prettified version of the module name.
Examples
--------
>>> get_title("preprocessing")
Preprocessing
>>> get_title("nlp")
NLP
"""
module_name = module_name.lower().strip()
if module_name == "nlp":
return "NLP"
else:
return module_name.capitalize()
"""
Update sidebars and markdown files
"""
# make sure folder exists
Path(ROOT_MD_DIRECTORY).mkdir(parents=True, exist_ok=True)
Path(ROOT_MD_DIRECTORY + "api").mkdir(parents=True, exist_ok=True)
api_sidebars = {}
for m in MODULES:
in_html_filename = f"{ROOT_HTML_DIRECTORY}/{m}.html"
out_md_filename = f"{ROOT_MD_DIRECTORY}/api-{m}.md"
id = "api-" + m.lower().strip()
title = get_prettified_module_name(m)
hide_title = "false"
# initialize api_sidebars
api_sidebars[title] = [id]
to_md(in_html_filename, out_md_filename, id, title, hide_title)
for a in glob.glob("./_build/html/api/*.html"):
object_name = a.split("/")[-1].replace(".html", "")
id = object_name
(_, module_name, fun_name) = object_name.split(".")
title = f"{module_name}.{fun_name}"
module_name = get_prettified_module_name(module_name)
hide_title = "true"
api_sidebars[module_name].sort()
api_sidebars[module_name] = api_sidebars[module_name] + ["api/" + id]
in_html_filename = f"{ROOT_HTML_DIRECTORY}/api/{object_name}.html"
out_md_filename = f"{ROOT_MD_DIRECTORY}/api/{object_name}.md"
to_md(in_html_filename, out_md_filename, id, title, hide_title)
# Load, update and save again sidebars.json
with open(SIDEBARS_FILEPATH) as js:
sidebars = json.load(js)
sidebars["api"] = api_sidebars
with open(SIDEBARS_FILEPATH, "w") as f:
json.dump(sidebars, f, indent=2)