diff --git a/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs b/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs index 471d8f762..9d65508d7 100644 --- a/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs +++ b/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs @@ -270,11 +270,8 @@ private Page CreatePage(SiteContext context, IDictionary config, // resolve id page.Id = page.Url.Replace(".html", string.Empty).Replace("index", string.Empty); - // ensure the date is accessible in the hash - if (!page.Bag.ContainsKey("date")) - { - page.Bag["date"] = page.Date; - } + // always write date back to Bag as DateTime + page.Bag["date"] = page.Date; // The GetDirectoryPage method is reentrant, we need a cache to stop a stack overflow :) pageCache.Add(file, page); diff --git a/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs b/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs index 7b631e91e..51e0e4893 100644 --- a/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs +++ b/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs @@ -718,20 +718,20 @@ public void page_default_values() [Fact] public void page_metadata_values() { - var currentDate = new DateTime(2015, 1, 27).ToShortDateString(); + var currentDate = new DateTime(2015, 1, 27); fileSystem.AddFile(@"C:\TestSite\SomeFile.md", new MockFileData(string.Format(@"--- title: my title date: {0} param: value ---# Title", - currentDate))); + currentDate.ToShortDateString()))); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); Assert.Equal(1, siteContext.Pages.Count); Assert.Equal("my title", siteContext.Pages[0].Title); - Assert.Equal(new DateTime(2015, 1, 27), siteContext.Pages[0].Date); + Assert.Equal(currentDate, siteContext.Pages[0].Date); Assert.Equal("

Title

", siteContext.Pages[0].Content.RemoveWhiteSpace()); Assert.Equal(@"C:\TestSite\_site\SomeFile.md", siteContext.Pages[0].Filepath); Assert.Equal(@"C:\TestSite\SomeFile.md", siteContext.Pages[0].File); @@ -811,20 +811,20 @@ public void post_default_values() [Fact] public void post_metadata_values() { - var currentDate = new DateTime(2015, 1, 27).ToShortDateString(); + var currentDate = new DateTime(2015, 1, 27); fileSystem.AddFile(@"C:\TestSite\_posts\SomeFile.md", new MockFileData(string.Format(@"--- title: my title date: {0} param: value ---# Title", - currentDate))); + currentDate.ToShortDateString()))); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); Assert.Equal(1, siteContext.Posts.Count); Assert.Equal("my title", siteContext.Posts[0].Title); - Assert.Equal(new DateTime(2015, 1, 27), siteContext.Posts[0].Date); + Assert.Equal(currentDate, siteContext.Posts[0].Date); Assert.Equal("

Title

", siteContext.Posts[0].Content.RemoveWhiteSpace()); Assert.Equal(@"C:\TestSite\_site\SomeFile.md", siteContext.Posts[0].Filepath); Assert.Equal(@"C:\TestSite\_posts\SomeFile.md", siteContext.Posts[0].File); diff --git a/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs b/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs index 74b7637cc..5342dfd31 100644 --- a/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs +++ b/src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs @@ -1895,5 +1895,53 @@ public void Permalink_generates_expected_file_path(string permalink, string expe // act Assert.Equal(ExpectedfileContents, fileSystem.File.ReadAllText(string.Format(@"C:\TestSite\_site\{0}", expectedUrl)).RemoveWhiteSpace()); } + + public class Given_Page_Without_Explicit_Date : BakingEnvironment { + private const string PageContents = "---\r\n layout: nil \r\n---\r\n\r\n{{ page.date | date_to_xmlschema }}"; + private readonly string ExpectedfileContents = new DateTime(2015, 2, 22).ToString("

yyyy-MM-ddTHH:mm:sszzz<\\/p>"); + + public override LiquidEngine Given() { + var engine = new LiquidEngine(); + engine.Initialize(); + return engine; + } + + public override void When() { + FileSystem.AddFile(@"C:\website\_posts\2015-02-22-post.md", new MockFileData(PageContents)); + var generator = GetSiteContextGenerator(FileSystem); + var context = generator.BuildContext(@"C:\website\", @"D:\Result\_site", false); + Subject.FileSystem = FileSystem; + Subject.Process(context); + } + + [Fact] + public void The_File_Should_Display_The_Page_Url() { + Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"D:\Result\_site\2015\02\22\post.html").RemoveWhiteSpace()); + } + } + + public class Given_Page_With_Explicit_Date : BakingEnvironment { + private const string PageContents = "---\r\n layout: nil \r\n date: 2015-02-23 12:30:00\r\n---\r\n\r\n{{ page.date | date_to_xmlschema }}"; + private readonly string ExpectedfileContents = new DateTime(2015, 2, 23, 12, 30, 0).ToString("

yyyy-MM-ddTHH:mm:sszzz<\\/p>"); + + public override LiquidEngine Given() { + var engine = new LiquidEngine(); + engine.Initialize(); + return engine; + } + + public override void When() { + FileSystem.AddFile(@"C:\website\_posts\2015-02-22-post.md", new MockFileData(PageContents)); + var generator = GetSiteContextGenerator(FileSystem); + var context = generator.BuildContext(@"C:\website\", @"D:\Result\_site", false); + Subject.FileSystem = FileSystem; + Subject.Process(context); + } + + [Fact] + public void The_File_Should_Display_The_Page_Url() { + Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"D:\Result\_site\2015\02\23\post.html").RemoveWhiteSpace()); + } + } } }