Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter unpublished pages early #12

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ pub struct Generator {
lookup_tree: BTreeMap<Date, Page<Properties>>,
article_pages: Vec<(String, Page<Properties>)>,
downloadables: Downloadables,
today: Date,
head: Markup,
header: Markup,
footer: Markup,
Expand All @@ -181,8 +180,18 @@ impl Generator {
pub async fn new<P: AsRef<Path>>(dir: P, pages: Vec<Page<Properties>>) -> Result<Generator> {
let length = pages.len();

let today = time::OffsetDateTime::now_utc().date();

let (link_map, lookup_tree, article_pages) = pages
.into_iter()
.filter(|page| {
page.properties
.published
.date
.as_ref()
.map(|date| date.start <= today)
.unwrap_or(false)
})
.map(|page| {
let date = page
.properties
Expand All @@ -201,10 +210,9 @@ impl Generator {
),
(Some(Either::Left(date)), Some(url)) => bail!("Diary currently doesn't support rendering a page with both a date and a URL but page {} has date {} and URL {}", page.id, date, url),
(None, None) => bail!("Diary pages must have either a date or a URL"),
(Some(Either::Left(date)), None) => (
format_day(date, true),
Either::Left(date),
),
(Some(Either::Left(date)), None) => {
(format_day(date, true), Either::Left(date))
}
(None, Some(url)) => (format!("/{}", url), Either::Right(url)),
};

Expand All @@ -230,8 +238,6 @@ impl Generator {
},
)?;

let today = time::OffsetDateTime::now_utc().date();

let read_config_file = async {
tokio::fs::File::open("config.json")
.await
Expand Down Expand Up @@ -265,7 +271,6 @@ impl Generator {
link_map,
lookup_tree,
article_pages,
today,
head,
header,
footer,
Expand Down Expand Up @@ -293,15 +298,6 @@ impl Generator {
}
}

fn filter_unpublished(&self, page: &Page<Properties>) -> bool {
page.properties
.published
.date
.as_ref()
.map(|date| date.start <= self.today)
.unwrap_or(false)
}

fn render_article<I>(
&self,
renderer: &HtmlRenderer,
Expand Down Expand Up @@ -359,7 +355,6 @@ impl Generator {

let (current_pages, pages) = range
.map(|(_, page)| page)
.filter(|page| self.filter_unpublished(page))
.map(|page| (page.id, page))
.unzip::<_, _, HashSet<_>, Vec<_>>();

Expand Down Expand Up @@ -458,7 +453,6 @@ impl Generator {

let (current_pages, pages) = range
.map(|(_, page)| page)
.filter(|page| self.filter_unpublished(page))
.map(|page| (page.id, page))
.unzip::<_, _, HashSet<_>, Vec<_>>();

Expand Down Expand Up @@ -540,7 +534,6 @@ impl Generator {
let days = self
.lookup_tree
.iter()
.filter(|(_, page)| self.filter_unpublished(page))
.map(|(date, page)| {
let renderer = HtmlRenderer {
heading_anchors: HeadingAnchors::After("#"),
Expand All @@ -567,11 +560,11 @@ impl Generator {
.lookup_tree
.range((Bound::Unbounded, Bound::Excluded(date)))
.rev()
.find(|(_, page)| self.filter_unpublished(page));
.next();
let next_page = self
.lookup_tree
.range((Bound::Excluded(date), Bound::Unbounded))
.find(|(_, page)| self.filter_unpublished(page));
.next();

let cover = self.download_cover(page)?;
let path = format_day(*date, false);
Expand Down Expand Up @@ -659,7 +652,6 @@ impl Generator {
let years = self
.lookup_tree
.iter()
.filter(|(_, page)| self.filter_unpublished(page))
.rev()
.map(|(&date, page)| IndexMonth {
month: (date.year(), date.month()),
Expand Down Expand Up @@ -791,7 +783,6 @@ impl Generator {
let articles = self
.article_pages
.iter()
.filter(|(_, page)| self.filter_unpublished(page))
.map(|(url, page)| {
let renderer = HtmlRenderer {
heading_anchors: HeadingAnchors::After("#"),
Expand Down Expand Up @@ -889,7 +880,7 @@ impl Generator {
let published_date = page.properties.published.date.as_ref().map(get_date);

let published_date = match published_date {
Some(published_date) if self.filter_unpublished(page) => published_date,
Some(published_date) => published_date,
_ => return None,
};

Expand Down
37 changes: 37 additions & 0 deletions tests/bugs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
mod utils;

use diary_generator::{Generator, Properties};
use notion_generator::response::{properties::DateProperty, Page};
use utils::{function, new_page, TestDir};

#[tokio::test]
async fn unpublished_pages_dont_cause_crashes() {
let cwd = TestDir::new(function!());

let page = new_page(
"ac3fb543-001f-4be5-a25e-4978abd05b1d",
"unpublished page with no date",
"just a page without a publish date yet",
None,
);

Generator::new(
&cwd,
vec![Page {
properties: Properties {
date: DateProperty {
date: None,
..page.properties.date
},
published: DateProperty {
date: None,
..page.properties.published
},
..page.properties
},
..page
}],
)
.await
.unwrap();
}
22 changes: 11 additions & 11 deletions tests/index_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,32 @@ async fn simple_index() {
new_page(
"cf2bacc9d75c4226aab53601c336f295",
"Day 0: Nannou, helping L, and lots of noise",
Time {
original: "2021-11-07".to_string(),
parsed: Either::Left(date!(2021 - 11 - 07)),
},
"Every journey starts with 1 O'clock: assistance. \
I just didn't know mine will also start with noise.",
Some(Time {
original: "2021-11-07".to_string(),
parsed: Either::Left(date!(2021 - 11 - 07)),
}),
),
new_page(
"ac3fb543001f4be5a25e4978abd05b1d",
"Day 1: Down the rabbit hole we go",
Time {
"Alice starts making games by watching trains with the loveliest coding conductor.",
Some(Time {
original: "2021-11-08".to_string(),
parsed: Either::Left(date!(2021 - 11 - 08)),
},
"Alice starts making games by watching trains with the loveliest coding conductor.",
}),
),
new_page(
"ac3fb543001f4be5a25e4978abd05b1d",
"Day 2: Enter Bevy & Shaders are hard",
Time {
original: "2021-11-09".to_string(),
parsed: Either::Left(date!(2021 - 11 - 09)),
},
"3 O’clock: departure. \
We are not entering the world of Bevy where we will actually make things happen. \
There’s no turning back now",
Some(Time {
original: "2021-11-09".to_string(),
parsed: Either::Left(date!(2021 - 11 - 09)),
}),
),
],
)
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use notion_generator::response::{
};
use time::macros::date;

pub fn new(id: &str, title: &str, date: Time, description: &str) -> Page<Properties> {
pub fn new(id: &str, title: &str, description: &str, date: Option<Time>) -> Page<Properties> {
Page {
object: "page".to_string(),
id: id.parse().unwrap(),
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn new(id: &str, title: &str, date: Time, description: &str) -> Page<Propert
},
date: DateProperty {
id: "TKGl".to_string(),
date: Some(NotionDate {
date: date.map(|date| NotionDate {
start: date,
end: None,
time_zone: None,
Expand Down