Switch to convert.rs from webp.rs; migrate md to html conversion there.
This commit is contained in:
28
src/convert.rs
Normal file
28
src/convert.rs
Normal 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())
|
||||
}
|
||||
44
src/main.rs
44
src/main.rs
@@ -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");
|
||||
}
|
||||
|
||||
12
src/webp.rs
12
src/webp.rs
@@ -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(())
|
||||
}
|
||||
Reference in New Issue
Block a user