Inline Images (Custom Emojis)
All checks were successful
run check / cargo check (push) Successful in 4m2s
All checks were successful
run check / cargo check (push) Successful in 4m2s
This commit is contained in:
parent
b2354530c2
commit
b9cad3c710
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,4 +2,5 @@
|
||||||
/templates
|
/templates
|
||||||
/output
|
/output
|
||||||
/md_src
|
/md_src
|
||||||
|
/emojis
|
||||||
mlem.toml
|
mlem.toml
|
||||||
|
|
|
@ -6,6 +6,8 @@ pub struct Config {
|
||||||
pub output_dir: String,
|
pub output_dir: String,
|
||||||
pub src_dir: String,
|
pub src_dir: String,
|
||||||
pub templates_dir: String,
|
pub templates_dir: String,
|
||||||
|
pub emoji_web_directory: String,
|
||||||
|
pub emoji_local_directory: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_config() -> Config {
|
pub fn read_config() -> Config {
|
||||||
|
|
41
src/emoji.rs
Normal file
41
src/emoji.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use regex::Regex;
|
||||||
|
use std::{ffi::OsString, fs::read_dir};
|
||||||
|
pub fn emoji_pass(
|
||||||
|
markdown: &str,
|
||||||
|
emoji_web_directory: &String,
|
||||||
|
emoji_local_directory: &String,
|
||||||
|
) -> String {
|
||||||
|
let mut markdown = markdown.to_owned();
|
||||||
|
let re_emojis = Regex::new(r":\w+:").unwrap();
|
||||||
|
|
||||||
|
for emoji in re_emojis.find_iter(&markdown.clone()) {
|
||||||
|
let emoji_file_name =
|
||||||
|
get_emoji_file_name(&get_emoji_name(emoji.as_str()), emoji_local_directory);
|
||||||
|
if emoji_file_name.is_none() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let html_string = format!(
|
||||||
|
"<img class=\"emoji\" src=\"{emoji_web_directory}/{}\"><img>",
|
||||||
|
emoji_file_name.unwrap().to_str().unwrap()
|
||||||
|
);
|
||||||
|
markdown = markdown.replacen(emoji.as_str(), &html_string, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
markdown
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_emoji_name(emoji_match: &str) -> String {
|
||||||
|
emoji_match.replace(':', "")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_emoji_file_name(emoji_name: &String, emoji_local_directory: &String) -> Option<OsString> {
|
||||||
|
let all_emojis = read_dir(emoji_local_directory).expect("Failed to read emoji dir");
|
||||||
|
for e in all_emojis {
|
||||||
|
let e = e.unwrap();
|
||||||
|
if OsString::from(emoji_name) == e.path().file_stem().unwrap() {
|
||||||
|
return Some(e.file_name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eprintln!("WARNING: No emoji found for {emoji_name} did you add it to your emoji dir?");
|
||||||
|
None
|
||||||
|
}
|
12
src/lib.rs
12
src/lib.rs
|
@ -4,7 +4,7 @@ use markdown::{to_html_with_options, CompileOptions, Options};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fs::{read_dir, read_to_string, self};
|
use std::fs::{self, read_dir, read_to_string};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
|
@ -18,7 +18,8 @@ pub fn read_src_files(src_dir: &str) -> Vec<SrcMD> {
|
||||||
files.push(SrcMD::new(
|
files.push(SrcMD::new(
|
||||||
file.path(),
|
file.path(),
|
||||||
title.to_string(),
|
title.to_string(),
|
||||||
DateTime::parse_from_str(&format!("{date} 00:00:00 +00:00"), "%Y-%m-%d %H:%M:%S %z").unwrap(),
|
DateTime::parse_from_str(&format!("{date} 00:00:00 +00:00"), "%Y-%m-%d %H:%M:%S %z")
|
||||||
|
.unwrap(),
|
||||||
file.path().file_stem().unwrap().to_os_string(),
|
file.path().file_stem().unwrap().to_os_string(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -26,8 +27,9 @@ pub fn read_src_files(src_dir: &str) -> Vec<SrcMD> {
|
||||||
files
|
files
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_to_fs(html: String,output_dir: &String,file_name: &String){
|
pub fn write_to_fs(html: String, output_dir: &String, file_name: &String) {
|
||||||
fs::write(format!("{output_dir}/{file_name}.html"), html).unwrap_or_else(|_| panic!("Error writing {file_name}"));
|
fs::write(format!("{output_dir}/{file_name}.html"), html)
|
||||||
|
.unwrap_or_else(|_| panic!("Error writing {file_name}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Constructor)]
|
#[derive(Constructor)]
|
||||||
|
@ -38,7 +40,7 @@ pub struct SrcMD {
|
||||||
pub file_name: OsString,
|
pub file_name: OsString,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_blog_entry(markdown: String,template_dir: &String) -> String {
|
pub fn generate_blog_entry(markdown: String, template_dir: &String) -> String {
|
||||||
let markdown = markdown.clone();
|
let markdown = markdown.clone();
|
||||||
|
|
||||||
let mut tera = Tera::new(&format!("{template_dir}/*")).unwrap();
|
let mut tera = Tera::new(&format!("{template_dir}/*")).unwrap();
|
||||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -1,14 +1,23 @@
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod emoji;
|
||||||
use mlem::*;
|
use mlem::*;
|
||||||
fn main() {
|
fn main() {
|
||||||
let config = config::read_config();
|
let config = config::read_config();
|
||||||
|
|
||||||
let raw_files = read_src_files(&config.src_dir);
|
let raw_files = read_src_files(&config.src_dir);
|
||||||
for file in raw_files {
|
for file in raw_files {
|
||||||
let markdown = read_to_string(file.path).expect("File does not exist");
|
let mut markdown = read_to_string(file.path).expect("File does not exist");
|
||||||
let html = generate_blog_entry(markdown,&config.templates_dir);
|
markdown = emoji::emoji_pass(
|
||||||
write_to_fs(html, &config.output_dir, &file.file_name.into_string().unwrap());
|
&markdown,
|
||||||
|
&config.emoji_web_directory,
|
||||||
|
&config.emoji_local_directory,
|
||||||
|
);
|
||||||
|
let html = generate_blog_entry(markdown, &config.templates_dir);
|
||||||
|
write_to_fs(
|
||||||
|
html,
|
||||||
|
&config.output_dir,
|
||||||
|
&file.file_name.into_string().unwrap(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue