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

Commit

Permalink
Merge pull request #233 from dkarzon/master
Browse files Browse the repository at this point in the history
Better support for exclude config setting
  • Loading branch information
Jérémie Bertrand committed Apr 12, 2015
2 parents 2c555b2 + 10cb372 commit f62c862
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
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
config.Add("permalink", "date");
}

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 @@ -399,8 +399,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 @@ -418,6 +417,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 @@ -429,14 +430,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 @@ -459,9 +460,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

0 comments on commit f62c862

Please sign in to comment.