From b256aaf7d0910bef215be48c3d6d9fd0b64eaced Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 1 May 2017 15:35:49 +0900 Subject: [PATCH] Do not append non-sortables pages when not sortable --- CHANGELOG.md | 2 + src/page.rs | 41 +++++++++++++++++-- .../content/posts/tutorials/devops/_index.md | 2 +- .../content/posts/tutorials/devops/docker.md | 3 +- .../content/posts/tutorials/devops/nix.md | 3 +- .../posts/tutorials/programming/_index.md | 2 +- .../posts/tutorials/programming/python.md | 2 +- .../posts/tutorials/programming/rust.md | 2 +- tests/site.rs | 2 +- 9 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f677c724..08d7ca25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Fix XML templates overriding and reloading - `title` and `description` are now optional in the front matter - Add GenericConfig, Vim syntax +- Add `_index.md` for homepage as well +- Allow sorting by `none`, `date` and `order` for sections ## 0.0.4 (2017-04-23) diff --git a/src/page.rs b/src/page.rs index 1296bf51..9530c3f5 100644 --- a/src/page.rs +++ b/src/page.rs @@ -239,8 +239,10 @@ impl ser::Serialize for Page { } } -/// Sort pages -/// TODO: write doc and tests +/// Sort pages using the method for the given section +/// +/// Any pages that doesn't have a date when the sorting method is date or order +/// when the sorting method is order will be ignored. pub fn sort_pages(pages: Vec, section: Option<&Section>) -> Vec { let sort_by = if let Some(ref sec) = section { sec.meta.sort_by() @@ -260,7 +262,7 @@ pub fn sort_pages(pages: Vec, section: Option<&Section>) -> Vec { } } can_be_sorted.sort_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap())); - can_be_sorted.append(&mut cannot_be_sorted); + // can_be_sorted.append(&mut cannot_be_sorted); can_be_sorted }, @@ -275,7 +277,7 @@ pub fn sort_pages(pages: Vec, section: Option<&Section>) -> Vec { } } can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order())); - can_be_sorted.append(&mut cannot_be_sorted); + // can_be_sorted.append(&mut cannot_be_sorted); can_be_sorted }, @@ -429,6 +431,37 @@ mod tests { assert_eq!(pages[2].clone().meta.order.unwrap(), 1); } + #[test] + fn test_can_sort_none() { + let input = vec![ + create_page_with_order(2), + create_page_with_order(3), + create_page_with_order(1), + ]; + let mut front_matter = FrontMatter::default(); + front_matter.sort_by = Some(SortBy::None); + let section = Section::new(Path::new("hey"), front_matter); + let pages = sort_pages(input, Some(§ion)); + // Should be sorted by date + assert_eq!(pages[0].clone().meta.order.unwrap(), 2); + assert_eq!(pages[1].clone().meta.order.unwrap(), 3); + assert_eq!(pages[2].clone().meta.order.unwrap(), 1); + } + + #[test] + fn test_ignore_page_with_missing_field() { + let input = vec![ + create_page_with_order(2), + create_page_with_order(3), + create_page_with_date("2019-01-01"), + ]; + let mut front_matter = FrontMatter::default(); + front_matter.sort_by = Some(SortBy::Order); + let section = Section::new(Path::new("hey"), front_matter); + let pages = sort_pages(input, Some(§ion)); + assert_eq!(pages.len(), 2); + } + #[test] fn test_populate_previous_and_next_pages() { let input = vec![ diff --git a/test_site/content/posts/tutorials/devops/_index.md b/test_site/content/posts/tutorials/devops/_index.md index b4f4ab5d..d2e9ea9b 100644 --- a/test_site/content/posts/tutorials/devops/_index.md +++ b/test_site/content/posts/tutorials/devops/_index.md @@ -1,4 +1,4 @@ +++ title = "DevOps" -description = "" +sort_by = "order" +++ diff --git a/test_site/content/posts/tutorials/devops/docker.md b/test_site/content/posts/tutorials/devops/docker.md index 85796b1a..5fa77495 100644 --- a/test_site/content/posts/tutorials/devops/docker.md +++ b/test_site/content/posts/tutorials/devops/docker.md @@ -1,7 +1,6 @@ +++ title = "Docker" -description = "" -date = "2017-02-01" +order = 1 +++ A simple page diff --git a/test_site/content/posts/tutorials/devops/nix.md b/test_site/content/posts/tutorials/devops/nix.md index 4fdf3515..3b49d2a7 100644 --- a/test_site/content/posts/tutorials/devops/nix.md +++ b/test_site/content/posts/tutorials/devops/nix.md @@ -1,7 +1,6 @@ +++ title = "Nix" -description = "" -date = "2017-03-01" +order = 2 +++ A simple page diff --git a/test_site/content/posts/tutorials/programming/_index.md b/test_site/content/posts/tutorials/programming/_index.md index d46aac2f..5a6e2e3c 100644 --- a/test_site/content/posts/tutorials/programming/_index.md +++ b/test_site/content/posts/tutorials/programming/_index.md @@ -1,4 +1,4 @@ +++ title = "Programming" -description = "" +sort_by = "order" +++ diff --git a/test_site/content/posts/tutorials/programming/python.md b/test_site/content/posts/tutorials/programming/python.md index 8a0a68d1..39c54571 100644 --- a/test_site/content/posts/tutorials/programming/python.md +++ b/test_site/content/posts/tutorials/programming/python.md @@ -1,6 +1,6 @@ +++ title = "Python tutorial" -description = "" +order = 1 +++ A simple page diff --git a/test_site/content/posts/tutorials/programming/rust.md b/test_site/content/posts/tutorials/programming/rust.md index b2173237..4d2cd152 100644 --- a/test_site/content/posts/tutorials/programming/rust.md +++ b/test_site/content/posts/tutorials/programming/rust.md @@ -1,6 +1,6 @@ +++ title = "Rust" -description = "" +order = 2 +++ A simple page diff --git a/tests/site.rs b/tests/site.rs index fbdbbac0..b9f41cc6 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -44,7 +44,7 @@ fn test_can_parse_site() { let posts_section = &site.sections[&posts_path]; assert_eq!(posts_section.subsections.len(), 1); //println!("{:#?}", posts_section.pages); - assert_eq!(posts_section.pages.len(), 5); + assert_eq!(posts_section.pages.len(), 4); let tutorials_section = &site.sections[&posts_path.join("tutorials")]; assert_eq!(tutorials_section.subsections.len(), 2);