A static site generator written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

render.rs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use comrak::{markdown_to_html, ComrakOptions};
  2. use config::Config;
  3. use page::Page;
  4. use post::Post;
  5. pub fn render_post(layout: &str, post_template: &str, post: &Post, config: &Config) -> String {
  6. layout
  7. .replace(
  8. "{{ page_title }}",
  9. &format!("{} | {}", post.title, config.site_name),
  10. )
  11. .replace(
  12. "{{ contents }}",
  13. &post_template.replace("{{ title }}", &post.title).replace(
  14. "{{ body }}",
  15. &markdown_to_html(&post.body, &ComrakOptions::default()),
  16. ),
  17. )
  18. }
  19. pub fn render_page(layout: &str, page_template: &str, page: &Page, config: &Config) -> String {
  20. layout
  21. .replace(
  22. "{{ page_title }}",
  23. &format!("{} | {}", page.title, config.site_name),
  24. )
  25. .replace(
  26. "{{ contents }}",
  27. &page_template.replace("{{ title }}", &page.title).replace(
  28. "{{ body }}",
  29. &markdown_to_html(&page.body, &ComrakOptions::default()),
  30. ),
  31. )
  32. }
  33. pub fn render_post_listing(
  34. layout: &str,
  35. post_listing_template: &str,
  36. post_item_template: &str,
  37. posts: &Vec<Post>,
  38. config: &Config,
  39. ) -> String {
  40. layout
  41. .replace("{{ page_title }}", &format!("{}", config.site_name))
  42. .replace(
  43. "{{ contents }}",
  44. &post_listing_template.replace(
  45. "{{ post_listing }}",
  46. &posts
  47. .iter()
  48. .map(|ref post| {
  49. post_item_template
  50. .replace("{{ slug }}", &post.slug)
  51. .replace("{{ title }}", &post.title)
  52. })
  53. .collect::<Vec<String>>()
  54. .join("\n"),
  55. ),
  56. )
  57. }
  58. #[cfg(test)]
  59. mod tests {
  60. use super::{render_post, render_post_listing, Config, Post};
  61. #[allow(unused_imports)]
  62. use chrono::NaiveDate;
  63. #[test]
  64. fn test_render_post() {
  65. let output = render_post(
  66. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  67. "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  68. &Post {
  69. title: String::from("hello world"),
  70. body: String::from("lorem ipsum dolor sit amet"),
  71. slug: String::from("hello-world"),
  72. date: NaiveDate::from_ymd(2019, 1, 1),
  73. },
  74. &Config {
  75. site_name: "Test Site".to_string(),
  76. },
  77. )
  78. .replace("\n", "");
  79. assert_eq!(
  80. "<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>",
  81. &output,
  82. );
  83. }
  84. #[test]
  85. fn test_render_post_listing() {
  86. let posts = vec![
  87. Post {
  88. title: String::from("First post"),
  89. body: String::from("lorem ipsum dolor sit amet"),
  90. slug: String::from("first-post"),
  91. date: NaiveDate::from_ymd(2019, 1, 1),
  92. },
  93. Post {
  94. title: String::from("Second post"),
  95. body: String::from("lorem ipsum dolor sit amet"),
  96. slug: String::from("second-post"),
  97. date: NaiveDate::from_ymd(2019, 1, 1),
  98. },
  99. Post {
  100. title: String::from("Third post"),
  101. body: String::from("lorem ipsum dolor sit amet"),
  102. slug: String::from("third-post"),
  103. date: NaiveDate::from_ymd(2019, 1, 1),
  104. },
  105. ];
  106. let output = render_post_listing(
  107. "<html><head><title>{{ page_title }}</title></head><body>{{ contents }}</body></html>",
  108. "<ul>{{ post_listing }}</ul>",
  109. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  110. &posts,
  111. &Config {
  112. site_name: "Test Site".to_string(),
  113. },
  114. )
  115. .replace("\n", "");
  116. assert_eq!(
  117. "<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>",
  118. &output,
  119. );
  120. }
  121. }