diff --git a/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs b/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs index 1fe8b1cc9..2bebfdcfb 100644 --- a/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs +++ b/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs @@ -27,22 +27,43 @@ public override void Initialize() { } - protected override void PreProcess() + private class TagComparer : IEqualityComparer + { + public bool Equals(ITag x, ITag y) + { + if (x == null || y == null) + { + return false; + } + + return x.Name == y.Name; + } + + public int GetHashCode(ITag obj) + { + return obj.Name.GetHashCode(); + } + } + + protected override void PreProcess() { includesPath = Path.Combine(Context.SourceFolder, "_includes"); if (Tags != null) { - _allTags.AddRange(Tags); + var toAdd = Tags.Except(_allTags, new TagComparer()).ToList(); + _allTags.AddRange(toAdd); } if (TagFactories != null) { - _allTags.AddRange(TagFactories.Select(factory => - { - factory.Initialize(Context); - return factory.CreateTag(); - })); + var toAdd = TagFactories.Select(factory => + { + factory.Initialize(Context); + return factory.CreateTag(); + }).Except(_allTags, new TagComparer()).ToList(); + + _allTags.AddRange(toAdd); } } diff --git a/src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs b/src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs index eb260b059..729d50cdf 100644 --- a/src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs +++ b/src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs @@ -249,6 +249,25 @@ public void PostUrlTag_should_be_used() Assert.Equal(expected, FileSystem.File.ReadAllText(@"C:\website\_site\index.html")); } + [Fact] + public void Engine_can_process_template_multiple_times() + { + // arrange + const string templateContents = "@Raw(Model.Content) @Tag.PostUrl(\"index.cshtml\")"; + const string pageContents = "

Hello

"; + const string expected = "

Hello

/index.html"; + + Subject.TagFactories = new List { new PostUrlTagFactory() }; + + // act + // Process contents multiple times (e.g., when a file has changed in taste) + ProcessContents(templateContents, pageContents, new Dictionary()); + ProcessContents(templateContents, pageContents, new Dictionary()); + + // assert + Assert.Equal(expected, FileSystem.File.ReadAllText(@"C:\website\_site\index.html")); + } + public class CustomTag : DotLiquid.Tag, ITag { public new string Name { get { return "Custom"; } }