A static site generator written in Rust
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

rss.rs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use chrono::{DateTime, Utc};
  2. use htmlescape::encode_minimal;
  3. use config::Config;
  4. use entry::{Entry, EntryKind};
  5. pub fn generate_post_rss(config: &Config, post: &Entry) -> String {
  6. let date: DateTime<Utc> = DateTime::from_utc(post.date.unwrap().and_hms(0, 0, 0), Utc);
  7. let date_string = date.format("%a, %d %b %Y %H:%M:%S %z").to_string();
  8. let url = format!("{}/posts/{}/", config.url, post.slug);
  9. format!(
  10. "<item><title>{}</title><description>{}</description><guid>{}</guid><link>{}</link><pubDate>{}</pubDate></item>",
  11. post.title, encode_minimal(&post.render("<article>{{ body }}</article>", config)), url, url, date_string
  12. )
  13. }
  14. pub fn generate_rss(config: &Config, posts: &Vec<Entry>) -> String {
  15. let items = posts
  16. .into_iter()
  17. .filter(|post| post.kind == EntryKind::Post)
  18. .map(|post| generate_post_rss(&config, &post))
  19. .collect::<Vec<String>>()
  20. .join("");
  21. format!(
  22. "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\"><channel><atom:link href=\"{}/rss.xml\" rel=\"self\" /><title>{}</title><description>{}</description><link>{}</link>{}</channel></rss>",
  23. config.url, config.site_name, config.description, config.url, items
  24. )
  25. }
  26. #[cfg(test)]
  27. mod tests {
  28. use chrono::NaiveDate;
  29. use super::generate_rss;
  30. use config::Config;
  31. use entry::{Entry, EntryKind};
  32. #[test]
  33. fn generates_rss_feed() {
  34. let config = Config {
  35. site_name: String::from("Lorem Ipsum"),
  36. url: String::from("https://www.loremipsum.com"),
  37. description: String::from("recent posts from loremipsum.com"),
  38. };
  39. let posts: Vec<Entry> = vec![Entry {
  40. title: String::from("Hello World"),
  41. body: String::from("lorem ipsum dolor sit amet"),
  42. slug: String::from("hello-world"),
  43. date: Some(NaiveDate::from_ymd(2019, 1, 1)),
  44. kind: EntryKind::Post,
  45. }];
  46. assert_eq!(
  47. generate_rss(&config, &posts),
  48. "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\"><channel><atom:link href=\"https://www.loremipsum.com/rss.xml\" rel=\"self\" /><title>Lorem Ipsum</title><description>recent posts from loremipsum.com</description><link>https://www.loremipsum.com</link><item><title>Hello World</title><description>&lt;article&gt;&lt;p&gt;lorem ipsum dolor sit amet&lt;/p&gt;\n&lt;/article&gt;</description><guid>https://www.loremipsum.com/posts/hello-world/</guid><link>https://www.loremipsum.com/posts/hello-world/</link><pubDate>Tue, 01 Jan 2019 00:00:00 +0000</pubDate></item></channel></rss>"
  49. );
  50. }
  51. }