Switch to convert.rs from webp.rs; migrate md to html conversion there.

This commit is contained in:
Olivia Brooks
2025-10-13 20:43:56 -04:00
parent 6f8a8bda96
commit fa9997a650
3 changed files with 46 additions and 38 deletions

28
src/convert.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::io::Read;
use std::path::Path;
use image::ImageReader;
use markdown;
pub fn to_webp(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
ImageReader::open(path)?.decode()?.save(format!(
"{}.webp",
path.file_stem().unwrap().to_str().unwrap()
))?;
Ok(())
}
pub fn md_to_html(path: &Path) -> Result<String, markdown::message::Message> {
let mut buf = String::new();
// Loading and Converting post.md to html
std::fs::OpenOptions::new()
.read(true) // Just ensure that file opens read-only.
.open(path)
.expect("Failed to open single_post file")
.read_to_string(&mut buf)
.expect("Failed to read single_post file to string");
markdown::to_html_with_options(&buf, &markdown::Options::gfm())
}

View File

@@ -1,23 +1,25 @@
mod blog;
mod cli;
mod convert;
mod error;
mod og;
mod webp;
use crate::blog::Meta;
use std::io::Read;
use clap::Parser;
use markdown;
fn main() {
let config = cli::Args::parse();
// Fallback Meta Setup
blog::FALLBACK_META_PATH.get_or_init(|| config.fallback_meta);
load_fb_meta(&config);
let _fb_meta = Meta::default();
println!("Loaded FallbackMeta");
convert::md_to_html(
config
.single_post
.expect("No path for single post processing")
.as_path(),
)
.expect("Failed to convert single_post to html");
// Loading meta.ron files process.
let _meta = Meta::try_from(
@@ -28,23 +30,13 @@ fn main() {
.as_path(),
)
.expect("Failed to deserialize single_meta file");
// 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);
}
/// Fallback Meta Setup
fn load_fb_meta(config: &cli::Args) {
blog::FALLBACK_META_PATH.get_or_init(|| config.fallback_meta.to_owned());
// Trigger OnceLock loading of fallback_meta path.
Meta::default();
println!("Loaded FallbackMeta");
}

View File

@@ -1,12 +0,0 @@
use std::path::Path;
use image::ImageReader;
fn to_webp(path: &Path) -> Result<(), Box<dyn std::error::Error>> {
ImageReader::open(path)?.decode()?.save(format!(
"{}.webp",
path.file_stem().unwrap().to_str().unwrap()
))?;
Ok(())
}