Add weight sorting

This commit is contained in:
Vincent Prouillet 2017-06-29 16:11:15 +09:00
parent 7768a0cc12
commit c7bea0bc54
3 changed files with 25 additions and 6 deletions

6
Cargo.lock generated
View file

@ -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"

View file

@ -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.

View file

@ -5,6 +5,7 @@ use content::Page;
pub enum SortBy {
Date,
Order,
Weight,
None,
}
@ -28,7 +29,7 @@ pub fn sort_pages(pages: Vec<Page>, sort_by: SortBy) -> (Vec<Page>, Vec<Page>) {
(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<Page>, sort_by: SortBy) -> (Vec<Page>, Vec<Page>) {
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![