From 852aabd1b213b5ed5fc2b03fd2a90cb270cd0fbd Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 21 Nov 2024 22:43:08 +0400 Subject: [PATCH] refactor: move all state to struct, add footnotes handling --- src/markdown.rs | 327 ++++++++++++++++++++++++++++-------------------- 1 file changed, 194 insertions(+), 133 deletions(-) diff --git a/src/markdown.rs b/src/markdown.rs index 0adafc3..1d52b36 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -12,13 +12,17 @@ pub fn compile_markdown( ) -> std::io::Result<()> { let src_text = std::fs::read_to_string(src)?; - let mut html = create_file(html)?; - let mut gmi = create_file(gmi)?; + let mut p = ParserVars { + html: Box::new(create_file(html)?), + gmi: Box::new(create_file(gmi)?), - let mut state = State::Start; - let mut ol = false; // ordered list - let mut counter: u64 = 0; - let mut links: Vec> = vec![]; + state: State::Start, + ol: false, + counter: 0, + + links: vec![], + notes: vec![], + }; for event in Parser::new_ext(&src_text, Options::all()) { use pulldown_cmark::Event::*; @@ -31,143 +35,145 @@ pub fn compile_markdown( match tag { Paragraph => { - html.write_all(b"

")?; - write_paragraph_start(&mut gmi, state)?; + p.html.write_all(b"

")?; + write_paragraph_start(&mut p)?; } Heading { level, id, .. } => { if let Some(id) = id { - html.write_fmt(format_args!("<{} id=\"{}\">", level, id))?; + p.html + .write_fmt(format_args!("<{} id=\"{}\">", level, id))?; } else { - html.write_fmt(format_args!("<{}>", level))?; + p.html.write_fmt(format_args!("<{}>", level))?; } - write_paragraph_start(&mut gmi, state)?; + write_paragraph_start(&mut p)?; let hashes = match level { HeadingLevel::H1 => "# ", HeadingLevel::H2 => "## ", _ => "### ", }; - gmi.write_all(hashes.as_bytes())?; - state = State::Paragraph; + p.gmi.write_all(hashes.as_bytes())?; + p.state = State::Paragraph; } BlockQuote(_kind) => { - html.write_all(b"

")?; + p.html.write_all(b"
")?; - write_paragraph_start(&mut gmi, state)?; - gmi.write_all(b"> ")?; - state = State::Quote; + write_paragraph_start(&mut p)?; + p.gmi.write_all(b"> ")?; + p.state = State::Quote; } CodeBlock(CodeBlockKind::Fenced(lang)) => { // TODO: highlighting with syntect - html.write_all(b"
")?;
+                        p.html.write_all(b"
")?;
 
-                        write_paragraph_start(&mut gmi, state)?;
-                        gmi.write_fmt(format_args!("```{}\r\n", lang))?;
+                        write_paragraph_start(&mut p)?;
+                        p.gmi.write_fmt(format_args!("```{}\r\n", lang))?;
                     }
 
                     CodeBlock(CodeBlockKind::Indented) => {
-                        html.write_all(b"
")?;
+                        p.html.write_all(b"
")?;
 
-                        write_paragraph_start(&mut gmi, state)?;
-                        gmi.write_all(b"```")?;
+                        write_paragraph_start(&mut p)?;
+                        p.gmi.write_all(b"```")?;
                     }
 
                     List(None) => {
-                        html.write_all(b"