From c7bea0bc54629d45127d253e0b8fb2781c36b694 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 29 Jun 2017 16:11:15 +0900 Subject: [PATCH] Add weight sorting --- Cargo.lock | 6 +++--- README.md | 2 +- src/content/sorting.rs | 23 +++++++++++++++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a1f6b6..2fe5bf6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,7 +12,7 @@ dependencies = [ "mount 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.0.15 (registry+https://github.com/rust-lang/crates.io-index)", - "rayon 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -666,7 +666,7 @@ dependencies = [ [[package]] name = "rayon" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rayon-core 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1194,7 +1194,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum pulldown-cmark 0.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "378e941dbd392c101f2cb88097fa4d7167bc421d4b88de3ff7dbee503bc3233b" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" -"checksum rayon 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "705cf28d52a26a9ab548930a9a3d9799eb77cf84d66d7cc6e52fa222ca662424" +"checksum rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b614fe08b6665cb9a231d07ac1364b0ef3cb3698f1239ee0c4c3a88a524f54c8" "checksum rayon-core 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7febc28567082c345f10cddc3612c6ea020fc3297a1977d472cf9fdb73e6e493" "checksum redox_syscall 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "3041aeb6000db123d2c9c751433f526e1f404b23213bd733167ab770c3989b4d" "checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b" diff --git a/README.md b/README.md index d64f980..83ddabc 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ You can also set the `template` variable to change which template will be used t Sections will also automatically pick up their subsections, allowing you to make some complex pages layout and table of contents. -You can define how a section pages are sorted using the `sort_by` key in the front-matter. The choices are `date`, `order` +You can define how a section pages are sorted using the `sort_by` key in the front-matter. The choices are `date`, `order`, `weight` (opposite of order) and `none` (default). Pages that can't be sorted will currently be silently dropped: the final page will be rendered but it will not appear in the `pages` variable in the section template. diff --git a/src/content/sorting.rs b/src/content/sorting.rs index 68a933c..5238d44 100644 --- a/src/content/sorting.rs +++ b/src/content/sorting.rs @@ -5,6 +5,7 @@ use content::Page; pub enum SortBy { Date, Order, + Weight, None, } @@ -28,7 +29,7 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { (can_be_sorted, cannot_be_sorted) }, - SortBy::Order => { + SortBy::Order | SortBy::Weight => { let mut can_be_sorted = vec![]; let mut cannot_be_sorted = vec![]; for page in pages { @@ -38,7 +39,11 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { cannot_be_sorted.push(page); } } - can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order())); + if sort_by == SortBy::Order { + can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order())); + } else { + can_be_sorted.sort_by(|a, b| a.meta.order().cmp(&b.meta.order())); + } (can_be_sorted, cannot_be_sorted) }, @@ -126,6 +131,20 @@ mod tests { assert_eq!(pages[2].clone().meta.order.unwrap(), 1); } + #[test] + fn can_sort_by_weight() { + let input = vec![ + create_page_with_order(2), + create_page_with_order(3), + create_page_with_order(1), + ]; + let (pages, _) = sort_pages(input, SortBy::Weight); + // Should be sorted by date + assert_eq!(pages[0].clone().meta.order.unwrap(), 1); + assert_eq!(pages[1].clone().meta.order.unwrap(), 2); + assert_eq!(pages[2].clone().meta.order.unwrap(), 3); + } + #[test] fn can_sort_by_none() { let input = vec![