Added article index generation

This commit is contained in:
Joe Bellus 2022-12-23 01:51:56 -05:00
parent 6d686e48aa
commit b70bb8e542
1 changed files with 30 additions and 3 deletions

View File

@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet};
#[derive(Serialize, Default)] #[derive(Serialize, Default)]
pub struct Article { pub struct Article {
title: String,
filename: String, filename: String,
date: String, date: String,
author: String, author: String,
@ -48,14 +49,29 @@ pub fn generate_article(
Ok(res) Ok(res)
} }
pub fn generate_article_index(
site: &Site,
articles: Vec<Article>,
template: &Template,
) -> Result<String> {
let ctx = json!({
"site": site,
"data": articles,
});
let res = template.generate(&ctx)?;
Ok(res)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::template::Template; use crate::{site::tests::fixture_site, template::Template};
use super::*; use super::*;
pub fn fixture_article() -> Article { pub fn fixture_article() -> Article {
Article { Article {
title: "Test Article #1".to_string(),
filename: "test.org".to_string(), filename: "test.org".to_string(),
date: "".to_string(), date: "".to_string(),
author: "Bob Bobberson".to_string(), author: "Bob Bobberson".to_string(),
@ -68,16 +84,27 @@ mod tests {
} }
#[test] #[test]
fn test_generate() { fn test_generate_article() {
let article_ctx = let article_ctx =
ArticleContext::load_string(fixture_article(), "ten animals i slam in a net") ArticleContext::load_string(fixture_article(), "ten animals i slam in a net")
.expect("couldnt build article"); .expect("couldnt build article");
let template = let template =
Template::new("content: {{{data.content}}}, site: {{site.title}}".to_string()); Template::new("content: {{{data.content}}}, site: {{site.title}}".to_string());
let site = crate::site::tests::fixture_site(); let site = fixture_site();
let res = let res =
generate_article(&site, &article_ctx, &template).expect("couldnt gnenerate template"); generate_article(&site, &article_ctx, &template).expect("couldnt gnenerate template");
assert_eq!(res, "content: ten animals i slam in a net, site: Test Site"); assert_eq!(res, "content: ten animals i slam in a net, site: Test Site");
} }
#[test]
fn test_generate_article_index() {
let articles = vec![fixture_article(), fixture_article()];
let site = fixture_site();
let template = Template::new("list: {{#each data}}{{title}} {{/each}}".to_string());
let res =
generate_article_index(&site, articles, &template).expect("couldnt generate template");
assert_eq!(res, "list: Test Article #1 Test Article #1 ");
}
} }