feat: try_mkdirs(), test markdown parser
This commit is contained in:
parent
97ea2762b2
commit
7a0aad81aa
3 changed files with 89 additions and 7 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/target
|
||||
/dist
|
||||
|
|
26
src/main.rs
26
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<Path>,
|
||||
src_dir: impl AsRef<Path>,
|
||||
|
@ -60,6 +67,11 @@ fn get_dist_path(
|
|||
.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 {
|
||||
let up_to_date = dst
|
||||
.as_ref()
|
||||
|
|
69
src/markdown.rs
Normal file
69
src/markdown.rs
Normal 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))
|
||||
}
|
Loading…
Add table
Reference in a new issue