From 6bb4ca6f1f7e8a024ff78ba6ba9aca0e67eeda1f Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 21 Nov 2024 23:03:23 +0400 Subject: [PATCH] refactor: separate markdown rendering into functions --- src/main.rs | 2 +- src/markdown.rs | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index e5a5916..b611c38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn main() { }; if check_outdated(src, &html) || check_outdated(src, &gmi) { let _ = try_mkdirs(&html); - if let Err(e) = markdown::compile_markdown(src, html, gmi) { + if let Err(e) = markdown::compile_md_file(src, html, gmi) { eprintln!("Could not compile content: {:?}", e); } } diff --git a/src/markdown.rs b/src/markdown.rs index 1d52b36..8a14d85 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -5,16 +5,16 @@ use std::{ use pulldown_cmark::{CowStr, Options, Parser}; -pub fn compile_markdown( +pub fn compile_md_file( src: impl AsRef, html: impl AsRef, gmi: impl AsRef, ) -> std::io::Result<()> { let src_text = std::fs::read_to_string(src)?; - let mut p = ParserVars { - html: Box::new(create_file(html)?), - gmi: Box::new(create_file(gmi)?), + let p = ParserVars { + html: create_file(html)?, + gmi: create_file(gmi)?, state: State::Start, ol: false, @@ -24,7 +24,20 @@ pub fn compile_markdown( notes: vec![], }; - for event in Parser::new_ext(&src_text, Options::all()) { + render_markdown(&src_text, p) +} + +#[inline(always)] +fn create_file(path: impl AsRef) -> std::io::Result>> { + let f = std::fs::OpenOptions::new() + .create(true) + .write(true) + .open(path)?; + Ok(Box::new(BufWriter::new(f))) +} + +fn render_markdown<'md>(src: &'md str, mut p: ParserVars<'md>) -> std::io::Result<()> { + for event in Parser::new_ext(&src, Options::all()) { use pulldown_cmark::Event::*; dbg!(&event); @@ -335,15 +348,6 @@ pub fn compile_markdown( Ok(()) } -#[inline(always)] -fn create_file(path: impl AsRef) -> std::io::Result> { - let f = std::fs::OpenOptions::new() - .create(true) - .write(true) - .open(path)?; - Ok(BufWriter::new(f)) -} - #[inline(always)] fn write_paragraph_start(p: &mut ParserVars) -> std::io::Result<()> { match p.state {