123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- use comrak::{markdown_to_html, ComrakOptions};
-
- use config::Config;
- use page::Page;
- use post::Post;
-
- pub fn render_post(layout: &str, post_template: &str, post: &Post, config: &Config) -> String {
- layout
- .replace(
- "{{ page_title }}",
- &format!("{} | {}", post.title, config.site_name),
- )
- .replace(
- "{{ contents }}",
- &post_template
- .replace("{{ title }}", &post.title)
- .replace(
- "{{ body }}",
- &markdown_to_html(&post.body, &ComrakOptions::default()),
- )
- .replace("{{ date }}", &post.date.format("%Y-%m-%d").to_string()),
- )
- }
-
- pub fn render_page(layout: &str, page_template: &str, page: &Page, config: &Config) -> String {
- layout
- .replace(
- "{{ page_title }}",
- &format!("{} | {}", page.title, config.site_name),
- )
- .replace(
- "{{ contents }}",
- &page_template.replace("{{ title }}", &page.title).replace(
- "{{ body }}",
- &markdown_to_html(&page.body, &ComrakOptions::default()),
- ),
- )
- }
-
- pub fn render_post_listing(
- layout: &str,
- post_listing_template: &str,
- post_item_template: &str,
- posts: &Vec<Post>,
- config: &Config,
- ) -> String {
- layout
- .replace("{{ page_title }}", &format!("{}", config.site_name))
- .replace(
- "{{ contents }}",
- &post_listing_template.replace(
- "{{ post_listing }}",
- &posts
- .iter()
- .map(|ref post| {
- post_item_template
- .replace("{{ slug }}", &post.slug)
- .replace("{{ title }}", &post.title)
- .replace("{{ date }}", &post.date.format("%Y-%m-%d").to_string())
- })
- .collect::<Vec<String>>()
- .join("\n"),
- ),
- )
- }
-
- #[cfg(test)]
- mod tests {
- use super::{render_post, render_post_listing, Config, Post};
- #[allow(unused_imports)]
- use chrono::NaiveDate;
-
- #[test]
- fn test_render_post() {
- let output = render_post(
- "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
- "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
- &Post {
- title: String::from("hello world"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("hello-world"),
- date: NaiveDate::from_ymd(2019, 1, 1),
- },
- &Config {
- site_name: "Test Site".to_string(),
- },
- )
- .replace("\n", "");
-
- assert_eq!(
- "<html><head><title>hello world | Test Site</title></head><body><article><h1>hello world</h1><div><p>lorem ipsum dolor sit amet</p></div></article></body></html>",
- &output,
- );
- }
-
- #[test]
- fn test_render_post_listing() {
- let posts = vec![
- Post {
- title: String::from("First post"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("first-post"),
- date: NaiveDate::from_ymd(2019, 1, 1),
- },
- Post {
- title: String::from("Second post"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("second-post"),
- date: NaiveDate::from_ymd(2019, 1, 1),
- },
- Post {
- title: String::from("Third post"),
- body: String::from("lorem ipsum dolor sit amet"),
- slug: String::from("third-post"),
- date: NaiveDate::from_ymd(2019, 1, 1),
- },
- ];
- let output = render_post_listing(
- "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
- "<ul>{{ post_listing }}</ul>",
- "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
- &posts,
- &Config {
- site_name: "Test Site".to_string(),
- },
- )
- .replace("\n", "");
-
- assert_eq!(
- "<html><head><title>Test Site</title></head><body><ul><li><a href=\"/first-post\">First post</a></li><li><a href=\"/second-post\">Second post</a></li><li><a href=\"/third-post\">Third post</a></li></ul></body></html>",
- &output,
- );
- }
- }
|