Posts in subdirs (#185)

This commit is contained in:
Uwe Arzt 2017-02-09 23:50:09 +01:00 committed by Johann Hofmann
parent 788cdd3f38
commit 291229bffb
17 changed files with 309 additions and 1 deletions

View file

@ -33,6 +33,14 @@ fn compare_paths(a: &Path, b: &Path) -> bool {
}
}
/// Checks if one path is the starting point of another path.
fn starts_with_path(this: &Path, starts_with: &Path) -> bool {
match (fs::canonicalize(this), fs::canonicalize(starts_with)) {
(Ok(p), Ok(p2)) => p.starts_with(p2),
_ => false,
}
}
/// The primary build function that transforms a directory into a site
pub fn build(config: &Config) -> Result<()> {
trace!("Build configuration: {:?}", config);
@ -72,7 +80,7 @@ pub fn build(config: &Config) -> Result<()> {
if template_extensions.contains(extension) {
// if the document is in the posts folder it's considered a post
let is_post =
entry_path.parent().map(|p| compare_paths(p, &posts_path)).unwrap_or(false);
entry_path.parent().map(|p| starts_with_path(p, &posts_path)).unwrap_or(false);
let new_path = entry_path.strip_prefix(source).expect("Entry not in source folder");
@ -253,3 +261,17 @@ fn create_document_file<T: AsRef<Path>, R: AsRef<Path>>(content: &str,
info!("Created {}", file_path.display());
Ok(())
}
// The tests are taken from tests/fixtures/`posts_in_subfolder`/
#[test]
fn test_starts_with_path() {
let posts_folder = Path::new("tests/fixtures/posts_in_subfolder/posts");
assert!(!starts_with_path(Path::new("tests/fixtures/posts_in_subfolder"), posts_folder));
assert!(starts_with_path(Path::new("tests/fixtures/posts_in_subfolder/posts"),
posts_folder));
assert!(starts_with_path(Path::new("tests/fixtures/posts_in_subfolder/posts/20170103"),
posts_folder));
assert!(starts_with_path(Path::new("tests/fixtures/posts_in_subfolder/posts/2017/01/08"),
posts_folder));
}

View file

@ -0,0 +1,5 @@
name: cobalt blog testing posts in subfolders
source: "."
dest: "build"
ignore:
- build/*

View file

@ -0,0 +1,20 @@
extends: default.liquid
title: README for different directory layouts
date: 03 Feb 2017 21:00:00 -0000
---
# Test for different blog layouts
## posts inside a single directory
All blog posts reside inside a single directory.
## posts inside some folders
For example there can be one folder per year. This makes managing of blog posts easier.
## one folder per post
There is one folder per blog post. Especially useful if there are many associated files
to one blog post (pictures, source files, ...)

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
{% if is_post %}
<title>{{ title }}</title>
{% else %}
<title>Cobalt.rs Blog</title>
{% endif %}
</head>
<body>
<div>
{% if is_post %}
{% include '_layouts/post.liquid' %}
{% else %}
{{ content }}
{% endif %}
</div>
</body>
<!-- end default.liquid -->
</html>

View file

@ -0,0 +1,8 @@
<!-- start post.liquid -->
<div>
<h2>{{ title }}</h2>
<p>
{{ content }}
</p>
</div>
<!-- end post.liquid -->

View file

@ -0,0 +1,17 @@
extends: default.liquid
---
<!-- start index.liquid -->
<div >
<h2>Blog!</h2>
<!--<br />-->
<div>
{% for post in posts %}
<div>
<h4>{{ post.title }}</h4>
<h4><a href="{{ post.path }}">{{ post.title }}</a></h4>
</div>
{% endfor %}
</div>
</div>
<!-- end index.liquid -->

View file

@ -0,0 +1,9 @@
extends: default.liquid
title: a post inside posts/2017/01/05/
date: 05 Jan 2017 21:00:00 -0000
---
# a post inside posts/2017/01/05/
/posts/2017/01/05/post.md

View file

@ -0,0 +1,9 @@
extends: default.liquid
title: a post inside posts/2017/01/08/
date: 08 Jan 2017 21:00:00 -0000
---
# a post inside posts/2017/01/08/
/posts/2017/01/08//post.md

View file

@ -0,0 +1,9 @@
extends: default.liquid
title: a post inside posts/20170103/
date: 03 Jan 2017 21:00:00 -0000
---
# a post inside posts/20170103/
/posts/20170103/post.md

View file

@ -0,0 +1,9 @@
extends: default.liquid
title: a post inside posts
date: 01 Jan 2017 21:00:00 -0000
---
# a post inside posts
/posts/post.md

View file

@ -195,3 +195,8 @@ pub fn yaml_error() {
pub fn excerpts() {
run_test("excerpts").unwrap();
}
#[test]
pub fn posts_in_subfolder() {
run_test("posts_in_subfolder").unwrap();
}

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
<title>Cobalt.rs Blog</title>
</head>
<body>
<div>
<h1>Test for different blog layouts</h1>
<h2>posts inside a single directory</h2>
<p>All blog posts reside inside a single directory.</p>
<h2>posts inside some folders</h2>
<p>For example there can be one folder per year. This makes managing of blog posts easier.</p>
<h2>one folder per post</h2>
<p>There is one folder per blog post. Especially useful if there are many associated files
to one blog post (pictures, source files, ...)</p>
</div>
</body>
<!-- end default.liquid -->
</html>

View file

@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
<title>Cobalt.rs Blog</title>
</head>
<body>
<div>
<!-- start index.liquid -->
<div >
<h2>Blog!</h2>
<!--<br />-->
<div>
<div>
<h4>a post inside posts/2017/01/08/</h4>
<h4><a href="posts/2017/01/08/post.html">a post inside posts/2017/01/08/</a></h4>
</div>
<div>
<h4>a post inside posts/2017/01/05/</h4>
<h4><a href="posts/2017/01/05/post.html">a post inside posts/2017/01/05/</a></h4>
</div>
<div>
<h4>a post inside posts/20170103/</h4>
<h4><a href="posts/20170103/post.html">a post inside posts/20170103/</a></h4>
</div>
<div>
<h4>a post inside posts</h4>
<h4><a href="posts/post.html">a post inside posts</a></h4>
</div>
</div>
</div>
<!-- end index.liquid -->
</div>
</body>
<!-- end default.liquid -->
</html>

View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
<title>a post inside posts/2017/01/05/</title>
</head>
<body>
<div>
<!-- start post.liquid -->
<div>
<h2>a post inside posts/2017/01/05/</h2>
<p>
<h1>a post inside posts/2017/01/05/</h1>
<p>/posts/2017/01/05/post.md</p>
</p>
</div>
<!-- end post.liquid -->
</div>
</body>
<!-- end default.liquid -->
</html>

View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
<title>a post inside posts/2017/01/08/</title>
</head>
<body>
<div>
<!-- start post.liquid -->
<div>
<h2>a post inside posts/2017/01/08/</h2>
<p>
<h1>a post inside posts/2017/01/08/</h1>
<p>/posts/2017/01/08//post.md</p>
</p>
</div>
<!-- end post.liquid -->
</div>
</body>
<!-- end default.liquid -->
</html>

View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
<title>a post inside posts/20170103/</title>
</head>
<body>
<div>
<!-- start post.liquid -->
<div>
<h2>a post inside posts/20170103/</h2>
<p>
<h1>a post inside posts/20170103/</h1>
<p>/posts/20170103/post.md</p>
</p>
</div>
<!-- end post.liquid -->
</div>
</body>
<!-- end default.liquid -->
</html>

View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<!-- start default.liquid -->
<head>
<title>a post inside posts</title>
</head>
<body>
<div>
<!-- start post.liquid -->
<div>
<h2>a post inside posts</h2>
<p>
<h1>a post inside posts</h1>
<p>/posts/post.md</p>
</p>
</div>
<!-- end post.liquid -->
</div>
</body>
<!-- end default.liquid -->
</html>