Browse Source

Do not append non-sortables pages when not sortable

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
b256aaf7d0
9 changed files with 46 additions and 13 deletions
  1. +2
    -0
      CHANGELOG.md
  2. +37
    -4
      src/page.rs
  3. +1
    -1
      test_site/content/posts/tutorials/devops/_index.md
  4. +1
    -2
      test_site/content/posts/tutorials/devops/docker.md
  5. +1
    -2
      test_site/content/posts/tutorials/devops/nix.md
  6. +1
    -1
      test_site/content/posts/tutorials/programming/_index.md
  7. +1
    -1
      test_site/content/posts/tutorials/programming/python.md
  8. +1
    -1
      test_site/content/posts/tutorials/programming/rust.md
  9. +1
    -1
      tests/site.rs

+ 2
- 0
CHANGELOG.md View File

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



+ 37
- 4
src/page.rs View File

@@ -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<Page>, section: Option<&Section>) -> Vec<Page> {
let sort_by = if let Some(ref sec) = section {
sec.meta.sort_by()
@@ -260,7 +262,7 @@ pub fn sort_pages(pages: Vec<Page>, section: Option<&Section>) -> Vec<Page> {
}
}
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<Page>, section: Option<&Section>) -> Vec<Page> {
}
}
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(&section));
// 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(&section));
assert_eq!(pages.len(), 2);
}

#[test]
fn test_populate_previous_and_next_pages() {
let input = vec![


+ 1
- 1
test_site/content/posts/tutorials/devops/_index.md View File

@@ -1,4 +1,4 @@
+++
title = "DevOps"
description = ""
sort_by = "order"
+++

+ 1
- 2
test_site/content/posts/tutorials/devops/docker.md View File

@@ -1,7 +1,6 @@
+++
title = "Docker"
description = ""
date = "2017-02-01"
order = 1
+++

A simple page

+ 1
- 2
test_site/content/posts/tutorials/devops/nix.md View File

@@ -1,7 +1,6 @@
+++
title = "Nix"
description = ""
date = "2017-03-01"
order = 2
+++

A simple page

+ 1
- 1
test_site/content/posts/tutorials/programming/_index.md View File

@@ -1,4 +1,4 @@
+++
title = "Programming"
description = ""
sort_by = "order"
+++

+ 1
- 1
test_site/content/posts/tutorials/programming/python.md View File

@@ -1,6 +1,6 @@
+++
title = "Python tutorial"
description = ""
order = 1
+++

A simple page

+ 1
- 1
test_site/content/posts/tutorials/programming/rust.md View File

@@ -1,6 +1,6 @@
+++
title = "Rust"
description = ""
order = 2
+++

A simple page

+ 1
- 1
tests/site.rs View File

@@ -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);


Loading…
Cancel
Save