Skip to content

Commit

Permalink
Merge pull request #220 from whostolemyhat/sort-order
Browse files Browse the repository at this point in the history
Allow posts to be reverse sorted
  • Loading branch information
epage committed May 11, 2017
2 parents dbcaf7e + 5c06950 commit 28df5e2
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
@@ -1,6 +1,6 @@
# ![Cobalt](https://raw.githubusercontent.com/cobalt-org/logos/master/cobald.logo.02.resize.png)
# ![Cobalt](https://raw.githubusercontent.com/cobalt-org/logos/master/cobald.logo.02.resize.png)
[![](https://img.shields.io/crates/v/cobalt-bin.svg?maxAge=25920)](https://crates.io/crates/cobalt-bin)
[![](https://travis-ci.org/cobalt-org/cobalt.rs.svg?branch=master)](https://travis-ci.org/cobalt-org/cobalt.rs) [![](https://ci.appveyor.com/api/projects/status/gp2mmvk8dpe8wsmi/branch/master?svg=true)](https://ci.appveyor.com/project/johannhof/cobalt-rs/branch/master)
[![](https://travis-ci.org/cobalt-org/cobalt.rs.svg?branch=master)](https://travis-ci.org/cobalt-org/cobalt.rs) [![](https://ci.appveyor.com/api/projects/status/gp2mmvk8dpe8wsmi/branch/master?svg=true)](https://ci.appveyor.com/project/johannhof/cobalt-rs/branch/master)
[![](https://coveralls.io/repos/cobalt-org/cobalt.rs/badge.svg?branch=master&service=github)](https://coveralls.io/github/cobalt-org/cobalt.rs?branch=master)
[![](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cobalt-org/cobalt.rs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

Expand Down Expand Up @@ -94,6 +94,8 @@ The ```extends``` attribute specifies which layout will be used.

The ```date``` attribute will be used to sort blog posts (from last to first). ```date``` must have the format `%dd %Mon %YYYY %HH:%MM:%SS %zzzz`, so for example `27 May 2016 21:00:30 +0100`.

You can change the order of posts from newest first (default) to oldest first by setting ```post_order: "asc"``` in your .cobalt.yml

#### Drafts

Cobalt supports leaving posts in "draft" state. Drafts will not be rendered unless Cobalt is run with the `--drafts` flag.
Expand Down Expand Up @@ -218,8 +220,8 @@ script:
- cobalt build

after_success: |
[ $TRAVIS_BRANCH = master ] &&
[ $TRAVIS_PULL_REQUEST = false ] &&
[ $TRAVIS_BRANCH = master ] &&
[ $TRAVIS_PULL_REQUEST = false ] &&
cobalt import &&
git config user.name "Cobalt Site Deployer" &&
git config user.email "name@example.com" &&
Expand Down
4 changes: 4 additions & 0 deletions src/cobalt.rs
Expand Up @@ -125,6 +125,10 @@ pub fn build(config: &Config) -> Result<()> {
// fall back to the default date
posts.sort_by(|a, b| b.date.unwrap_or(default_date).cmp(&a.date.unwrap_or(default_date)));

if &config.post_order == "asc" {
posts.reverse();
}

// collect all posts attributes to pass them to other posts for rendering
let simple_posts_data: Vec<Value> = posts.iter()
.map(|x| Value::Object(x.attributes.clone()))
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Expand Up @@ -15,6 +15,7 @@ pub struct Config {
pub include_drafts: bool,
pub posts: String,
pub post_path: Option<String>,
pub post_order: String,
pub template_extensions: Vec<String>,
pub rss: Option<String>,
pub name: Option<String>,
Expand All @@ -34,6 +35,7 @@ impl Default for Config {
include_drafts: false,
posts: "posts".to_owned(),
post_path: None,
post_order: "desc".to_owned(),
template_extensions: vec!["md".to_owned(), "liquid".to_owned()],
rss: None,
name: None,
Expand Down Expand Up @@ -89,6 +91,10 @@ impl Config {
config.posts = posts.to_owned();
};

if let Some(post_order) = yaml["post_order"].as_str() {
config.post_order = post_order.to_owned();
};

if let Some(extensions) = yaml["template_extensions"].as_vec() {
config.template_extensions = extensions.iter()
.filter_map(|k| k.as_str().map(|k| k.to_owned()))
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/post_order/.cobalt.yml
@@ -0,0 +1,7 @@
name: cobalt blog
source: "."
dest: "build"
post_order: "asc"
ignore:
- .git/*
- build/*
12 changes: 12 additions & 0 deletions tests/fixtures/post_order/_layouts/default.liquid
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>{{ path }}</h1>

{{ content }}
</body>
</html>

10 changes: 10 additions & 0 deletions tests/fixtures/post_order/_layouts/posts.liquid
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - {{ title }}</title>
</head>
<body>
{{ content }}
</body>
</html>

7 changes: 7 additions & 0 deletions tests/fixtures/post_order/index.liquid
@@ -0,0 +1,7 @@
extends: default.liquid
---
This is my Index page!

{% for post in posts %}
<a href="{{post.path}}">{{ post.title }}</a>
{% endfor %}
10 changes: 10 additions & 0 deletions tests/fixtures/post_order/posts/my-first-blogpost.md
@@ -0,0 +1,10 @@
extends: posts.liquid

title: My first Blogpost
date: 01 Jan 2016 21:00:00 +0100
---
# {{ title }}

Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
10 changes: 10 additions & 0 deletions tests/fixtures/post_order/posts/my-fourth-blogpost.md
@@ -0,0 +1,10 @@
extends: posts.liquid

title: My fourth Blogpost
date: 29 May 2016 23:00:00 +0100
---
# {{ title }}

Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
10 changes: 10 additions & 0 deletions tests/fixtures/post_order/posts/my-second-blogpost.md
@@ -0,0 +1,10 @@
extends: posts.liquid

title: My second Blogpost
date: 02 Jan 2016 10:00:00 +0100
---
# {{ title }}

Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
10 changes: 10 additions & 0 deletions tests/fixtures/post_order/posts/my-third-blogpost.md
@@ -0,0 +1,10 @@
extends: posts.liquid

title: My third Blogpost
date: 27 May 2016 23:00:00 +0100
---
# {{ title }}

Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
5 changes: 5 additions & 0 deletions tests/mod.rs
Expand Up @@ -175,6 +175,11 @@ pub fn sort_posts() {
run_test("sort_posts").expect("Build error");
}

#[test]
pub fn post_order() {
run_test("post_order").expect("Build error");
}

#[test]
pub fn previous_next() {
run_test("previous_next").expect("Build error");
Expand Down
23 changes: 23 additions & 0 deletions tests/target/post_order/index.html
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>index.html</h1>

This is my Index page!


<a href="posts/my-first-blogpost.html">My first Blogpost</a>

<a href="posts/my-second-blogpost.html">My second Blogpost</a>

<a href="posts/my-third-blogpost.html">My third Blogpost</a>

<a href="posts/my-fourth-blogpost.html">My fourth Blogpost</a>


</body>
</html>

13 changes: 13 additions & 0 deletions tests/target/post_order/posts/my-first-blogpost.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - My first Blogpost</title>
</head>
<body>
<h1>My first Blogpost</h1>
<p>Hey there this is my first blogpost and this is super awesome.</p>
<p>My Blog is lorem ipsum like, yes it is..</p>

</body>
</html>

13 changes: 13 additions & 0 deletions tests/target/post_order/posts/my-fourth-blogpost.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - My fourth Blogpost</title>
</head>
<body>
<h1>My fourth Blogpost</h1>
<p>Hey there this is my first blogpost and this is super awesome.</p>
<p>My Blog is lorem ipsum like, yes it is..</p>

</body>
</html>

13 changes: 13 additions & 0 deletions tests/target/post_order/posts/my-second-blogpost.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - My second Blogpost</title>
</head>
<body>
<h1>My second Blogpost</h1>
<p>Hey there this is my first blogpost and this is super awesome.</p>
<p>My Blog is lorem ipsum like, yes it is..</p>

</body>
</html>

13 changes: 13 additions & 0 deletions tests/target/post_order/posts/my-third-blogpost.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - My third Blogpost</title>
</head>
<body>
<h1>My third Blogpost</h1>
<p>Hey there this is my first blogpost and this is super awesome.</p>
<p>My Blog is lorem ipsum like, yes it is..</p>

</body>
</html>

0 comments on commit 28df5e2

Please sign in to comment.