diff --git a/Cargo.lock b/Cargo.lock index 837450a..f757813 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,7 @@ version = "0.1.0" dependencies = [ "chrono", "clap", + "markdown", "ron", "serde", ] @@ -186,6 +187,15 @@ version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" +[[package]] +name = "markdown" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5cab8f2cadc416a82d2e783a1946388b31654d391d1c7d92cc1f03e295b1deb" +dependencies = [ + "unicode-id", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -291,6 +301,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "unicode-id" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ba288e709927c043cbe476718d37be306be53fb1fafecd0dbe36d072be2580" + [[package]] name = "unicode-ident" version = "1.0.19" diff --git a/Cargo.toml b/Cargo.toml index 72c7741..a3d9849 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ license = "MIT" [dependencies] chrono = "0.4" +markdown = "1.0.0" ron = "0.11" [dependencies.clap] diff --git a/src/blog.rs b/src/blog.rs index a895c29..72f18d1 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -1,8 +1,12 @@ use crate::error; + use chrono::NaiveDate; use ron::error::SpannedResult; use serde::{Deserialize, Deserializer, de::DeserializeOwned}; -use std::{fs::File, path::PathBuf}; +use std::{ + fs::File, + path::{Path, PathBuf}, +}; const SANE_DATE_FORMAT: &str = "%Y-%m-%d"; @@ -34,7 +38,7 @@ impl Default for Meta { // Get FALLBACK_META, loading it from FALLBACK_META_PATH if OnceLock not initialized. FALLBACK_META .get_or_init(|| { - MetaFallback::try_from(FALLBACK_META_PATH.get().unwrap().to_owned()) + MetaFallback::try_from(FALLBACK_META_PATH.get().unwrap().as_path()) .expect("Failed to deserialize fallback_meta file") }) .to_owned() @@ -118,26 +122,26 @@ impl TryFrom for MetaFallback { } } -impl TryFrom for Meta { +impl TryFrom<&Path> for Meta { type Error = error::MetaError; - fn try_from(path: PathBuf) -> Result { + fn try_from(path: &Path) -> Result { Self::try_from( std::fs::OpenOptions::new() .read(true) // Just ensure that file opens read-only. - .open(path.as_path())?, + .open(path)?, ) } } -impl TryFrom for MetaFallback { +impl TryFrom<&Path> for MetaFallback { type Error = error::MetaError; - fn try_from(path: PathBuf) -> Result { + fn try_from(path: &Path) -> Result { Self::try_from( std::fs::OpenOptions::new() .read(true) // Just ensure that file opens read-only. - .open(path.as_path())?, + .open(path)?, ) } } diff --git a/src/cli.rs b/src/cli.rs index 7a20235..27ab0e1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,6 +10,9 @@ pub struct Args { #[clap(flatten)] pub single_or_batch: SingleOrBatch, + + #[arg(short, long, value_hint = clap::ValueHint::DirPath)] + pub single_post: Option, } #[derive(Debug, Parser)] diff --git a/src/main.rs b/src/main.rs index 2ec3e47..6b8b9a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use clap::Parser; mod blog; mod cli; mod error; @@ -6,18 +5,45 @@ mod og; use crate::blog::Meta; +use clap::Parser; +use markdown; +use std::io::Read; + fn main() { let config = cli::Args::parse(); + // Fallback Meta Setup blog::FALLBACK_META_PATH.get_or_init(|| config.fallback_meta); + let _fb_meta = Meta::default(); + println!("Loaded FallbackMeta"); + + // Loading meta.ron files process. let _meta = Meta::try_from( config .single_or_batch .single_meta - .expect("No patch for single file processing."), + .expect("No path for single file processing") + .as_path(), ) .expect("Failed to deserialize single_meta file"); - println!("Loaded FallbackMeta"); + // Loading and Converting post.md to html + let mut md_post = std::fs::OpenOptions::new() + .read(true) // Just ensure that file opens read-only. + .open( + config + .single_post + .expect("No path for single post processing"), + ) + .expect("Failed to open single_post file"); + let mut buf = String::new(); + md_post + .read_to_string(&mut buf) + .expect("Failed to read single_post file to string"); + + let html_post = markdown::to_html_with_options(&buf, &markdown::Options::gfm()) + .expect("Failed to convert single_post file to html"); + + dbg!(html_post); } diff --git a/src/og.rs b/src/og.rs index 12a7516..e0e1ae6 100644 --- a/src/og.rs +++ b/src/og.rs @@ -1,6 +1,5 @@ -use std::fmt; - use serde::Deserialize; +use std::fmt; #[derive(Clone, Debug, Deserialize)] pub enum Gender {