leptos-use/docs/book/src/extract_doc_comment.py

95 lines
2.8 KiB
Python
Raw Normal View History

2023-05-15 01:52:02 +01:00
import sys
2023-05-19 22:28:26 +01:00
import re
2023-05-15 01:52:02 +01:00
def main():
name = sys.argv[1]
file_name = f"../../../../src/{name}.rs"
2023-05-27 03:00:08 +01:00
types = [];
2023-05-15 01:52:02 +01:00
with open(file_name) as f:
in_code_block = False
2023-05-19 00:58:48 +01:00
doc_comment_started = False
2023-05-27 03:00:08 +01:00
initial_doc_finished = False
2023-05-15 01:52:02 +01:00
for line in f.readlines():
2023-05-27 03:00:08 +01:00
if initial_doc_finished:
process_further_line(line, name, types)
elif line.startswith("///"):
2023-05-19 00:58:48 +01:00
doc_comment_started = True
2023-05-15 01:52:02 +01:00
line = line.strip().replace("/// ", "").replace("///", "")
if "```" in line:
if not in_code_block:
line = line.replace("```", "```rust,ignore")
in_code_block = not in_code_block
line = process_line(line, name)
2023-05-15 01:52:02 +01:00
print(line)
2023-05-27 03:00:08 +01:00
2023-05-19 00:58:48 +01:00
elif doc_comment_started:
2023-05-27 03:00:08 +01:00
initial_doc_finished = True
add_types_paragraph(types)
add_source_paragraph(name)
def add_types_paragraph(types):
if types:
print("\n## Types\n")
print("\n".join(types))
def add_source_paragraph(name):
print("\n## Source\n")
source_url = f"https://github.com/Synphonyte/leptos-use/blob/main/src/{name}.rs"
demo_url = f"https://github.com/Synphonyte/leptos-use/tree/main/examples/{name}"
docs_url = f"https://docs.rs/leptos-use/latest/leptos_use/fn.{name}.html"
print(
f"<a href=\"{source_url}\" target=\"_blank\">Source</a> • <a href=\"{demo_url}\" target=\"_blank\">Demo</a> • <a href=\"{docs_url}\" target=\"_blank\">Docs</a>")
2023-05-15 01:52:02 +01:00
2023-05-19 22:28:26 +01:00
interal_doc_link_pattern = re.compile(r"\[`([^]]+)\`](?!\()")
2023-05-27 03:00:08 +01:00
ident_pattern = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*\b")
2023-05-19 22:28:26 +01:00
def process_line(line, name):
stripped = line.strip()
result = line
if stripped.startswith("[Link to Demo](https://"):
example_link = stripped.replace("[Link to Demo](", "").replace(")", "")
result = f'''<div class="demo-container">
<a class="demo-source" href="{example_link}/src/main.rs" target="_blank">source <i class="fa fa-github"></i></a>
<div id="demo-anchor"></div>
</div>'''
2023-05-19 22:28:26 +01:00
else:
result = re.sub(interal_doc_link_pattern,
r"[`\1`](https://docs.rs/leptos-use/latest/leptos_use/fn.\1.html)",
line)
return result
2023-05-27 03:00:08 +01:00
def process_further_line(line, name, types):
if line.startswith("pub enum"):
append_type(line, "enum", types)
elif line.startswith("pub struct"):
append_type(line, "struct", types)
def append_type(line, ty, types):
start_index = len(f"pub {ty} ")
m = re.search(ident_pattern, line[start_index:])
if m is not None:
ident = m.group(0)
types.append(f"- [`{ty} {ident}`](https://docs.rs/leptos-use/latest/leptos_use/{ty}.{ident}.html)")
2023-05-15 01:52:02 +01:00
if __name__ == '__main__':
main()