Added article index generation
This commit is contained in:
parent
6d686e48aa
commit
b70bb8e542
|
@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet};
|
|||
|
||||
#[derive(Serialize, Default)]
|
||||
pub struct Article {
|
||||
title: String,
|
||||
filename: String,
|
||||
date: String,
|
||||
author: String,
|
||||
|
@ -48,14 +49,29 @@ pub fn generate_article(
|
|||
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)]
|
||||
mod tests {
|
||||
use crate::template::Template;
|
||||
use crate::{site::tests::fixture_site, template::Template};
|
||||
|
||||
use super::*;
|
||||
|
||||
pub fn fixture_article() -> Article {
|
||||
Article {
|
||||
title: "Test Article #1".to_string(),
|
||||
filename: "test.org".to_string(),
|
||||
date: "".to_string(),
|
||||
author: "Bob Bobberson".to_string(),
|
||||
|
@ -68,16 +84,27 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate() {
|
||||
fn test_generate_article() {
|
||||
let article_ctx =
|
||||
ArticleContext::load_string(fixture_article(), "ten animals i slam in a net")
|
||||
.expect("couldnt build article");
|
||||
let template =
|
||||
Template::new("content: {{{data.content}}}, site: {{site.title}}".to_string());
|
||||
let site = crate::site::tests::fixture_site();
|
||||
let site = fixture_site();
|
||||
let res =
|
||||
generate_article(&site, &article_ctx, &template).expect("couldnt gnenerate template");
|
||||
|
||||
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 ");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue