Formatting
This commit is contained in:
parent
6dbedc5eb6
commit
ae3c4a9a6f
|
@ -1,7 +1,7 @@
|
||||||
use serde::{Deserialize,Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
#[derive(Deserialize,Serialize,Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub output_dir: String,
|
pub output_dir: String,
|
||||||
pub src_dir: String,
|
pub src_dir: String,
|
||||||
|
@ -9,7 +9,7 @@ pub struct Config {
|
||||||
pub emoji_config: Option<EmojiConfig>,
|
pub emoji_config: Option<EmojiConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize,Serialize,Debug)]
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct EmojiConfig {
|
pub struct EmojiConfig {
|
||||||
pub emoji_web_directory: String,
|
pub emoji_web_directory: String,
|
||||||
pub emoji_local_directory: String,
|
pub emoji_local_directory: String,
|
||||||
|
@ -17,7 +17,12 @@ pub struct EmojiConfig {
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Config { output_dir: "output".to_string() , src_dir: "md_src".to_string(), templates_dir: "templates".to_string(), emoji_config: None }
|
Config {
|
||||||
|
output_dir: "output".to_string(),
|
||||||
|
src_dir: "md_src".to_string(),
|
||||||
|
templates_dir: "templates".to_string(),
|
||||||
|
emoji_config: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn read_config() -> Config {
|
pub fn read_config() -> Config {
|
||||||
|
|
11
src/emoji.rs
11
src/emoji.rs
|
@ -2,10 +2,7 @@ use regex::Regex;
|
||||||
use std::{ffi::OsString, fs::read_dir};
|
use std::{ffi::OsString, fs::read_dir};
|
||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
pub fn emoji_pass(
|
pub fn emoji_pass(markdown: &str, emoji_config: &Option<config::EmojiConfig>) -> String {
|
||||||
markdown: &str,
|
|
||||||
emoji_config: &Option<config::EmojiConfig>,
|
|
||||||
) -> String {
|
|
||||||
if emoji_config.is_none() {
|
if emoji_config.is_none() {
|
||||||
return markdown.to_string();
|
return markdown.to_string();
|
||||||
}
|
}
|
||||||
|
@ -14,8 +11,10 @@ pub fn emoji_pass(
|
||||||
let re_emojis = Regex::new(r":\w+:").unwrap();
|
let re_emojis = Regex::new(r":\w+:").unwrap();
|
||||||
|
|
||||||
for emoji in re_emojis.find_iter(&markdown.clone()) {
|
for emoji in re_emojis.find_iter(&markdown.clone()) {
|
||||||
let emoji_file_name =
|
let emoji_file_name = get_emoji_file_name(
|
||||||
get_emoji_file_name(&get_emoji_name(emoji.as_str()), &emoji_config.as_ref().unwrap().emoji_local_directory);
|
&get_emoji_name(emoji.as_str()),
|
||||||
|
&emoji_config.as_ref().unwrap().emoji_local_directory,
|
||||||
|
);
|
||||||
if emoji_file_name.is_none() {
|
if emoji_file_name.is_none() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
10
src/index.rs
10
src/index.rs
|
@ -39,13 +39,11 @@ fn get_unformatted_text(html: String) -> String {
|
||||||
|
|
||||||
fn truncate(value: &Value, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
|
fn truncate(value: &Value, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
|
||||||
let mut value = value.as_str().unwrap().to_string();
|
let mut value = value.as_str().unwrap().to_string();
|
||||||
let new_len:usize = args.get("len").unwrap().as_str().unwrap().parse().unwrap();
|
let new_len: usize = args.get("len").unwrap().as_str().unwrap().parse().unwrap();
|
||||||
value
|
value.truncate(new_len);
|
||||||
.truncate(new_len);
|
|
||||||
Ok(Value::String(value.to_string()))
|
Ok(Value::String(value.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::index::*;
|
use crate::index::*;
|
||||||
|
@ -54,13 +52,13 @@ mod tests {
|
||||||
let mut args: HashMap<String, Value> = HashMap::new();
|
let mut args: HashMap<String, Value> = HashMap::new();
|
||||||
args.insert("len".to_string(), Value::String("4".to_string()));
|
args.insert("len".to_string(), Value::String("4".to_string()));
|
||||||
let truncated_string = truncate(&Value::String("Meow Nya".to_string()), &args).unwrap();
|
let truncated_string = truncate(&Value::String("Meow Nya".to_string()), &args).unwrap();
|
||||||
assert_eq!(truncated_string.as_str().unwrap(),"Meow");
|
assert_eq!(truncated_string.as_str().unwrap(), "Meow");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unformat_html() {
|
fn unformat_html() {
|
||||||
let html_src = "<p>Meow nyaaa<em> UwU</em></p>".to_string();
|
let html_src = "<p>Meow nyaaa<em> UwU</em></p>".to_string();
|
||||||
let unformatted_text = get_unformatted_text(html_src);
|
let unformatted_text = get_unformatted_text(html_src);
|
||||||
assert_eq!(unformatted_text,"Meow nyaaa UwU");
|
assert_eq!(unformatted_text, "Meow nyaaa UwU");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub mod index;
|
||||||
use mlem::*;
|
use mlem::*;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main(){
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
if args.len() == 1 {
|
if args.len() == 1 {
|
||||||
|
@ -25,10 +25,7 @@ fn generate() {
|
||||||
|
|
||||||
for file in raw_files {
|
for file in raw_files {
|
||||||
let mut markdown = read_to_string(file.path).expect("File does not exist");
|
let mut markdown = read_to_string(file.path).expect("File does not exist");
|
||||||
markdown = emoji::emoji_pass(
|
markdown = emoji::emoji_pass(&markdown, &config.emoji_config);
|
||||||
&markdown,
|
|
||||||
&config.emoji_config,
|
|
||||||
);
|
|
||||||
|
|
||||||
let (html, index_content) = generate_blog_entry(markdown, &config.templates_dir);
|
let (html, index_content) = generate_blog_entry(markdown, &config.templates_dir);
|
||||||
write_to_fs(
|
write_to_fs(
|
||||||
|
|
Loading…
Reference in a new issue