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