Skip to content

Commit de9c9d6

Browse files
authored
Merge pull request #1241 from epage/page
fix(page): Don't panic on empty all
2 parents d07deb4 + 7a1ade1 commit de9c9d6

File tree

26 files changed

+400
-15
lines changed

26 files changed

+400
-15
lines changed

src/pagination/all.rs

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ pub(crate) fn create_all_paginators(
1414
// f32 used here in order to not lose information to ceil the result,
1515
// otherwise we can lose an index
1616
let total_indexes = (total_pages as f32 / config.per_page as f32).ceil() as usize;
17+
18+
if total_pages == 0 {
19+
return Ok(vec![paginator::create_paginator(
20+
0,
21+
total_indexes,
22+
total_pages,
23+
config,
24+
doc,
25+
&[],
26+
index_title,
27+
)?]);
28+
}
29+
1730
let paginators: Result<Vec<_>> = all_posts
1831
.chunks(config.per_page as usize)
1932
.enumerate()

src/pagination/tags.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,22 @@ fn walk_tags(
5050
doc: &Document,
5151
) -> Result<Vec<Paginator>> {
5252
// create all other paginators
53-
let mut tag_paginators: TagPaginators = per_tags
54-
.iter_mut()
55-
.try_fold(TagPaginators::default(), |mut acc, (tag, posts)| {
56-
sort_posts(posts, config);
57-
let cur_tag_paginators = all::create_all_paginators(
58-
posts,
59-
doc,
60-
config,
61-
Some(&liquid::model::Value::scalar(tag.to_owned())),
62-
)?;
63-
acc.firsts_of_tags.push(cur_tag_paginators[0].clone());
64-
acc.paginators.extend(cur_tag_paginators.into_iter());
65-
Ok(acc)
66-
})
67-
.or_else(std::result::Result::<_, anyhow::Error>::Err)?;
53+
let mut tag_paginators = TagPaginators::default();
54+
for (tag, posts) in per_tags.iter_mut() {
55+
sort_posts(posts, config);
56+
let cur_tag_paginators = all::create_all_paginators(
57+
posts,
58+
doc,
59+
config,
60+
Some(&liquid::model::Value::scalar(tag.to_owned())),
61+
)?;
62+
tag_paginators
63+
.firsts_of_tags
64+
.push(cur_tag_paginators[0].clone());
65+
tag_paginators
66+
.paginators
67+
.extend(cur_tag_paginators.into_iter());
68+
}
6869

6970
tag_paginators.firsts_of_tags.sort_unstable_by_key(|p| {
7071
p.index_title
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
syntax_highlight:
2+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1></h1>
8+
9+
This is my Index page!
10+
11+
12+
13+
14+
<div>
15+
16+
17+
18+
<span>1 / 0</span>
19+
</div>
20+
21+
</body>
22+
</html>
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>{{ page.permalink }}</h1>
8+
9+
{{ page.content }}
10+
</body>
11+
</html>
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My blog - {{ page.title }}</title>
5+
</head>
6+
<body>
7+
{{ page.content }}
8+
</body>
9+
</html>
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
permalink: /
3+
layout: default.liquid
4+
pagination:
5+
include: All
6+
---
7+
This is my Index page!
8+
9+
{% for page in paginator.pages %}
10+
<a href="{{page.permalink}}">{{ page.title }}</a>
11+
{% endfor %}
12+
13+
14+
<div>
15+
{% if paginator.previous_index %}
16+
<a href="/{{ paginator.previous_index_permalink }}"
17+
class="left arrow">&#8592;</a>
18+
{% endif %}
19+
{% if paginator.next_index %}
20+
<a href="/{{ paginator.next_index_permalink }}"
21+
class="right arrow">&#8594;</a>
22+
{% endif %}
23+
24+
<span>{{ paginator.index }} / {{ paginator.total_indexes }}</span>
25+
</div>

tests/cmd/pagination_all_empty.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```console
2+
$ cobalt -v build --destination _dest
3+
DEBUG: Using config file `./_cobalt.yml`
4+
Building from `.` into `[CWD]/_dest`
5+
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
6+
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
7+
DEBUG: built glob set; 5 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
8+
DEBUG: Loading data from `./_data`
9+
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
10+
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
11+
DEBUG: built glob set; 0 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
12+
DEBUG: Loading snippets from `./_includes`
13+
Build successful
14+
15+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
syntax_highlight:
2+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>categories.html</h1>
8+
9+
10+
11+
12+
13+
14+
</body>
15+
</html>
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>{{ page.permalink }}</h1>
8+
9+
{{ page.content }}
10+
</body>
11+
</html>
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My blog - {{ page.title }}</title>
5+
</head>
6+
<body>
7+
{{ page.content }}
8+
</body>
9+
</html>
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
permalink: /categories.html
3+
layout: default.liquid
4+
pagination:
5+
include: Categories
6+
permalink_suffix: ./_p/{{num}}
7+
---
8+
{% if paginator.index_title %}
9+
<div>Category: {% for category in paginator.index_title %}{{ category | prepend: "/" }}{% endfor %}</div>
10+
{% if paginator.indexes %}
11+
<div>Sub-categories:</div>
12+
{% endif %}
13+
{% endif %}
14+
{% if paginator.indexes %}
15+
<ul>
16+
{% for pcat in paginator.indexes %}
17+
<li><a href="/{{ pcat.index_permalink }}/">{{ pcat.index_title | last }} ({{ pcat.total_pages }})</a>
18+
{% endfor %}
19+
</ul>
20+
{% endif %}
21+
22+
{% if paginator.pages %}
23+
<div>Posts in this category:</div>
24+
<ul>
25+
{% for page in paginator.pages %}
26+
<li><a href="/{{ page.permalink }}">{{ page.title }}</a>
27+
{% endfor %}
28+
</ul>
29+
<div>
30+
{% if paginator.previous_index %}
31+
<a href="{{ paginator.previous_index_permalink }}"
32+
class="left arrow">&#8592;</a>
33+
{% endif %}
34+
{% if paginator.next_index %}
35+
<a href="{{ paginator.next_index_permalink }}"
36+
class="right arrow">&#8594;</a>
37+
{% endif %}
38+
39+
<span>{{ paginator.index }} / {{ paginator.total_indexes }}</span>
40+
</div>
41+
{% endif %}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```console
2+
$ cobalt -v build --destination _dest
3+
DEBUG: Using config file `./_cobalt.yml`
4+
Building from `.` into `[CWD]/_dest`
5+
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
6+
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
7+
DEBUG: built glob set; 5 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
8+
DEBUG: Loading data from `./_data`
9+
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
10+
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
11+
DEBUG: built glob set; 0 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
12+
DEBUG: Loading snippets from `./_includes`
13+
Build successful
14+
15+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
syntax_highlight:
2+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>dates.html</h1>
8+
9+
10+
11+
12+
13+
14+
</body>
15+
</html>
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>{{ page.permalink }}</h1>
8+
9+
{{ page.content }}
10+
</body>
11+
</html>
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>My blog - {{ page.title }}</title>
5+
</head>
6+
<body>
7+
{{ page.content }}
8+
</body>
9+
</html>
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
permalink: /dates.html
3+
layout: default.liquid
4+
pagination:
5+
include: Dates
6+
permalink_suffix: ./_p/{{num}}
7+
---
8+
{% if paginator.index_title %}
9+
{% capture date_title %}{% for field in paginator.index_title %}{{ field | prepend: "-" }}{% endfor %}{% endcapture %}
10+
{% capture date_clean %}{{ date_title | remove_first: "-" }}{% endcapture %}
11+
<div>Date: {{ date_clean | date: "%Y/%B" }}</div>
12+
{% if paginator.indexes %}
13+
<div>Sub-dates:</div>
14+
{% endif %}
15+
{% endif %}
16+
{% if paginator.indexes %}
17+
All years:
18+
<ul>
19+
{% for pdate in paginator.indexes %}
20+
<li><a href="/{{ pdate.index_permalink }}/">{{ pdate.index_title | last }} ({{ pdate.total_pages }})</a>
21+
{% endfor %}
22+
</ul>
23+
{% endif %}
24+
25+
{% if paginator.pages %}
26+
<div>Posts in this date:</div>
27+
<ul>
28+
{% for page in paginator.pages %}
29+
<li><a href="/{{ page.permalink }}">{{ page.title }}</a>
30+
{% endfor %}
31+
</ul>
32+
<div>
33+
{% if paginator.previous_index %}
34+
<a href="{{ paginator.previous_index_permalink }}"
35+
class="left arrow">&#8592;</a>
36+
{% endif %}
37+
{% if paginator.next_index %}
38+
<a href="{{ paginator.next_index_permalink }}"
39+
class="right arrow">&#8594;</a>
40+
{% endif %}
41+
42+
<span>{{ paginator.index }} / {{ paginator.total_indexes }}</span>
43+
</div>
44+
{% endif %}

tests/cmd/pagination_dates_empty.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```console
2+
$ cobalt -v build --destination _dest
3+
DEBUG: Using config file `./_cobalt.yml`
4+
Building from `.` into `[CWD]/_dest`
5+
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
6+
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
7+
DEBUG: built glob set; 5 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
8+
DEBUG: Loading data from `./_data`
9+
DEBUG: glob converted to regex: Glob { glob: "**/.*", re: "(?-u)^(?:/?|.*/)//.[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('.'), ZeroOrMore]) }
10+
DEBUG: glob converted to regex: Glob { glob: "**/_*", re: "(?-u)^(?:/?|.*/)_[^/]*$", opts: GlobOptions { case_insensitive: false, literal_separator: true, backslash_escape: true, empty_alternates: false }, tokens: Tokens([RecursivePrefix, Literal('_'), ZeroOrMore]) }
11+
DEBUG: built glob set; 0 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 2 regexes
12+
DEBUG: Loading snippets from `./_includes`
13+
Build successful
14+
15+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
syntax_highlight:
2+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>tags</h1>
8+
9+
10+
<ul>
11+
12+
</ul>
13+
14+
15+
</body>
16+
</html>
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>test</title>
5+
</head>
6+
<body>
7+
<h1>{{ page.permalink }}</h1>
8+
9+
{{ page.content }}
10+
</body>
11+
</html>
12+

0 commit comments

Comments
 (0)