From 3d22b4f1f93a358cdc2c7a56c319e33e51652494 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 2 Jan 2019 22:11:34 +0100 Subject: [PATCH] Generate per language RSS feed --- .gitignore | 1 + components/config/src/config.rs | 2 +- components/library/src/content/file_info.rs | 2 +- components/site/src/lib.rs | 34 +++++++++++++++++++- components/site/tests/common.rs | 2 +- components/site/tests/site_i18n.rs | 15 +++++++-- test_site_i18n/config.toml | 5 ++- test_site_i18n/content/blog/_index.it.md | 4 +++ test_site_i18n/content/blog/fixed-slug.it.md | 7 ++++ 9 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 test_site_i18n/content/blog/_index.it.md create mode 100644 test_site_i18n/content/blog/fixed-slug.it.md diff --git a/.gitignore b/.gitignore index 74d95af2..777cd7b2 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ huge-kb current.bench now.bench +*.zst # snapcraft artifacts snap/.snapcraft diff --git a/components/config/src/config.rs b/components/config/src/config.rs index 13af11c3..8e556dfc 100644 --- a/components/config/src/config.rs +++ b/components/config/src/config.rs @@ -246,7 +246,7 @@ impl Config { } /// Is this site using i18n? - pub fn uses_i18n(&self) -> bool { + pub fn is_multilingual(&self) -> bool { !self.languages.is_empty() } diff --git a/components/library/src/content/file_info.rs b/components/library/src/content/file_info.rs index 2b8fc88c..58a5990c 100644 --- a/components/library/src/content/file_info.rs +++ b/components/library/src/content/file_info.rs @@ -109,7 +109,7 @@ impl FileInfo { /// remove it and return the language code pub fn find_language(&mut self, config: &Config) -> Result> { // No languages? Nothing to do - if !config.uses_i18n() { + if !config.is_multilingual() { return Ok(None); } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index efe889dc..99a0892e 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -511,9 +511,41 @@ impl Site { self.render_sections()?; self.render_orphan_pages()?; self.render_sitemap()?; + if self.config.generate_rss { - self.render_rss_feed(self.library.pages_values(), None)?; + let pages = if self.config.is_multilingual() { + self.library + .pages_values() + .iter() + .filter(|p| p.lang.is_none()) + .map(|p| *p) + .collect() + } else { + self.library.pages_values() + }; + self.render_rss_feed(pages, None)?; } + + for lang in &self.config.languages { + if !lang.rss { + continue; + } + let pages = self + .library + .pages_values() + .iter() + .filter(|p| { + if let Some(ref l) = p.lang { + l == &lang.code + } else { + false + } + }) + .map(|p| *p) + .collect(); + self.render_rss_feed(pages, Some(&PathBuf::from(lang.code.clone())))?; + } + self.render_404()?; self.render_robots()?; self.render_taxonomies()?; diff --git a/components/site/tests/common.rs b/components/site/tests/common.rs index 34c6a45c..bab5db9b 100644 --- a/components/site/tests/common.rs +++ b/components/site/tests/common.rs @@ -30,7 +30,7 @@ macro_rules! file_contains { let mut file = std::fs::File::open(&path).unwrap(); let mut s = String::new(); file.read_to_string(&mut s).unwrap(); - println!("{}", s); + // println!("{}", s); s.contains($text) }}; } diff --git a/components/site/tests/site_i18n.rs b/components/site/tests/site_i18n.rs index 0b5e93b5..d4bf5198 100644 --- a/components/site/tests/site_i18n.rs +++ b/components/site/tests/site_i18n.rs @@ -13,8 +13,8 @@ fn can_parse_multilingual_site() { let mut site = Site::new(&path, "config.toml").unwrap(); site.load().unwrap(); - assert_eq!(site.library.pages().len(), 9); - assert_eq!(site.library.sections().len(), 4); + assert_eq!(site.library.pages().len(), 10); + assert_eq!(site.library.sections().len(), 6); // default index sections let default_index_section = @@ -79,4 +79,15 @@ fn can_build_multilingual_site() { assert!(file_exists!(public, "sitemap.xml")); assert!(file_contains!(public, "sitemap.xml", "https://example.com/blog/something-else/")); assert!(file_contains!(public, "sitemap.xml", "https://example.com/fr/blog/something-else/")); + assert!(file_contains!(public, "sitemap.xml", "https://example.com/it/blog/something-else/")); + + // one rss per language + assert!(file_exists!(public, "rss.xml")); + assert!(file_contains!(public, "rss.xml", "https://example.com/blog/something-else/")); + assert!(!file_contains!(public, "rss.xml", "https://example.com/fr/blog/something-else/")); + assert!(file_exists!(public, "fr/rss.xml")); + assert!(!file_contains!(public, "fr/rss.xml", "https://example.com/blog/something-else/")); + assert!(file_contains!(public, "fr/rss.xml", "https://example.com/fr/blog/something-else/")); + // Italian doesn't have RSS enabled + assert!(!file_exists!(public, "it/rss.xml")); } diff --git a/test_site_i18n/config.toml b/test_site_i18n/config.toml index c5dd0f06..0a6635a4 100644 --- a/test_site_i18n/config.toml +++ b/test_site_i18n/config.toml @@ -11,8 +11,11 @@ highlight_code = false # Whether to build a search index to be used later on by a JavaScript library build_search_index = false +generate_rss = true + languages = [ - {code = "fr"}, + {code = "fr", rss = true}, + {code = "it", rss = false}, ] [extra] diff --git a/test_site_i18n/content/blog/_index.it.md b/test_site_i18n/content/blog/_index.it.md new file mode 100644 index 00000000..b19438ae --- /dev/null +++ b/test_site_i18n/content/blog/_index.it.md @@ -0,0 +1,4 @@ ++++ +sort_by = "date" +insert_anchors = "right" ++++ diff --git a/test_site_i18n/content/blog/fixed-slug.it.md b/test_site_i18n/content/blog/fixed-slug.it.md new file mode 100644 index 00000000..fcd918e1 --- /dev/null +++ b/test_site_i18n/content/blog/fixed-slug.it.md @@ -0,0 +1,7 @@ ++++ +title = "Un slug fixe" +slug = "something-else" +date = 2017-01-01 ++++ + +Una pagina che definisce il suo slug nel front-matter