dc09rs/src/markdown.rs

70 lines
1.5 KiB
Rust
Raw Normal View History

use std::{io::BufWriter, path::Path};
use pulldown_cmark::{Options, Parser};
pub fn compile_markdown(
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 html = create_file(html)?;
let gmi = create_file(gmi)?;
for event in Parser::new_ext(&src_text, Options::all()) {
use pulldown_cmark::Event::*;
match event {
Start(tag) => {
use pulldown_cmark::Tag::*;
println!("{tag:?}");
}
End(tag) => {
use pulldown_cmark::Tag::*;
println!("{tag:?}");
}
Text(text) => {
println!("!{text}!");
}
Code(code) => {
println!("`{code}`");
}
SoftBreak => {}
HardBreak => {
println!("<br>");
}
Rule => {
println!("------");
}
TaskListMarker(state) => {
println!("[{state}] task");
}
other => {
eprintln!("Unsupported markdown event: {:?}", other);
}
}
}
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))
}