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

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 {