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

42 lines
1.2 KiB
Python
Raw Normal View History

2023-05-15 01:52:02 +01:00
import sys
def main():
name = sys.argv[1]
file_name = f"../../../../src/{name}.rs"
with open(file_name) as f:
in_code_block = False
2023-05-19 00:58:48 +01:00
doc_comment_started = False
2023-05-15 01:52:02 +01:00
for line in f.readlines():
if 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-19 00:58:48 +01:00
elif doc_comment_started:
return
2023-05-15 01:52:02 +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</a>
<iframe class="demo" src="{name}/demo/index.html" width="100%" frameborder="0">
</iframe>
</div>'''
return result
2023-05-15 01:52:02 +01:00
if __name__ == '__main__':
main()