Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.
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
33 changes: 18 additions & 15 deletions src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,13 @@ public SiteContext BuildContext(string path, string destinationPath, bool includ
if (!config.ContainsKey("permalink"))
config.Add("permalink", "/:year/:month/:day/:title.html");

if (config.ContainsKey("pretzel"))
if (config.ContainsKey("include"))
{
var pretzelSettings = config["pretzel"] as Dictionary<string, object>;
if (pretzelSettings != null)
{
if (pretzelSettings.ContainsKey("include") && includes.Count == 0)
{
includes.AddRange((IEnumerable<string>)pretzelSettings["include"]);
}
if (pretzelSettings.ContainsKey("exclude") && excludes.Count == 0)
{
excludes.AddRange((IEnumerable<string>)pretzelSettings["exclude"]);
}
}
includes.AddRange((IEnumerable<string>)config["include"]);
}
if (config.ContainsKey("exclude"))
{
excludes.AddRange((IEnumerable<string>)config["exclude"]);
}

var context = new SiteContext
Expand Down Expand Up @@ -193,14 +186,24 @@ private bool ContainsYamlFrontMatter(string file)
return postFirstLine != null && postFirstLine.StartsWith("---");
}

private bool IsExcludedPath(string relativePath)
{
return excludes.Contains(relativePath) || excludes.Any(e => relativePath.StartsWith(e));
}

private bool IsIncludedPath(string relativePath)
{
return includes.Contains(relativePath) || includes.Any(e => relativePath.StartsWith(e));
}

public bool CanBeIncluded(string relativePath)
{
if (excludes.Count > 0 && excludes.Contains(relativePath))
if (excludes.Count > 0 && IsExcludedPath(relativePath))
{
return false;
}

if (includes.Count > 0 && includes.Contains(relativePath))
if (includes.Count > 0 && IsIncludedPath(relativePath))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ public void CanBeIncluded_Scenarios_Include()
// arrange
Func<string, bool> function = generator.CanBeIncluded;
fileSystem.AddFile(@"C:\TestSite\_config.yml", new MockFileData(@"---
pretzel:
include: [_folder, .something-else, some-file.tmp, test\somefile.txt, subfolder\childfolder, anotherfolder\tempfile.tmp]
include: [_folder, .something-else, some-file.tmp, test\somefile.txt, subfolder\childfolder, anotherfolder\tempfile.tmp]
---"));
// act
var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false);
Expand All @@ -439,6 +438,8 @@ public void CanBeIncluded_Scenarios_Include()
Assert.False(function("some-file.TMP"));
Assert.True(function("another-file.bar"));

Assert.True(function("_folder\file.txt"));

Assert.True(function(@"test\somefile.txt"));
Assert.True(function(@"subfolder\childfolder"));
Assert.True(function(@"anotherfolder\tempfile.tmp"));
Expand All @@ -450,14 +451,14 @@ public void CanBeIncluded_Scenarios_Exclude()
// arrange
Func<string, bool> function = generator.CanBeIncluded;
fileSystem.AddFile(@"C:\TestSite\_config.yml", new MockFileData(@"---
pretzel:
exclude: [folder, .htaccess, some-file.tmp, test\somefile.txt, subfolder\childfolder, anotherfolder\tempfile.tmp]
exclude: [folder, .htaccess, some-file.tmp, test\somefile.txt, subfolder\childfolder, anotherfolder\tempfile.tmp]
---"));
// act
var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false);

// assert
Assert.False(function("folder"));
Assert.False(function("folder\file.txt"));
Assert.False(function("_folder"));

// .htaccess is excluded
Expand All @@ -480,9 +481,8 @@ public void CanBeIncluded_Scenarios_IncludeExclude()
// arrange
Func<string, bool> function = generator.CanBeIncluded;
fileSystem.AddFile(@"C:\TestSite\_config.yml", new MockFileData(@"---
pretzel:
include: [_folder, .something-else]
exclude: [folder, test\somefile.txt]
include: [_folder, .something-else]
exclude: [folder, test\somefile.txt]
---"));
// act
var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false);
Expand Down