Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #274 from thoemmi/only_frontmatter_categories
Browse files Browse the repository at this point in the history
added option only_frontmatter_categories
  • Loading branch information
Jérémie Bertrand committed Nov 16, 2015
2 parents ead84af + 7571867 commit 5d3946d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
1 change: 1 addition & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All existing plugins must be recompiled with referencing only Pretzel.Logic.
- [#253](https://github.com/Code52/pretzel/pull/253) - Run on Mono contributed by Thiago 'Jedi' Abreu ([thiagoabreu](https://github.com/thiagoabreu))
- [#259](https://github.com/Code52/pretzel/pull/259) - Changed date guessing heuristic to use last modification time of posts instead of current time, if no better data is available. contributed by Gábor Gergely ([kodfodrasz](https://github.com/kodfodrasz))
- [#260](https://github.com/Code52/pretzel/pull/260) - Refactor logging contributed by Gábor Gergely ([kodfodrasz](https://github.com/kodfodrasz))
- [#274](https://github.com/Code52/pretzel/pull/274) - Added option to only use categories found in posts's frontmatter by Thomas Freudenberg ([thoemmi](https://github.com/thoemmi))

## Fixes
- [#198](https://github.com/Code52/pretzel/issues/198) - Liquid tag/filter with underscore doesn't works in markdown files
Expand Down
20 changes: 16 additions & 4 deletions src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,12 @@ private List<string> ResolveCategories(SiteContext context, IDictionary<string,
{
var categories = new List<string>();

var postPath = page.File.Replace(context.SourceFolder, string.Empty);
string rawCategories = postPath.Replace(fileSystem.Path.GetFileName(page.File), string.Empty).Replace("_posts", string.Empty);
categories.AddRange(rawCategories.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries));

if (!IsOnlyFrontmatterCategories(context))
{
var postPath = page.File.Replace(context.SourceFolder, string.Empty);
string rawCategories = postPath.Replace(fileSystem.Path.GetFileName(page.File), string.Empty).Replace("_posts", string.Empty);
categories.AddRange(rawCategories.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries));
}
if (header.ContainsKey("categories") && header["categories"] is IEnumerable<string>)
{
categories.AddRange((IEnumerable<string>)header["categories"]);
Expand All @@ -306,6 +308,16 @@ private List<string> ResolveCategories(SiteContext context, IDictionary<string,
return categories;
}

private static bool IsOnlyFrontmatterCategories(SiteContext context)
{
object onlyFrontmatterCategories;
if (!context.Config.TryGetValue("only_frontmatter_categories", out onlyFrontmatterCategories))
{
return false;
}
return onlyFrontmatterCategories is bool && (bool) onlyFrontmatterCategories;
}

private string GetFilePathForPage(SiteContext context, string file)
{
return Path.Combine(context.OutputFolder, MapToOutputPath(context, file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void site_context_generator_finds_posts()
// assert
Assert.Equal(1, siteContext.Posts.Count);
}

[Fact]
public void posts_do_not_include_pages()
{
Expand Down Expand Up @@ -997,6 +997,20 @@ public void permalink_with_folder_categories()
Assert.Equal(outputPath, firstPost.Url);
}

[Fact]
public void permalink_with_folder_categories_frontmatter_only()
{
fileSystem.AddFile(@"C:\TestSite\_config.yml", new MockFileData(@"only_frontmatter_categories: true"));
fileSystem.AddFile(@"C:\TestSite\foo\bar\_posts\2015-03-09-SomeFile.md", new MockFileData(@"---
categories: [cat1, cat2]
---# Title"));
var outputPath = "/cat1/cat2/2015/03/09/SomeFile.html";
// act
var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false);
var firstPost = siteContext.Posts.First();
Assert.Equal(outputPath, firstPost.Url);
}

[InlineData("date", "/cat1/cat2/2015/03/09/foobar-baz.html", "cat1,cat2")]
[InlineData("date", "/2015/03/09/foobar-baz.html", "")]
[InlineData("/:dashcategories/:year/:month/:day/:title.html", "/cat1-cat2/2015/03/09/foobar-baz.html", "cat1,cat2")]
Expand Down
43 changes: 43 additions & 0 deletions src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,49 @@ public void The_first_alphabetically_category_must_appear_in_the_content()
}
}

public class Given_Posts_Folder_Is_In_Another_Folder_With_Frontmatter_Only_Categories : BakingEnvironment<LiquidEngine>
{
private SiteContext Context;
private const string ContentWithCategory = "---\r\n category: mycategory \r\n permalink: /:categories/:year/:month/:day/:title.html \r\n---\r\n{{ site.categories[0].name }}";
private const string ExpectedPageContent = "<p>mycategory</p>";

public override LiquidEngine Given()
{
return new LiquidEngine();
}

public override void When()
{
FileSystem.AddFile(@"C:\website\_config.yml", new MockFileData(@"only_frontmatter_categories: true"));
FileSystem.AddFile(@"C:\website\oh\my\_posts\2015-02-02-post.md", new MockFileData(ContentWithCategory));
var generator = GetSiteContextGenerator(FileSystem);
Context = generator.BuildContext(@"C:\website\", @"C:\website\_site\", false);
Subject.FileSystem = FileSystem;
Subject.Process(Context);
}

[Fact]
public void Layout_With_Bad_Header_Should_Not_Throw_Exception()
{
Assert.Equal(Context.Posts.Count, 1);
var categories = Context.Posts[0].Categories.ToList();
Assert.Equal(categories.Count, 1);
Assert.Equal(categories[0], "mycategory");
}

[Fact]
public void The_permalink_should_use_all_categories()
{
Assert.True(FileSystem.File.Exists(@"C:\website\_site\mycategory\2015\02\02\post.html"));
}

[Fact]
public void The_first_alphabetically_category_must_appear_in_the_content()
{
Assert.Equal(ExpectedPageContent, FileSystem.File.ReadAllText(@"C:\website\_site\mycategory\2015\02\02\post.html").RemoveWhiteSpace());
}
}

public class Given_Engine_Has_Custom_Tag : BakingEnvironment<LiquidEngine>
{
private const string PageContent = "---\r\n \r\n---\r\n{% custom %}";
Expand Down

0 comments on commit 5d3946d

Please sign in to comment.