This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from tditiecher/master
Added support for specifying frontmatter defaults in _config.yml
- Loading branch information
Showing
10 changed files
with
321 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Pretzel.Logic.Extensions; | ||
|
||
namespace Pretzel.Logic | ||
{ | ||
public interface IDefaultsConfiguration | ||
{ | ||
IDictionary<string, object> ForScope(string path); | ||
} | ||
|
||
internal sealed class DefaultsConfiguration : IDefaultsConfiguration | ||
{ | ||
private readonly IDictionary<string, IDictionary<string, object>> _scopedValues; | ||
|
||
public DefaultsConfiguration(IDictionary<string, object> configuration) | ||
{ | ||
_scopedValues = new Dictionary<string, IDictionary<string, object>>(); | ||
FillScopedValues(configuration); | ||
} | ||
|
||
private void FillScopedValues(IDictionary<string, object> configuration) | ||
{ | ||
if (!configuration.ContainsKey("defaults")) return; | ||
|
||
var defaults = configuration["defaults"] as List<object>; | ||
if (defaults == null) return; | ||
|
||
foreach (var item in defaults.ConvertAll(x => x as IDictionary<string, object>)) | ||
{ | ||
if (item != null && item.ContainsKey("scope") && item.ContainsKey("values")) | ||
{ | ||
var scopeDictionary = item["scope"] as IDictionary<string, object>; | ||
if (scopeDictionary != null && scopeDictionary.ContainsKey("path")) | ||
{ | ||
var path = (string)scopeDictionary["path"]; | ||
var values = item["values"] as IDictionary<string, object>; | ||
_scopedValues.Add(path, values ?? new Dictionary<string, object>()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public IDictionary<string, object> ForScope(string path) | ||
{ | ||
IDictionary<string, object> result = new Dictionary<string, object>(); | ||
|
||
if (path == null) return result; | ||
|
||
if (path.Length > 0) | ||
{ | ||
result = result.Merge(ForScope(Path.GetDirectoryName(path))); | ||
} | ||
if (_scopedValues.ContainsKey(path)) | ||
{ | ||
result = result.Merge(_scopedValues[path]); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Pretzel.Logic.Extensions | ||
{ | ||
/// <summary> | ||
/// Dictionary extension methods. | ||
/// </summary> | ||
public static class DictionaryExtensions | ||
{ | ||
/// <summary> | ||
/// Merges two dictionaries on top of each other and returns a new dictionary. | ||
/// Values from the second override the original values when the key is already present. | ||
/// Values from the second will be added when the key is not present in the first. | ||
/// </summary> | ||
public static IDictionary<TKey, TValue> Merge<TKey, TValue>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second) | ||
{ | ||
var result = new Dictionary<TKey,TValue>(first); | ||
if (second != null) | ||
{ | ||
foreach (var key in second.Keys) | ||
{ | ||
result[key] = second[key]; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.IO.Abstractions.TestingHelpers; | ||
using Pretzel.Logic; | ||
using Pretzel.Logic.Extensions; | ||
using Xunit; | ||
|
||
namespace Pretzel.Tests | ||
{ | ||
public class ConfigurationTests | ||
{ | ||
private readonly Configuration _sut; | ||
|
||
private const string SampleConfig = @" | ||
pretzel: | ||
engine: liquid | ||
title: 'Site Title' | ||
defaults: | ||
- | ||
scope: | ||
path: '' | ||
values: | ||
author: 'default-author' | ||
- | ||
scope: | ||
path: '_posts' | ||
values: | ||
layout: 'post' | ||
author: 'posts-specific-author' | ||
- | ||
scope: | ||
path: '_posts\2016' | ||
values: | ||
layout: 'post-layout-for-2016' | ||
"; | ||
|
||
public ConfigurationTests() | ||
{ | ||
var fileSystem = new MockFileSystem(); | ||
fileSystem.AddFile(@"C:\WebSite\_config.yml", new MockFileData(SampleConfig)); | ||
|
||
_sut = new Configuration(fileSystem, @"C:\WebSite"); | ||
_sut.ReadFromFile(); | ||
} | ||
|
||
[Fact] | ||
public void Indexer_should_correctly_return_the_value() | ||
{ | ||
Assert.Equal("Site Title", _sut["title"]); | ||
} | ||
|
||
[Fact] | ||
public void Permalinks_should_be_added_with_default_value_if_not_specified_in_file() | ||
{ | ||
Assert.Equal(Configuration.DefaultPermalink, _sut["permalink"]); | ||
} | ||
|
||
[Fact] | ||
public void DefaultsForScope_should_layer_the_most_specific_scope_on_top() | ||
{ | ||
var defaults = _sut.Defaults.ForScope(@"_posts\2016"); | ||
|
||
Assert.Equal("post-layout-for-2016", defaults["layout"]); | ||
} | ||
|
||
[Fact] | ||
public void DefaultsForScope_should_take_value_from_less_specific_when_not_found_in_most_specific() | ||
{ | ||
var defaults = _sut.Defaults.ForScope(@"_posts\2016"); | ||
|
||
Assert.Equal("posts-specific-author", defaults["author"]); | ||
} | ||
|
||
[Fact] | ||
public void DefaultsForScope_should_fallback_to_value_from_empty_path_when_given_path_not_found() | ||
{ | ||
var defaults = _sut.Defaults.ForScope("_nonexisting"); | ||
|
||
Assert.Equal("default-author", defaults["author"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using Pretzel.Logic.Extensions; | ||
using Xunit; | ||
|
||
namespace Pretzel.Tests.Extensions | ||
{ | ||
public class DictionaryExtensionTests | ||
{ | ||
[Fact] | ||
public void Merge_two_dictionaries_returns_new_merged_dictionary() | ||
{ | ||
var first = new Dictionary<string, string> | ||
{ | ||
{ "A", "a-first" }, | ||
{ "B", "b-first" }, | ||
}; | ||
var second = new Dictionary<string, string> | ||
{ | ||
{ "A", "a-second" }, | ||
{ "C", "c-second" }, | ||
}; | ||
|
||
var merged = first.Merge(second); | ||
|
||
Assert.Equal(3, merged.Count); | ||
Assert.Equal("a-second", merged["A"]); | ||
Assert.Equal("b-first", merged["B"]); | ||
Assert.Equal("c-second", merged["C"]); | ||
} | ||
|
||
[Fact] | ||
public void Merge_with_null_returns_copy_of_first() | ||
{ | ||
var first = new Dictionary<string, string> | ||
{ | ||
{ "A", "a-first" }, | ||
{ "B", "b-first" }, | ||
}; | ||
|
||
var merged = first.Merge(null); | ||
|
||
Assert.NotSame(merged, first); | ||
Assert.Equal(2, merged.Count); | ||
Assert.Equal("a-first", merged["A"]); | ||
Assert.Equal("b-first", merged["B"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.