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

Inconsistent page.date handling #250

Merged
merged 4 commits into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,8 @@ private Page CreatePage(SiteContext context, IDictionary<string, object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<h1>Title</h1>", 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);
Expand Down Expand Up @@ -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("<h1>Title</h1>", 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);
Expand Down
48 changes: 48 additions & 0 deletions src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LiquidEngine> {
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("<p>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<LiquidEngine> {
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("<p>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());
}
}
}
}