refactor: separate markdown rendering into functions

This commit is contained in:
DarkCat09 2024-11-21 23:03:23 +04:00
parent 852aabd1b2
commit 6bb4ca6f1f
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82
2 changed files with 19 additions and 15 deletions

View file

@ -37,7 +37,7 @@ fn main() {
}; };
if check_outdated(src, &html) || check_outdated(src, &gmi) { if check_outdated(src, &html) || check_outdated(src, &gmi) {
let _ = try_mkdirs(&html); 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); eprintln!("Could not compile content: {:?}", e);
} }
} }

View file

@ -5,16 +5,16 @@ use std::{
use pulldown_cmark::{CowStr, Options, Parser}; use pulldown_cmark::{CowStr, Options, Parser};
pub fn compile_markdown( pub fn compile_md_file(
src: impl AsRef<Path>, src: impl AsRef<Path>,
html: impl AsRef<Path>, html: impl AsRef<Path>,
gmi: impl AsRef<Path>, gmi: impl AsRef<Path>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let src_text = std::fs::read_to_string(src)?; let src_text = std::fs::read_to_string(src)?;
let mut p = ParserVars { let p = ParserVars {
html: Box::new(create_file(html)?), html: create_file(html)?,
gmi: Box::new(create_file(gmi)?), gmi: create_file(gmi)?,
state: State::Start, state: State::Start,
ol: false, ol: false,
@ -24,7 +24,20 @@ pub fn compile_markdown(
notes: vec![], 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<Path>) -> std::io::Result<Box<BufWriter<std::fs::File>>> {
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::*; use pulldown_cmark::Event::*;
dbg!(&event); dbg!(&event);
@ -335,15 +348,6 @@ pub fn compile_markdown(
Ok(()) Ok(())
} }
#[inline(always)]
fn create_file(path: impl AsRef<Path>) -> std::io::Result<BufWriter<std::fs::File>> {
let f = std::fs::OpenOptions::new()
.create(true)
.write(true)
.open(path)?;
Ok(BufWriter::new(f))
}
#[inline(always)] #[inline(always)]
fn write_paragraph_start(p: &mut ParserVars) -> std::io::Result<()> { fn write_paragraph_start(p: &mut ParserVars) -> std::io::Result<()> {
match p.state { match p.state {