Figure out markdown to html conversion.

This commit is contained in:
Olivia Brooks
2025-10-13 19:40:10 -04:00
parent bc3eb9d77a
commit 7a7ba5d21b
6 changed files with 62 additions and 13 deletions

16
Cargo.lock generated
View File

@@ -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"

View File

@@ -6,6 +6,7 @@ license = "MIT"
[dependencies]
chrono = "0.4"
markdown = "1.0.0"
ron = "0.11"
[dependencies.clap]

View File

@@ -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<File> for MetaFallback {
}
}
impl TryFrom<PathBuf> for Meta {
impl TryFrom<&Path> for Meta {
type Error = error::MetaError;
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
fn try_from(path: &Path) -> Result<Self, Self::Error> {
Self::try_from(
std::fs::OpenOptions::new()
.read(true) // Just ensure that file opens read-only.
.open(path.as_path())?,
.open(path)?,
)
}
}
impl TryFrom<PathBuf> for MetaFallback {
impl TryFrom<&Path> for MetaFallback {
type Error = error::MetaError;
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
fn try_from(path: &Path) -> Result<Self, Self::Error> {
Self::try_from(
std::fs::OpenOptions::new()
.read(true) // Just ensure that file opens read-only.
.open(path.as_path())?,
.open(path)?,
)
}
}

View File

@@ -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<PathBuf>,
}
#[derive(Debug, Parser)]

View File

@@ -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);
}

View File

@@ -1,6 +1,5 @@
use std::fmt;
use serde::Deserialize;
use std::fmt;
#[derive(Clone, Debug, Deserialize)]
pub enum Gender {