feat: try_mkdirs(), test markdown parser

This commit is contained in:
DarkCat09 2024-10-25 17:02:00 +04:00
parent 97ea2762b2
commit 7a0aad81aa
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82
3 changed files with 89 additions and 7 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
/dist

View file

@ -1,3 +1,5 @@
mod markdown;
use std::{ use std::{
ffi::OsString, ffi::OsString,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -29,17 +31,22 @@ fn main() {
match src.extension().map(|ext| ext.as_encoded_bytes()) { match src.extension().map(|ext| ext.as_encoded_bytes()) {
Some(b"md") => { Some(b"md") => {
let (html, gmi) = {
let dst = get_dist_path(src, &src_dir, &dst_dir); let dst = get_dist_path(src, &src_dir, &dst_dir);
if check_outdated(src, dst.with_extension("html")) (dst.with_extension("html"), dst.with_extension("gmi"))
|| check_outdated(src, 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") => { Some(b"css") => {
let dst = get_dist_path(src, &src_dir, &dst_dir); 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( fn get_dist_path(
src: impl AsRef<Path>, src: impl AsRef<Path>,
src_dir: impl AsRef<Path>, src_dir: impl AsRef<Path>,
@ -60,6 +67,11 @@ fn get_dist_path(
.join(src.as_ref().strip_prefix(&src_dir).unwrap()) .join(src.as_ref().strip_prefix(&src_dir).unwrap())
} }
#[inline]
fn try_mkdirs(dst: impl AsRef<Path>) -> std::io::Result<()> {
std::fs::create_dir_all(dst.as_ref().parent().unwrap_or_else(|| Path::new("/")))
}
fn check_outdated(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> bool { fn check_outdated(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> bool {
let up_to_date = dst let up_to_date = dst
.as_ref() .as_ref()

69
src/markdown.rs Normal file
View file

@ -0,0 +1,69 @@
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))
}