python_to_mermaid/
lib.rs

1use std::fmt::Write as _;
2
3use anyhow::Result;
4
5use convert::{flowchart_to_mermaid, python_to_flowchart};
6
7pub mod convert;
8pub mod flowchart;
9pub mod mermaid;
10
11pub fn python_file_to_markdown(source: &str) -> Result<String> {
12    let fn_defs = python_to_flowchart::enumerate_fn_defs(source)?;
13
14    let mut buf = String::new();
15
16    for (i, fn_def) in fn_defs.into_iter().enumerate() {
17        let name = fn_def.name.clone();
18
19        let fc = python_to_flowchart::convert(fn_def)?;
20        let mfc = flowchart_to_mermaid::convert(&fc);
21
22        if i > 0 {
23            // Add a newline between flowcharts
24            writeln!(buf)?;
25        }
26
27        writeln!(buf, "## `{name}`")?;
28        writeln!(buf)?;
29        writeln!(buf, "```mermaid")?;
30        let mut s = String::new();
31        mfc.render(&mut s);
32        write!(buf, "{}", s)?;
33        writeln!(buf, "```")?;
34    }
35
36    Ok(buf)
37}