A static site generator written in Rust
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

render.rs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use comrak::{markdown_to_html, ComrakOptions};
  2. use post::Post;
  3. pub fn render_post(layout: &str, post_template: &str, post: &Post) -> String {
  4. layout.replace(
  5. "{{ contents }}",
  6. &post_template.replace("{{ title }}", &post.title).replace(
  7. "{{ body }}",
  8. &markdown_to_html(&post.body, &ComrakOptions::default()),
  9. ),
  10. )
  11. }
  12. pub fn render_post_listing(
  13. layout: &str,
  14. post_listing_template: &str,
  15. post_item_template: &str,
  16. posts: &Vec<Post>,
  17. ) -> String {
  18. layout.replace(
  19. "{{ contents }}",
  20. &post_listing_template.replace(
  21. "{{ post_listing }}",
  22. &posts
  23. .iter()
  24. .map(|ref post| {
  25. post_item_template
  26. .replace("{{ slug }}", &post.slug)
  27. .replace("{{ title }}", &post.title)
  28. }).collect::<Vec<String>>()
  29. .join("\n"),
  30. ),
  31. )
  32. }
  33. #[cfg(test)]
  34. mod tests {
  35. #[allow(unused_imports)]
  36. use super::{render_post, render_post_listing, Post};
  37. #[test]
  38. fn test_render_post() {
  39. let output = render_post(
  40. "<html><head><title>Test</title></head><body>{{ contents }}</body></html>",
  41. "<article><h1>{{ title }}</h1><div>{{ body }}</div></article>",
  42. &Post {
  43. title: String::from("hello world"),
  44. body: String::from("lorem ipsum dolor sit amet"),
  45. slug: String::from("hello-world"),
  46. },
  47. ).replace("\n", "");
  48. assert_eq!(
  49. "<html><head><title>Test</title></head><body><article><h1>hello wo\
  50. rld</h1><div><p>lorem ipsum dolor sit amet</p></div></article></b\
  51. ody></html>",
  52. &output,
  53. );
  54. }
  55. #[test]
  56. fn test_render_post_listing() {
  57. let posts = vec![
  58. Post {
  59. title: String::from("First post"),
  60. body: String::from("lorem ipsum dolor sit amet"),
  61. slug: String::from("first-post"),
  62. },
  63. Post {
  64. title: String::from("Second post"),
  65. body: String::from("lorem ipsum dolor sit amet"),
  66. slug: String::from("second-post"),
  67. },
  68. Post {
  69. title: String::from("Third post"),
  70. body: String::from("lorem ipsum dolor sit amet"),
  71. slug: String::from("third-post"),
  72. },
  73. ];
  74. let output = render_post_listing(
  75. "<html><head><title>Test</title></head><body>{{ contents }}</body></html>",
  76. "<ul>{{ post_listing }}</ul>",
  77. "<li><a href=\"/{{ slug }}\">{{ title }}</a></li>",
  78. &posts,
  79. ).replace("\n", "");
  80. assert_eq!(
  81. "<html><head><title>Test</title></head><body><ul><li><a href=\"/fir\
  82. st-post\">First post</a></li><li><a href=\"/second-post\">Second po\
  83. st</a></li><li><a href=\"/third-post\">Third post</a></li></ul></bo\
  84. dy></html>",
  85. &output,
  86. );
  87. }
  88. }