From 7a0aad81aa8319835e6626ab03d9584c4cc5804b Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 25 Oct 2024 17:02:00 +0400 Subject: [PATCH] feat: try_mkdirs(), test markdown parser --- .gitignore | 1 + src/main.rs | 26 ++++++++++++++----- src/markdown.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/markdown.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..4f96631 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/dist diff --git a/src/main.rs b/src/main.rs index 717a6a5..e5a5916 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod markdown; + use std::{ ffi::OsString, path::{Path, PathBuf}, @@ -29,17 +31,22 @@ fn main() { match src.extension().map(|ext| ext.as_encoded_bytes()) { Some(b"md") => { - let dst = get_dist_path(src, &src_dir, &dst_dir); - if check_outdated(src, dst.with_extension("html")) - || check_outdated(src, dst.with_extension("gmi")) - { - // + let (html, gmi) = { + let dst = get_dist_path(src, &src_dir, &dst_dir); + (dst.with_extension("html"), dst.with_extension("gmi")) + }; + if check_outdated(src, &html) || check_outdated(src, &gmi) { + let _ = try_mkdirs(&html); + if let Err(e) = markdown::compile_markdown(src, html, gmi) { + eprintln!("Could not compile content: {:?}", e); + } } } Some(b"css") => { let dst = get_dist_path(src, &src_dir, &dst_dir); - if check_outdated(src, dst) { + if check_outdated(src, &dst) { + let _ = try_mkdirs(&dst); // } } @@ -49,7 +56,7 @@ fn main() { } } -#[inline(always)] +#[inline] fn get_dist_path( src: impl AsRef, src_dir: impl AsRef, @@ -60,6 +67,11 @@ fn get_dist_path( .join(src.as_ref().strip_prefix(&src_dir).unwrap()) } +#[inline] +fn try_mkdirs(dst: impl AsRef) -> std::io::Result<()> { + std::fs::create_dir_all(dst.as_ref().parent().unwrap_or_else(|| Path::new("/"))) +} + fn check_outdated(src: impl AsRef, dst: impl AsRef) -> bool { let up_to_date = dst .as_ref() diff --git a/src/markdown.rs b/src/markdown.rs new file mode 100644 index 0000000..575064b --- /dev/null +++ b/src/markdown.rs @@ -0,0 +1,69 @@ +use std::{io::BufWriter, path::Path}; + +use pulldown_cmark::{Options, Parser}; + +pub fn compile_markdown( + src: impl AsRef, + html: impl AsRef, + gmi: impl AsRef, +) -> 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!("
"); + } + + Rule => { + println!("------"); + } + + TaskListMarker(state) => { + println!("[{state}] task"); + } + + other => { + eprintln!("Unsupported markdown event: {:?}", other); + } + } + } + + 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)) +}