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

Add ContentExcerpt feature for pages #202

Merged
merged 5 commits into from
Feb 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
1 change: 1 addition & 0 deletions src/Pretzel.Logic/Templating/Context/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public Page()
public IEnumerable<string> Categories { get; set; }
public IEnumerable<string> Tags { get; set; }
public string Content { get; set; }
public string Excerpt { get; set; }
public string Filepath { get; set; }
public IDictionary<string, object> Bag { get; set; }
public IEnumerable<Page> DirectoryPages { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion src/Pretzel.Logic/Templating/Context/PageContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;

Expand Down Expand Up @@ -32,6 +33,8 @@ public PageContext(PageContext context)

public string Content { get; set; }

public string Excerpt { get; set; }

public SiteContext Site { get; private set; }

public Page Page { get; set; }
Expand Down Expand Up @@ -77,9 +80,9 @@ public static PageContext FromPage(SiteContext siteContext, Page page, string ou
context.Title = siteContext.Title;

context.Content = page.Content;
context.Excerpt = page.Excerpt;
context.Bag = page.Bag;
context.Bag["id"] = page.Id;
context.Bag.Add("url", page.Url);
return context;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/Pretzel.Logic/Templating/Context/SiteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ namespace Pretzel.Logic.Templating.Context
{
public class SiteContext
{
private const string ExcerptSeparatorDefault = "<!--more-->";

private string engine;
private string title;
private string excerptSeparator = ExcerptSeparatorDefault;

public IDictionary<string, object> Config { get; set; }
public string SourceFolder { get; set; }
Expand All @@ -32,6 +35,19 @@ public string Title
set { title = value; }
}

public string ExcerptSeparator
{
get
{
if (Config.Keys.Contains("excerpt_separator"))
{
excerptSeparator = Config["excerpt_separator"].ToString();
}
return excerptSeparator;
}
set { excerptSeparator = value; }
}

public string Engine
{
get
Expand Down
14 changes: 13 additions & 1 deletion src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ private Page CreatePage(SiteContext context, IDictionary<string, object> config,
return pageCache[file];
var contents = SafeReadContents(file);
var header = contents.YamlHeader();
var content = RenderContent(file, contents, header);
var excerptSeparator = header.ContainsKey("excerpt_separator")
? header["excerpt_separator"].ToString()
: context.ExcerptSeparator;

if (header.ContainsKey("published") && header["published"].ToString().ToLower() == "false")
{
Expand All @@ -232,11 +236,13 @@ private Page CreatePage(SiteContext context, IDictionary<string, object> config,
{
Title = header.ContainsKey("title") ? header["title"].ToString() : "this is a post",
Date = header.ContainsKey("date") ? DateTime.Parse(header["date"].ToString()) : file.Datestamp(),
Content = RenderContent(file, contents, header),
Content = content,
Excerpt = GetContentExcerpt(content, excerptSeparator),
Filepath = isPost ? GetPathWithTimestamp(context.OutputFolder, file) : GetFilePathForPage(context, file),
File = file,
Bag = header,
};
page.Bag["excerpt"] = page.Excerpt;

// resolve categories and tags
if (isPost)
Expand Down Expand Up @@ -336,6 +342,12 @@ private string RenderContent(string file, string contents, IDictionary<string, o
return html;
}

private static string GetContentExcerpt(string content, string excerptSeparator)
{
var index = content.IndexOf(excerptSeparator, StringComparison.InvariantCulture);
return index == -1 ? content : content.Substring(0, index);
}

private string SafeReadLine(string file)
{
string postFirstLine;
Expand Down
42 changes: 34 additions & 8 deletions src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ public void page_default_values()
Assert.Equal("<h1>Title</h1>\n", siteContext.Pages[0].Content);
Assert.Equal(@"C:\TestSite\_site\SomeFile.md", siteContext.Pages[0].Filepath);
Assert.Equal(@"C:\TestSite\SomeFile.md", siteContext.Pages[0].File);
Assert.Equal(2, siteContext.Pages[0].Bag.Count);
Assert.Equal(3, siteContext.Pages[0].Bag.Count); // param, excerpt, date
Assert.Equal("value", siteContext.Pages[0].Bag["param"]);
}

Expand All @@ -759,7 +759,7 @@ public void page_metadata_values()
Assert.Equal("<h1>Title</h1>\n", siteContext.Pages[0].Content);
Assert.Equal(@"C:\TestSite\_site\SomeFile.md", siteContext.Pages[0].Filepath);
Assert.Equal(@"C:\TestSite\SomeFile.md", siteContext.Pages[0].File);
Assert.Equal(3, siteContext.Pages[0].Bag.Count);
Assert.Equal(4, siteContext.Pages[0].Bag.Count); // title, date, param, excerpt
Assert.Equal("value", siteContext.Pages[0].Bag["param"]);
Assert.Equal("my title", siteContext.Pages[0].Bag["title"]);
Assert.Equal(currentDate, siteContext.Pages[0].Bag["date"]);
Expand All @@ -784,7 +784,7 @@ public void page_with_date_in_title()
Assert.Equal("<h1>Title</h1>\n", siteContext.Pages[0].Content);
Assert.Equal(string.Format(@"C:\TestSite\_site\{0}-SomeFile.md", currentDate.Replace("/", "-")), siteContext.Pages[0].Filepath);
Assert.Equal(filePath, siteContext.Pages[0].File);
Assert.Equal(2, siteContext.Pages[0].Bag.Count);
Assert.Equal(3, siteContext.Pages[0].Bag.Count); // param, excerpt, date
Assert.Equal("value", siteContext.Pages[0].Bag["param"]);
}

Expand All @@ -807,7 +807,7 @@ public void page_with_false_date_in_title()
Assert.Equal("<h1>Title</h1>\n", siteContext.Pages[0].Content);
Assert.Equal(string.Format(@"C:\TestSite\_site\{0}SomeFile.md", currentDate.Replace("/", "-")), siteContext.Pages[0].Filepath);
Assert.Equal(filePath, siteContext.Pages[0].File);
Assert.Equal(2, siteContext.Pages[0].Bag.Count);
Assert.Equal(3, siteContext.Pages[0].Bag.Count); // param, excerpt, date
Assert.Equal("value", siteContext.Pages[0].Bag["param"]);
}

Expand All @@ -827,7 +827,7 @@ public void post_default_values()
Assert.Equal("<h1>Title</h1>\n", siteContext.Posts[0].Content);
Assert.Equal(@"C:\TestSite\_site\SomeFile.md", siteContext.Posts[0].Filepath);
Assert.Equal(@"C:\TestSite\_posts\SomeFile.md", siteContext.Posts[0].File);
Assert.Equal(2, siteContext.Posts[0].Bag.Count);
Assert.Equal(3, siteContext.Posts[0].Bag.Count); // param, excerpt, date
Assert.Equal("value", siteContext.Posts[0].Bag["param"]);
}

Expand All @@ -851,12 +851,38 @@ public void post_metadata_values()
Assert.Equal("<h1>Title</h1>\n", siteContext.Posts[0].Content);
Assert.Equal(@"C:\TestSite\_site\SomeFile.md", siteContext.Posts[0].Filepath);
Assert.Equal(@"C:\TestSite\_posts\SomeFile.md", siteContext.Posts[0].File);
Assert.Equal(3, siteContext.Posts[0].Bag.Count);
Assert.Equal(4, siteContext.Posts[0].Bag.Count); // title, date, param, excerpt
Assert.Equal("value", siteContext.Posts[0].Bag["param"]);
Assert.Equal("my title", siteContext.Posts[0].Bag["title"]);
Assert.Equal(currentDate, siteContext.Posts[0].Bag["date"]);
}

[Fact]
public void post_with_excerpt()
{
fileSystem.AddFile(@"C:\TestSite\_posts\SomeFile.md", new MockFileData(string.Format(@"One<!--more-->Two")));

// act
var siteContext = generator.BuildContext(@"C:\TestSite", false);

Assert.Equal(1, siteContext.Posts.Count);
Assert.Equal("<p>One", siteContext.Posts[0].Excerpt);
}

[Fact]
public void post_with_excerpt_and_custom_separator()
{
fileSystem.AddFile(@"C:\TestSite\_posts\SomeFile.md", new MockFileData(string.Format(@"---
excerpt_separator: <!--excerpt_separator-->
---One<!--more-->Two<!--excerpt_separator-->Three")));

// act
var siteContext = generator.BuildContext(@"C:\TestSite", false);

Assert.Equal(1, siteContext.Posts.Count);
Assert.Equal("<p>One<!--more-->Two", siteContext.Posts[0].Excerpt);
}

[Fact]
public void post_with_date_in_title()
{
Expand All @@ -876,7 +902,7 @@ public void post_with_date_in_title()
Assert.Equal("<h1>Title</h1>\n", siteContext.Posts[0].Content);
Assert.Equal(string.Format(@"C:\TestSite\_site\{0}\SomeFile.md", currentDate.Replace("/", "\\")), siteContext.Posts[0].Filepath);
Assert.Equal(filePath, siteContext.Posts[0].File);
Assert.Equal(2, siteContext.Posts[0].Bag.Count);
Assert.Equal(3, siteContext.Posts[0].Bag.Count); // param, excerpt, date
Assert.Equal("value", siteContext.Posts[0].Bag["param"]);
}

Expand All @@ -899,7 +925,7 @@ public void post_with_false_date_in_title()
Assert.Equal("<h1>Title</h1>\n", siteContext.Posts[0].Content);
Assert.Equal(string.Format(@"C:\TestSite\_site\{0}SomeFile.md", currentDate.Replace("/", "\\")), siteContext.Posts[0].Filepath);
Assert.Equal(filePath, siteContext.Posts[0].File);
Assert.Equal(2, siteContext.Posts[0].Bag.Count);
Assert.Equal(3, siteContext.Posts[0].Bag.Count); // param, excerpt, date
Assert.Equal("value", siteContext.Posts[0].Bag["param"]);
}

Expand Down
30 changes: 30 additions & 0 deletions src/Pretzel.Tests/Templating/Jekyll/LiquidEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,36 @@ public void The_Output_Should_Have_Been_Transformed()
}
}

public class Given_Page_Has_Excerpt : BakingEnvironment<LiquidEngine>
{
const string IndexContents = "---\r\n title: index\r\n show: true \r\n---\r\n\r\n{% for post in site.posts %}{{ post.excerpt }}{% endfor %}";
const string PostContents = "---\r\n layout: nil \r\n---\r\n\r\n<p>One<!--more-->Two</p>";
const string ExpectedIndexContents = "<p><p>One</p>";

public override LiquidEngine Given()
{
var engine = new LiquidEngine();
engine.Initialize();
return engine;
}

public override void When()
{
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(IndexContents));
FileSystem.AddFile(@"C:\website\_posts\2015-02-03-post.md", new MockFileData(PostContents));
var generator = new SiteContextGenerator(FileSystem, Enumerable.Empty<IContentTransform>());
var context = generator.BuildContext(@"C:\website\", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}

[Fact]
public void Posts_Should_Have_Excerpt()
{
Assert.Equal(ExpectedIndexContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}

public class Given_Page_Has_HighlightBlock : BakingEnvironment<LiquidEngine>
{
const string PageContents = "---\r\n layout: nil \r\n---\r\n\r\n{% highlight %}a word{% endhighlight %}";
Expand Down