mlem/src/index.rs
2024-04-09 17:28:56 +02:00

65 lines
2.2 KiB
Rust

use derive_more::Constructor;
use serde::Serialize;
use serde_json::value::Value;
use std::collections::HashMap;
use tera::{Context, Tera};
#[derive(Constructor, Debug, Serialize)]
pub struct BlogPost {
pub title: String,
pub human_date: String,
pub sort_date: i64, // Sort date = unix timestamp
pub content: String, // Unformatted Content of blog post can be used to display a preview or reading time estimate. Will be html when assigned but later turned into raw text
pub output_file_name: String,
}
pub fn generate(mut blog_posts: Vec<BlogPost>, template_dir: &String) -> String {
for post in &mut blog_posts {
post.content = get_unformatted_text(post.content.clone());
}
let mut tera = Tera::new(&format!("{}/*", template_dir)).unwrap();
tera.autoescape_on(vec![]);
tera.register_filter("truncate", truncate);
let mut context = Context::new();
context.insert("blog_posts", &blog_posts);
tera.render("index.html", &context).unwrap()
}
fn get_unformatted_text(html: String) -> String {
let frag = scraper::Html::parse_fragment(&html);
let mut unformatted_text = String::new();
for node in frag.tree {
if let scraper::node::Node::Text(text) = node {
unformatted_text.push_str(&text);
}
}
unformatted_text
}
fn truncate(value: &Value, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
let mut value = value.as_str().unwrap().to_string();
let new_len: usize = args.get("len").unwrap().as_str().unwrap().parse().unwrap();
value.truncate(new_len);
Ok(Value::String(value.to_string()))
}
#[cfg(test)]
mod tests {
use crate::index::*;
#[test]
fn truncate_shortens() {
let mut args: HashMap<String, Value> = HashMap::new();
args.insert("len".to_string(), Value::String("4".to_string()));
let truncated_string = truncate(&Value::String("Meow Nya".to_string()), &args).unwrap();
assert_eq!(truncated_string.as_str().unwrap(), "Meow");
}
#[test]
fn unformat_html() {
let html_src = "<p>Meow nyaaa<em> UwU</em></p>".to_string();
let unformatted_text = get_unformatted_text(html_src);
assert_eq!(unformatted_text, "Meow nyaaa UwU");
}
}