From c2437cc0eb7e431493022d3b1a5c254fa998119c Mon Sep 17 00:00:00 2001 From: Daniel Sockwell Date: Fri, 27 Jul 2018 22:20:20 -0400 Subject: [PATCH] Remove `order` and add `heavier`/`later` This commit removes the option to sort by order and also removes `page.next` and `page.previous` variables. Instead, pages can be sorted by two methods `date` and `weight`. The Tera `reverse` filter will reverse either of those sorts, so the old `order` behavior can be achieved by using the `reverse` filter with `weight`. In place of the `previous`/`next` variables, this commit adds the `page.earlier`/`page.later` variables (which are set when the page is sorted by date) and the `page.heavier`/`page.lighter` variables (which are set when the page is sorted by weight). These variables have the advantage of not having confusing semantics when the `reverse` filter is used. --- components/content/src/page.rs | 30 ++- components/content/src/sorting.rs | 174 ++++++------------ components/front_matter/src/lib.rs | 2 - components/rebuild/tests/rebuild.rs | 10 +- components/site/src/lib.rs | 2 +- .../content/posts/tutorials/devops/_index.md | 2 +- .../content/posts/tutorials/devops/docker.md | 2 +- .../content/posts/tutorials/devops/nix.md | 2 +- .../posts/tutorials/programming/_index.md | 2 +- .../posts/tutorials/programming/python.md | 2 +- .../posts/tutorials/programming/rust.md | 2 +- test_site/content/rebuild/_index.md | 2 +- test_site/content/rebuild/first.md | 2 +- test_site/content/rebuild/second.md | 2 +- 14 files changed, 89 insertions(+), 147 deletions(-) diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 780faf2..567c95c 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -44,10 +44,14 @@ pub struct Page { /// When is found in the text, will take the content up to that part /// as summary pub summary: Option, - /// The previous page, by whatever sorting is used for the index/section - pub previous: Option>, - /// The next page, by whatever sorting is used for the index/section - pub next: Option>, + /// The earlier page, for pages sorted by date + pub earlier: Option>, + /// The later page, for pages sorted by date + pub later: Option>, + /// The lighter page, for pages sorted by weight + pub lighter: Option>, + /// The heavier page, for pages sorted by weight + pub heavier: Option>, /// Toc made from the headers of the markdown file pub toc: Vec
, } @@ -68,8 +72,10 @@ impl Page { components: vec![], permalink: "".to_string(), summary: None, - previous: None, - next: None, + earlier: None, + later: None, + lighter: None, + heavier: None, toc: vec![], } } @@ -229,8 +235,10 @@ impl Default for Page { components: vec![], permalink: "".to_string(), summary: None, - previous: None, - next: None, + earlier: None, + later: None, + lighter: None, + heavier: None, toc: vec![], } } @@ -263,8 +271,10 @@ impl ser::Serialize for Page { let (word_count, reading_time) = get_reading_analytics(&self.raw_content); state.serialize_field("word_count", &word_count)?; state.serialize_field("reading_time", &reading_time)?; - state.serialize_field("previous", &self.previous)?; - state.serialize_field("next", &self.next)?; + state.serialize_field("earlier", &self.earlier)?; + state.serialize_field("later", &self.later)?; + state.serialize_field("lighter", &self.lighter)?; + state.serialize_field("heavier", &self.heavier)?; state.serialize_field("toc", &self.toc)?; state.serialize_field("draft", &self.is_draft())?; let assets = self.serialize_assets(); diff --git a/components/content/src/sorting.rs b/components/content/src/sorting.rs index b96c6b1..8d9bcda 100644 --- a/components/content/src/sorting.rs +++ b/components/content/src/sorting.rs @@ -7,7 +7,7 @@ use front_matter::SortBy; /// Sort pages by the given criteria /// -/// Any pages that doesn't have a the required field when the sorting method is other than none +/// Any pages that doesn't have a required field when the sorting method is other than none /// will be ignored. pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { if sort_by == SortBy::None { @@ -19,7 +19,6 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { .partition(|page| { match sort_by { SortBy::Date => page.meta.date.is_some(), - SortBy::Order => page.meta.order.is_some(), SortBy::Weight => page.meta.weight.is_some(), _ => unreachable!() } @@ -36,16 +35,6 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { } }) }, - SortBy::Order => { - can_be_sorted.par_sort_unstable_by(|a, b| { - let ord = b.meta.order().cmp(&a.meta.order()); - if ord == Ordering::Equal { - a.permalink.cmp(&b.permalink) - } else { - ord - } - }) - }, SortBy::Weight => { can_be_sorted.par_sort_unstable_by(|a, b| { let ord = a.meta.weight().cmp(&b.meta.weight()); @@ -64,7 +53,7 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { /// Horribly inefficient way to set previous and next on each pages that skips drafts /// So many clones -pub fn populate_previous_and_next_pages(input: &[Page]) -> Vec { +pub fn populate_previous_and_next_pages(input: &[Page], sort_by: SortBy) -> Vec { let mut res = Vec::with_capacity(input.len()); // The input is already sorted @@ -91,9 +80,21 @@ pub fn populate_previous_and_next_pages(input: &[Page]) -> Vec { // Remove prev/next otherwise we serialise the whole thing... let mut next_page = input[j].clone(); - next_page.previous = None; - next_page.next = None; - new_page.next = Some(Box::new(next_page)); + + match sort_by { + SortBy::Weight => { + next_page.lighter = None; + next_page.heavier = None; + new_page.lighter = Some(Box::new(next_page)); + }, + SortBy::Date => { + next_page.earlier = None; + next_page.later = None; + new_page.later = Some(Box::new(next_page)); + }, + SortBy::None => { + } + } break; } } @@ -113,9 +114,20 @@ pub fn populate_previous_and_next_pages(input: &[Page]) -> Vec { // Remove prev/next otherwise we serialise the whole thing... let mut previous_page = input[j].clone(); - previous_page.previous = None; - previous_page.next = None; - new_page.previous = Some(Box::new(previous_page)); + match sort_by { + SortBy::Weight => { + previous_page.lighter = None; + previous_page.heavier = None; + new_page.heavier = Some(Box::new(previous_page)); + }, + SortBy::Date => { + previous_page.earlier = None; + previous_page.later = None; + new_page.earlier = Some(Box::new(previous_page)); + }, + SortBy::None => { + } + } break; } } @@ -137,22 +149,6 @@ mod tests { Page::new("content/hello.md", front_matter) } - fn create_page_with_order(order: usize, filename: &str) -> Page { - let mut front_matter = PageFrontMatter::default(); - front_matter.order = Some(order); - let mut p = Page::new("content/".to_string() + filename, front_matter); - // Faking a permalink to test sorting with equal order - p.permalink = filename.to_string(); - p - } - - fn create_draft_page_with_order(order: usize) -> Page { - let mut front_matter = PageFrontMatter::default(); - front_matter.order = Some(order); - front_matter.draft = true; - Page::new("content/hello.md", front_matter) - } - fn create_page_with_weight(weight: usize) -> Page { let mut front_matter = PageFrontMatter::default(); front_matter.weight = Some(weight); @@ -173,37 +169,6 @@ mod tests { assert_eq!(pages[2].clone().meta.date.unwrap().to_string(), "2017-01-01"); } - #[test] - fn can_sort_by_order() { - let input = vec![ - create_page_with_order(2, "hello.md"), - create_page_with_order(3, "hello2.md"), - create_page_with_order(1, "hello3.md"), - ]; - let (pages, _) = sort_pages(input, SortBy::Order); - // Should be sorted by order - assert_eq!(pages[0].clone().meta.order.unwrap(), 3); - assert_eq!(pages[1].clone().meta.order.unwrap(), 2); - assert_eq!(pages[2].clone().meta.order.unwrap(), 1); - } - - #[test] - fn can_sort_by_order_uses_permalink_to_break_ties() { - let input = vec![ - create_page_with_order(3, "b.md"), - create_page_with_order(3, "a.md"), - create_page_with_order(3, "c.md"), - ]; - let (pages, _) = sort_pages(input, SortBy::Order); - // Should be sorted by order - assert_eq!(pages[0].clone().meta.order.unwrap(), 3); - assert_eq!(pages[0].clone().permalink, "a.md"); - assert_eq!(pages[1].clone().meta.order.unwrap(), 3); - assert_eq!(pages[1].clone().permalink, "b.md"); - assert_eq!(pages[2].clone().meta.order.unwrap(), 3); - assert_eq!(pages[2].clone().permalink, "c.md"); - } - #[test] fn can_sort_by_weight() { let input = vec![ @@ -221,25 +186,25 @@ mod tests { #[test] fn can_sort_by_none() { let input = vec![ - create_page_with_order(2, "a.md"), - create_page_with_order(3, "a.md"), - create_page_with_order(1, "a.md"), + create_page_with_weight(2), + create_page_with_weight(3), + create_page_with_weight(1), ]; let (pages, _) = sort_pages(input, SortBy::None); // 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); + assert_eq!(pages[0].clone().meta.weight.unwrap(), 2); + assert_eq!(pages[1].clone().meta.weight.unwrap(), 3); + assert_eq!(pages[2].clone().meta.weight.unwrap(), 1); } #[test] fn ignore_page_with_missing_field() { let input = vec![ - create_page_with_order(2, "a.md"), - create_page_with_order(3, "a.md"), + create_page_with_weight(2), + create_page_with_weight(3), create_page_with_date("2019-01-01"), ]; - let (pages, unsorted) = sort_pages(input, SortBy::Order); + let (pages, unsorted) = sort_pages(input, SortBy::Weight); assert_eq!(pages.len(), 2); assert_eq!(unsorted.len(), 1); } @@ -247,54 +212,23 @@ mod tests { #[test] fn can_populate_previous_and_next_pages() { let input = vec![ - create_page_with_order(1, "a.md"), - create_page_with_order(2, "b.md"), - create_page_with_order(3, "a.md"), - ]; - let pages = populate_previous_and_next_pages(&input); - - assert!(pages[0].clone().next.is_none()); - assert!(pages[0].clone().previous.is_some()); - assert_eq!(pages[0].clone().previous.unwrap().meta.order.unwrap(), 2); - - assert!(pages[1].clone().next.is_some()); - assert!(pages[1].clone().previous.is_some()); - assert_eq!(pages[1].clone().previous.unwrap().meta.order.unwrap(), 3); - assert_eq!(pages[1].clone().next.unwrap().meta.order.unwrap(), 1); - - assert!(pages[2].clone().next.is_some()); - assert!(pages[2].clone().previous.is_none()); - assert_eq!(pages[2].clone().next.unwrap().meta.order.unwrap(), 2); - } - - #[test] - fn can_populate_previous_and_next_pages_skip_drafts() { - let input = vec![ - create_draft_page_with_order(0), - create_page_with_order(1, "a.md"), - create_page_with_order(2, "b.md"), - create_page_with_order(3, "c.md"), - create_draft_page_with_order(4), + create_page_with_weight(1), + create_page_with_weight(2), + create_page_with_weight(3), ]; - let pages = populate_previous_and_next_pages(&input); - - assert!(pages[0].clone().next.is_none()); - assert!(pages[0].clone().previous.is_none()); - - assert!(pages[1].clone().next.is_none()); - assert!(pages[1].clone().previous.is_some()); - assert_eq!(pages[1].clone().previous.unwrap().meta.order.unwrap(), 2); + let pages = populate_previous_and_next_pages(&input, SortBy::Weight); - assert!(pages[2].clone().next.is_some()); - assert!(pages[2].clone().previous.is_some()); - assert_eq!(pages[2].clone().previous.unwrap().meta.order.unwrap(), 3); - assert_eq!(pages[2].clone().next.unwrap().meta.order.unwrap(), 1); + assert!(pages[0].clone().lighter.is_none()); + assert!(pages[0].clone().heavier.is_some()); + assert_eq!(pages[0].clone().heavier.unwrap().meta.weight.unwrap(), 2); - assert!(pages[3].clone().next.is_some()); - assert!(pages[3].clone().previous.is_none()); - assert_eq!(pages[3].clone().next.unwrap().meta.order.unwrap(), 2); + assert!(pages[1].clone().heavier.is_some()); + assert!(pages[1].clone().lighter.is_some()); + assert_eq!(pages[1].clone().lighter.unwrap().meta.weight.unwrap(), 1); + assert_eq!(pages[1].clone().heavier.unwrap().meta.weight.unwrap(), 3); - assert!(pages[4].clone().next.is_none()); - assert!(pages[4].clone().previous.is_none()); + assert!(pages[2].clone().lighter.is_some()); + assert!(pages[2].clone().heavier.is_none()); + assert_eq!(pages[2].clone().lighter.unwrap().meta.weight.unwrap(), 2); } } diff --git a/components/front_matter/src/lib.rs b/components/front_matter/src/lib.rs index 6cf79f0..8249376 100644 --- a/components/front_matter/src/lib.rs +++ b/components/front_matter/src/lib.rs @@ -30,8 +30,6 @@ lazy_static! { pub enum SortBy { /// Most recent to oldest Date, - /// Lower order comes last - Order, /// Lower weight comes first Weight, /// No sorting diff --git a/components/rebuild/tests/rebuild.rs b/components/rebuild/tests/rebuild.rs index 12cdb41..dbc3a35 100644 --- a/components/rebuild/tests/rebuild.rs +++ b/components/rebuild/tests/rebuild.rs @@ -2,7 +2,7 @@ extern crate rebuild; extern crate site; extern crate tempfile; extern crate fs_extra; - + use std::env; use std::fs::{remove_dir_all, File}; use std::io::prelude::*; @@ -79,7 +79,7 @@ fn can_rebuild_after_simple_change_to_page_content() { let file_path = edit_file!(site_path, "content/rebuild/first.md", br#" +++ title = "first" -order = 1 +weight = 1 date = 2017-01-01 +++ @@ -97,7 +97,7 @@ fn can_rebuild_after_title_change_page_global_func_usage() { let file_path = edit_file!(site_path, "content/rebuild/first.md", br#" +++ title = "Premier" -order = 10 +weight = 10 date = 2017-01-01 +++ @@ -115,12 +115,12 @@ fn can_rebuild_after_sort_change_in_section() { let file_path = edit_file!(site_path, "content/rebuild/_index.md", br#" +++ paginate_by = 1 -sort_by = "order" +sort_by = "weight" template = "rebuild.html" +++ "#); let res = after_content_change(&mut site, &file_path); assert!(res.is_ok()); - assert!(file_contains!(site_path, "public/rebuild/index.html", "

second

first

")); + assert!(file_contains!(site_path, "public/rebuild/index.html", "

first

second

")); } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index ce33384..85d1095 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -401,7 +401,7 @@ impl Site { } let pages = mem::replace(&mut section.pages, vec![]); let (sorted_pages, cannot_be_sorted_pages) = sort_pages(pages, section.meta.sort_by); - section.pages = populate_previous_and_next_pages(&sorted_pages); + section.pages = populate_previous_and_next_pages(&sorted_pages, section.meta.sort_by); section.ignored_pages = cannot_be_sorted_pages; } } diff --git a/test_site/content/posts/tutorials/devops/_index.md b/test_site/content/posts/tutorials/devops/_index.md index 8e99674..2922364 100644 --- a/test_site/content/posts/tutorials/devops/_index.md +++ b/test_site/content/posts/tutorials/devops/_index.md @@ -1,6 +1,6 @@ +++ title = "DevOps" -sort_by = "order" +sort_by = "weight" redirect_to = "posts/tutorials/devops/docker" weight = 10 +++ diff --git a/test_site/content/posts/tutorials/devops/docker.md b/test_site/content/posts/tutorials/devops/docker.md index ad19af5..46aea98 100644 --- a/test_site/content/posts/tutorials/devops/docker.md +++ b/test_site/content/posts/tutorials/devops/docker.md @@ -1,6 +1,6 @@ +++ title = "Docker" -order = 1 +weight = 1 date = 2017-01-01 +++ diff --git a/test_site/content/posts/tutorials/devops/nix.md b/test_site/content/posts/tutorials/devops/nix.md index 004ad58..bc80853 100644 --- a/test_site/content/posts/tutorials/devops/nix.md +++ b/test_site/content/posts/tutorials/devops/nix.md @@ -1,6 +1,6 @@ +++ title = "Nix" -order = 2 +weight = 2 date = 2017-01-01 +++ diff --git a/test_site/content/posts/tutorials/programming/_index.md b/test_site/content/posts/tutorials/programming/_index.md index 4a2c76f..6515620 100644 --- a/test_site/content/posts/tutorials/programming/_index.md +++ b/test_site/content/posts/tutorials/programming/_index.md @@ -1,5 +1,5 @@ +++ title = "Programming" -sort_by = "order" +sort_by = "weight" weight = 1 +++ diff --git a/test_site/content/posts/tutorials/programming/python.md b/test_site/content/posts/tutorials/programming/python.md index d65d52f..891ff29 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" -order = 1 +weight = 1 date = 2017-01-01 +++ diff --git a/test_site/content/posts/tutorials/programming/rust.md b/test_site/content/posts/tutorials/programming/rust.md index 3582c4b..a409b43 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" -order = 2 +weight = 2 date = 2017-01-01 +++ diff --git a/test_site/content/rebuild/_index.md b/test_site/content/rebuild/_index.md index c617948..46570a3 100644 --- a/test_site/content/rebuild/_index.md +++ b/test_site/content/rebuild/_index.md @@ -1,5 +1,5 @@ +++ paginate_by = 1 -sort_by = "order" +sort_by = "weight" template = "rebuild.html" +++ diff --git a/test_site/content/rebuild/first.md b/test_site/content/rebuild/first.md index a3bd65e..213fa4b 100644 --- a/test_site/content/rebuild/first.md +++ b/test_site/content/rebuild/first.md @@ -1,6 +1,6 @@ +++ title = "first" -order = 10 +weight = 10 date = 2017-01-01 +++ diff --git a/test_site/content/rebuild/second.md b/test_site/content/rebuild/second.md index 4a84919..2ee5601 100644 --- a/test_site/content/rebuild/second.md +++ b/test_site/content/rebuild/second.md @@ -1,6 +1,6 @@ +++ title = "second" -order = 100 +weight = 100 date = 2016-01-01 +++