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.5KB

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