Skip to content

Commit

Permalink
feat(liquid): Generalize page.is_post to page.collection
Browse files Browse the repository at this point in the history
`page.collection` contains the collection slug which is `posts` for,
well, the built-in posts collection.

This is a step towards cobalt-org#323

BREAKING CHANGE: `page.is_post` is replaced by `page.collection`

Generally, it'll look like
`{% if page.is_post %}`
and can be replaced by
`{% if page.collection == "posts" %}`

though `cobalt migrate` should do this for you.
  • Loading branch information
epage committed Dec 30, 2017
1 parent 3da1d12 commit 2dcd57d
Show file tree
Hide file tree
Showing 28 changed files with 56 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/bin/cobalt/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn migrate_variables(content: String) -> Result<String> {
(r#"\{\{\s*content\s*}}"#, "{{ page.content }}"),
(r#"\{\{\s*previous\.(.*?)\s*}}"#, "{{ page.previous.$1 }}"),
(r#"\{\{\s*next\.(.*?)\s*}}"#, "{{ page.next.$1 }}"),
(r#"\{%\s*if\s+is_post\s*%}"#, "{% if page.is_post %}"),
(r#"\{%\s*if\s+is_post\s*%}"#, r#"{% if page.collection == "posts" %}"#),
(r#"\{%\s*if\s+previous\s*%}"#, "{% if page.previous %}"),
(r#"\{%\s*if\s+next\s*%}"#, "{% if page.next %}"),
(r#"\{%\s*if\s+draft\s*%}"#, "{% if page.is_draft %}"),
Expand Down
4 changes: 3 additions & 1 deletion src/cobalt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ pub fn build(config: &Config) -> Result<()> {
let default_date = cobalt_model::DateTime::default();

let (mut posts, documents): (Vec<Document>, Vec<Document>) =
documents.into_iter().partition(|x| x.front.is_post);
documents
.into_iter()
.partition(|x| x.front.collection == "posts");

// sort documents by date, if there's no date (none was provided or it couldn't be read) then
// fall back to the default date
Expand Down
2 changes: 2 additions & 0 deletions src/cobalt_model/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ impl CollectionBuilder {
attributes.insert("jsonfeed".to_owned(), liquid::Value::str(jsonfeed));
}

let default = default.set_collection(slug.clone());

let new = Collection {
title,
slug,
Expand Down
7 changes: 2 additions & 5 deletions src/cobalt_model/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ pub struct PageBuilder {

impl From<PageBuilder> for collection::CollectionBuilder {
fn from(config: PageBuilder) -> Self {
let slug = Some("pages".to_owned());
// Pages aren't publicly exposed as a collection
let slug = Some("".to_owned());
let dir = Some(".".to_owned());
let default = config.default.merge_excerpt_separator("".to_owned());
collection::CollectionBuilder {
Expand Down Expand Up @@ -105,10 +106,6 @@ impl From<PostBuilder> for collection::CollectionBuilder {
default,
} = config;

// HACK: We shouldn't allow a user to set this in the first place but too lazy to setup
// error reporting in here.
let default = default.set_post(true);

let slug = Some("posts".to_owned());
collection::CollectionBuilder {
title,
Expand Down
45 changes: 22 additions & 23 deletions src/cobalt_model/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ pub struct FrontmatterBuilder {
pub layout: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_draft: Option<bool>,
#[serde(skip_serializing_if = "liquid::Object::is_empty")]
pub data: liquid::Object,
// Controlled by where the file is found. We might allow control over the type at a later
// point but we need to first define those semantics.
#[serde(skip)]
pub is_post: Option<bool>,
#[serde(skip_serializing_if = "liquid::Object::is_empty")]
pub data: liquid::Object,
pub collection: Option<String>,
}

impl FrontmatterBuilder {
Expand Down Expand Up @@ -172,9 +172,9 @@ impl FrontmatterBuilder {
}
}

pub fn set_post<B: Into<Option<bool>>>(self, is_post: B) -> Self {
pub fn set_collection<S: Into<Option<String>>>(self, collection: S) -> Self {
Self {
is_post: is_post.into(),
collection: collection.into(),
..self
}
}
Expand Down Expand Up @@ -227,8 +227,8 @@ impl FrontmatterBuilder {
}

#[cfg(test)]
pub fn merge_post<B: Into<Option<bool>>>(self, post: B) -> Self {
self.merge(Self::new().set_post(post.into()))
pub fn merge_collection<S: Into<Option<String>>>(self, collection: S) -> Self {
self.merge(Self::new().set_collection(collection.into()))
}

pub fn merge_data(self, other_data: liquid::Object) -> Self {
Expand All @@ -244,7 +244,7 @@ impl FrontmatterBuilder {
format,
layout,
is_draft,
is_post,
collection,
data,
} = self;
Self {
Expand All @@ -259,7 +259,7 @@ impl FrontmatterBuilder {
format: format,
layout: layout,
is_draft: is_draft,
is_post: is_post,
collection: collection,
data: merge_objects(data, other_data),
}
}
Expand All @@ -277,7 +277,7 @@ impl FrontmatterBuilder {
format,
layout,
is_draft,
is_post,
collection,
data,
} = self;
let Self {
Expand All @@ -292,7 +292,7 @@ impl FrontmatterBuilder {
format: other_format,
layout: other_layout,
is_draft: other_is_draft,
is_post: other_is_post,
collection: other_collection,
data: other_data,
} = other;
Self {
Expand All @@ -307,7 +307,7 @@ impl FrontmatterBuilder {
format: format.or_else(|| other_format),
layout: layout.or_else(|| other_layout),
is_draft: is_draft.or_else(|| other_is_draft),
is_post: is_post.or_else(|| other_is_post),
collection: collection.or_else(|| other_collection),
data: merge_objects(data, other_data),
}
}
Expand Down Expand Up @@ -362,11 +362,11 @@ impl FrontmatterBuilder {
format,
layout,
is_draft,
is_post,
collection,
data,
} = self;

let is_post = is_post.unwrap_or(false);
let collection = collection.unwrap_or_else(|| "".to_owned());

let permalink = permalink.unwrap_or_else(|| PATH_ALIAS.to_owned());
let permalink = if !permalink.starts_with('/') {
Expand All @@ -391,7 +391,7 @@ impl FrontmatterBuilder {
format: format.unwrap_or_else(SourceFormat::default),
layout: layout,
is_draft: is_draft.unwrap_or(false),
is_post: is_post,
collection: collection,
data: data,
};

Expand Down Expand Up @@ -423,8 +423,7 @@ pub struct Frontmatter {
pub format: SourceFormat,
pub layout: Option<String>,
pub is_draft: bool,
#[serde(skip)]
pub is_post: bool,
pub collection: String,
pub data: liquid::Object,
}

Expand Down Expand Up @@ -634,7 +633,7 @@ mod test {
format: Some(SourceFormat::Markdown),
layout: Some("layout a".to_owned()),
is_draft: Some(true),
is_post: Some(false),
collection: Some("pages".to_owned()),
data: liquid::Object::new(),
};
let b = FrontmatterBuilder {
Expand All @@ -649,7 +648,7 @@ mod test {
format: Some(SourceFormat::Raw),
layout: Some("layout b".to_owned()),
is_draft: Some(true),
is_post: Some(false),
collection: Some("posts".to_owned()),
data: liquid::Object::new(),
};

Expand Down Expand Up @@ -677,7 +676,7 @@ mod test {
format: Some(SourceFormat::Markdown),
layout: Some("layout a".to_owned()),
is_draft: Some(true),
is_post: Some(false),
collection: Some("pages".to_owned()),
data: liquid::Object::new(),
};

Expand All @@ -692,7 +691,7 @@ mod test {
.merge_format(SourceFormat::Raw)
.merge_layout("layout b".to_owned())
.merge_draft(true)
.merge_post(false);
.merge_collection("posts".to_owned());
assert_eq!(merge_b_into_a, a);

let merge_empty_into_a = a.clone()
Expand All @@ -706,7 +705,7 @@ mod test {
.merge_format(None)
.merge_layout(None)
.merge_draft(None)
.merge_post(None);
.merge_collection(None);
assert_eq!(merge_empty_into_a, a);

let merge_a_into_empty = FrontmatterBuilder::new()
Expand All @@ -720,7 +719,7 @@ mod test {
.merge_format(SourceFormat::Markdown)
.merge_layout("layout a".to_owned())
.merge_draft(true)
.merge_post(false);
.merge_collection("pages".to_owned());
assert_eq!(merge_a_into_empty, a);
}

Expand Down
3 changes: 1 addition & 2 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,14 @@ fn document_attributes(front: &cobalt_model::Frontmatter,
("categories".to_owned(), categories),
("is_draft".to_owned(), liquid::Value::Bool(front.is_draft)),
("file".to_owned(), liquid::Value::Object(file)),
("collection".to_owned(), liquid::Value::str(&front.collection)),
("data".to_owned(), liquid::Value::Object(front.data.clone()))];
let mut attributes: liquid::Object = attributes.into_iter().collect();

if let Some(ref published_date) = front.published_date {
attributes.insert("published_date".to_owned(),
liquid::Value::Str(published_date.format()));
}
// TODO(epage): Provide a way to determine tbis
attributes.insert("is_post".to_owned(), liquid::Value::Bool(front.is_post));

attributes
}
Expand Down
6 changes: 2 additions & 4 deletions src/legacy_model/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ impl From<GlobalConfig> for cobalt_model::ConfigBuilder {

let default = cobalt_model::FrontmatterBuilder::new()
.set_excerpt_separator(excerpt_separator)
.set_draft(false)
.set_post(false);
.set_draft(false);
let posts = cobalt_model::PostBuilder {
title: None,
description: None,
Expand All @@ -143,8 +142,7 @@ impl From<GlobalConfig> for cobalt_model::ConfigBuilder {
rss: rss,
jsonfeed: jsonfeed,
default: cobalt_model::FrontmatterBuilder::new()
.set_permalink(post_path.map(convert_permalink))
.set_post(true),
.set_permalink(post_path.map(convert_permalink)),
};

let site = cobalt_model::SiteBuilder {
Expand Down
8 changes: 2 additions & 6 deletions tests/fixtures/excerpts/_layouts/default.liquid
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<!DOCTYPE html>
<html>
<head>
{% if page.is_post %}
<title>{{ page.title }}</title>
{% else %}
<title>Cobalt.rs Blog</title>
{% endif %}
<title>{{ page.title }}</title>
</head>
<body>
<div>
{% if page.is_post %}
{% if page.collection == "posts" %}
{% include 'post.liquid' %}
{% else %}
{{ page.content }}
Expand Down
8 changes: 2 additions & 6 deletions tests/fixtures/posts_in_subfolder/_layouts/default.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
<html>
<!-- start default.liquid -->
<head>
{% if page.is_post %}
<title>{{ page.title }}</title>
{% else %}
<title>Cobalt.rs Blog</title>
{% endif %}
<title>{{ page.title }}</title>
</head>
<body>
<div>
{% if page.is_post %}
{% if page.collection == "posts" %}
{% include 'post.liquid' %}
{% else %}
{{ page.content }}
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Cobalt.rs Blog</title>

<title>Index</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-1.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>First block is an excerpt</title>

<title>First block is an excerpt</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-10.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Reference-style links immediately above first block</title>

<title>Reference-style links immediately above first block</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-11.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Reference-style links immediately below first block do not resolve</title>

<title>Reference-style links immediately below first block do not resolve</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-12.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Reference-style links in a non-markdown post do not resolve</title>

<title>Reference-style links in a non-markdown post do not resolve</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-2.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>An empty post means an empty excerpt</title>

<title>An empty post means an empty excerpt</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-3.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Explicit `excerpt`</title>

<title>Explicit `excerpt`</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-4.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Custom excerpt separator</title>

<title>Custom excerpt separator</title>
</head>
<body>
<div>
Expand Down
4 changes: 1 addition & 3 deletions tests/target/excerpts/posts/post-5.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<!DOCTYPE html>
<html>
<head>

<title>Both excerpt and excerpt separator are there</title>

<title>Both excerpt and excerpt separator are there</title>
</head>
<body>
<div>
Expand Down
Loading

0 comments on commit 2dcd57d

Please sign in to comment.