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) {
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);
}
}

View file

@ -5,16 +5,16 @@ use std::{
use pulldown_cmark::{CowStr, Options, Parser};
pub fn compile_markdown(
pub fn compile_md_file(
src: impl AsRef<Path>,
html: impl AsRef<Path>,
gmi: impl AsRef<Path>,
) -> 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<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::*;
dbg!(&event);
@ -335,15 +348,6 @@ pub fn compile_markdown(
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)]
fn write_paragraph_start(p: &mut ParserVars) -> std::io::Result<()> {
match p.state {