2023-05-19 20:43:03 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
2023-06-10 03:19:00 +01:00
|
|
|
import sys
|
2023-09-30 18:45:36 +01:00
|
|
|
import re
|
2023-05-19 20:43:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
src_dir = os.path.join(os.getcwd(), "src")
|
|
|
|
for dir in os.listdir(src_dir):
|
|
|
|
category = dir
|
|
|
|
category_dir = os.path.join(src_dir, dir)
|
|
|
|
if os.path.isdir(category_dir):
|
|
|
|
for file in os.listdir(category_dir):
|
2023-06-10 03:19:00 +01:00
|
|
|
if file.endswith(".md") and (len(sys.argv) == 1 or (sys.argv[1] in file)):
|
2023-05-19 20:43:03 +01:00
|
|
|
build_and_copy_demo(category, file)
|
2024-07-27 18:10:38 +02:00
|
|
|
rewrite_links(category, file)
|
2023-05-19 20:43:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def build_and_copy_demo(category, md_name):
|
|
|
|
name = md_name[:-3]
|
|
|
|
example_dir = f"../../examples/{name}"
|
|
|
|
if os.path.exists(example_dir):
|
2023-06-26 13:08:51 +01:00
|
|
|
p = subprocess.Popen(["trunk", "build", "--release"], cwd=example_dir)
|
2023-07-03 16:10:58 +01:00
|
|
|
code = p.wait()
|
|
|
|
|
|
|
|
if code != 0:
|
2024-07-27 18:10:38 +02:00
|
|
|
sys.stderr.write(f"failed to build example '{name}'\n")
|
|
|
|
sys.exit(code)
|
2023-05-19 23:44:50 +01:00
|
|
|
|
|
|
|
example_output_path = os.path.join(example_dir, "dist")
|
|
|
|
target_path = os.path.join("book", category, name, "demo")
|
|
|
|
|
2023-05-27 03:00:08 +01:00
|
|
|
print(f"Copying demo from {example_output_path} -> {target_path}")
|
2023-05-19 23:44:50 +01:00
|
|
|
|
|
|
|
shutil.copytree(example_output_path, target_path,
|
2023-05-19 20:43:03 +01:00
|
|
|
dirs_exist_ok=True)
|
|
|
|
|
2023-05-26 18:09:01 +01:00
|
|
|
with open(os.path.join(target_path, "index.html"), "r") as f:
|
2023-05-28 17:42:16 +01:00
|
|
|
html = f.read().replace("/demo", f"./{name}/demo")
|
|
|
|
demo_head = html.split("<head>")[1].split("</head>")[0]
|
|
|
|
demo_body = html.split("<body>")[1].split("</body>")[0]
|
2023-05-26 18:09:01 +01:00
|
|
|
|
|
|
|
book_html_path = os.path.join("book", category, f"{name}.html")
|
|
|
|
with open(book_html_path, "r") as f:
|
|
|
|
html = f.read()
|
|
|
|
head_split = html.split("<head>")
|
|
|
|
target_head = head_split[1].split("</head>")[0]
|
2023-09-30 18:45:36 +01:00
|
|
|
body_split = re.split("<body[^>]*>", html)[1].split("</body>")
|
2023-05-26 18:09:01 +01:00
|
|
|
target_body = body_split[0]
|
|
|
|
|
|
|
|
with open(book_html_path, "w") as f:
|
|
|
|
f.write(
|
|
|
|
f"""{head_split[0]}
|
|
|
|
<head>
|
2023-05-28 17:42:16 +01:00
|
|
|
{demo_head}
|
2023-05-26 18:09:01 +01:00
|
|
|
{target_head}
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-05-28 17:42:16 +01:00
|
|
|
{demo_body}
|
2023-05-26 18:09:01 +01:00
|
|
|
{target_body}
|
|
|
|
</body>
|
|
|
|
{body_split[1]}""")
|
|
|
|
|
2023-05-19 20:43:03 +01:00
|
|
|
|
2024-07-27 18:10:38 +02:00
|
|
|
def rewrite_links(category, md_name):
|
|
|
|
"""Rewrite links in generated documentation to make them
|
|
|
|
compatible between rustdoc and the book.
|
|
|
|
"""
|
|
|
|
html_name = f"{md_name[:-3]}.html"
|
|
|
|
target_path = os.path.join("book", category, html_name)
|
|
|
|
|
|
|
|
with open(target_path, "r") as f:
|
|
|
|
html = f.read()
|
|
|
|
|
|
|
|
html = html.replace(
|
|
|
|
"fn@crate::", "",
|
|
|
|
).replace(
|
|
|
|
"crate::", "",
|
|
|
|
).replace(
|
|
|
|
"fn@", "",
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(target_path, "w") as f:
|
|
|
|
f.write(html)
|
|
|
|
|
|
|
|
|
2023-05-19 20:43:03 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|