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

153 lines
4.3 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-29 01:52:03 +01:00
import os
2023-05-15 01:52:02 +01:00
def main():
2023-05-29 01:52:03 +01:00
category = os.path.split(os.getcwd())[-1]
2023-06-03 05:25:45 +01:00
name = sys.argv[1]
file_name = f"../../../../src/{name}.rs"
module = None
if "/" in name:
module, name = name.split("/")
if module is not None:
module_display = f"leptos_use::{module}"
else:
module_display = "leptos_use"
feature = sys.argv[2] if len(sys.argv) > 2 else None
feature_display = ""
if feature is not None:
feature_display = f"<div>Feature</div><div>{feature}</div>"
2023-05-29 01:52:03 +01:00
print(f"""
<div class="meta-data">
<div>Category</div>
<div>{category.title()}</div>
2023-06-03 05:25:45 +01:00
<div>Module</div>
<div><code>{module_display}</code></div>
{feature_display}
2023-05-29 01:52:03 +01:00
</div>
2023-06-03 05:25:45 +01:00
""")
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-06-03 14:58:30 +01:00
stripped_line = line.strip()
2023-05-27 03:00:08 +01:00
if initial_doc_finished:
2023-06-03 05:25:45 +01:00
process_further_line(line, types, module)
2023-05-27 03:00:08 +01:00
2023-06-03 14:58:30 +01:00
elif stripped_line.startswith("///") or stripped_line.startswith("#[doc ="):
2023-05-19 00:58:48 +01:00
doc_comment_started = True
2023-06-03 14:58:30 +01:00
if line.startswith("#[doc"):
line = stripped_line.replace("#[doc = \" ", "").replace("#[doc = \"", "")[:-2]
else:
line = stripped_line.replace("/// ", "").replace("///", "")
2023-05-15 01:52:02 +01:00
if "```" in line:
if not in_code_block:
line = line.replace("```", "```rust,ignore")
in_code_block = not in_code_block
2023-06-03 05:25:45 +01:00
line = process_line(line, name, module)
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
2023-06-03 14:58:30 +01:00
if feature is not None:
append_feature_paragraph(feature)
2023-05-27 03:00:08 +01:00
add_types_paragraph(types)
2023-06-03 05:25:45 +01:00
add_source_paragraph(name, module)
2023-05-27 03:00:08 +01:00
def add_types_paragraph(types):
if types:
print("\n## Types\n")
print("\n".join(types))
2023-06-03 05:25:45 +01:00
def add_source_paragraph(name, module):
2023-05-27 03:00:08 +01:00
print("\n## Source\n")
2023-06-03 05:25:45 +01:00
if module is not None:
module = f"/{module}"
source_url = f"https://github.com/Synphonyte/leptos-use/blob/main/src{module}/{name}.rs"
2023-05-27 03:00:08 +01:00
demo_url = f"https://github.com/Synphonyte/leptos-use/tree/main/examples/{name}"
2023-06-03 05:25:45 +01:00
docs_url = f"https://docs.rs/leptos-use/latest/leptos_use{module}/fn.{name}.html"
2023-05-27 03:00:08 +01:00
2023-05-29 01:52:03 +01:00
demo_link = " • <a href=\"{demo_url}\" target=\"_blank\">Demo</a>" if os.path.isdir(
os.path.join("..", "..", "..", "..", "examples", name)) else ""
2023-05-27 03:00:08 +01:00
print(
2023-05-29 01:52:03 +01:00
f"<a href=\"{source_url}\" target=\"_blank\">Source</a>{demo_link} • <a href=\"{docs_url}\" target=\"_blank\">Docs</a>")
2023-05-15 01:52:02 +01:00
2023-06-03 05:25:45 +01:00
internal_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
2023-06-03 05:25:45 +01:00
def process_line(line, name, module):
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:
2023-06-03 05:25:45 +01:00
if module is not None:
module = f"/{module}"
result = re.sub(internal_doc_link_pattern,
rf"[`\1`](https://docs.rs/leptos-use/latest/leptos_use{module}/fn.\1.html)",
2023-05-19 22:28:26 +01:00
line)
return result
2023-06-03 05:25:45 +01:00
def process_further_line(line, types, module=None):
2023-05-27 03:00:08 +01:00
if line.startswith("pub enum"):
2023-06-03 05:25:45 +01:00
append_type(line, "enum", types, module)
2023-05-27 03:00:08 +01:00
elif line.startswith("pub struct"):
2023-06-03 05:25:45 +01:00
append_type(line, "struct", types, module)
2023-05-27 03:00:08 +01:00
2023-06-03 14:58:30 +01:00
def append_feature_paragraph(feature):
print(f"""## Feature
> This function is only available if the crate feature **`{feature}`** is enabled""")
2023-06-03 05:25:45 +01:00
def append_type(line, ty, types, module=None):
2023-05-27 03:00:08 +01:00
start_index = len(f"pub {ty} ")
m = re.search(ident_pattern, line[start_index:])
if m is not None:
ident = m.group(0)
2023-06-03 05:25:45 +01:00
if module is not None:
module = f"/{module}"
else:
module = ""
types.append(f"- [`{ty} {ident}`](https://docs.rs/leptos-use/latest/leptos_use{module}/{ty}.{ident}.html)")
2023-05-27 03:00:08 +01:00
2023-05-15 01:52:02 +01:00
if __name__ == '__main__':
main()