diff --git a/ReleaseNotes.md b/ReleaseNotes.md index b2a3d7c93..641dd9b3c 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -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 diff --git a/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs b/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs index 9c7c022a7..ec03df975 100644 --- a/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs +++ b/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs @@ -290,10 +290,12 @@ private List ResolveCategories(SiteContext context, IDictionary(); - 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) { categories.AddRange((IEnumerable)header["categories"]); @@ -306,6 +308,16 @@ private List ResolveCategories(SiteContext context, IDictionary + { + 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 = "

mycategory

"; + + 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 { private const string PageContent = "---\r\n \r\n---\r\n{% custom %}";