diff --git a/Nustache.Core/StartSection.cs b/Nustache.Core/Block.cs similarity index 72% rename from Nustache.Core/StartSection.cs rename to Nustache.Core/Block.cs index 76f495e..1260b3d 100644 --- a/Nustache.Core/StartSection.cs +++ b/Nustache.Core/Block.cs @@ -1,8 +1,8 @@ namespace Nustache.Core { - public class StartSection : Container + public class Block : Section { - public StartSection(string name, params Part[] children) + public Block(string name, params Part[] children) : base(name) { Load(children); @@ -22,7 +22,7 @@ public override void Render(RenderContext context) #region Boring stuff - public bool Equals(StartSection other) + public bool Equals(Block other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; @@ -33,8 +33,8 @@ public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != typeof(StartSection)) return false; - return Equals((StartSection)obj); + if (obj.GetType() != typeof(Block)) return false; + return Equals((Block)obj); } public override int GetHashCode() @@ -44,7 +44,7 @@ public override int GetHashCode() public override string ToString() { - return string.Format("StartSection(\"{0}\")", Name); + return string.Format("Block(\"{0}\")", Name); } #endregion diff --git a/Nustache.Core/Nustache.Core.csproj b/Nustache.Core/Nustache.Core.csproj index c153d49..8a9c29d 100644 --- a/Nustache.Core/Nustache.Core.csproj +++ b/Nustache.Core/Nustache.Core.csproj @@ -45,7 +45,7 @@ - + @@ -55,7 +55,7 @@ - + diff --git a/Nustache.Core/Parser.cs b/Nustache.Core/Parser.cs index 7af5a33..2fb913d 100644 --- a/Nustache.Core/Parser.cs +++ b/Nustache.Core/Parser.cs @@ -1,38 +1,32 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Nustache.Core { public class Parser { - public IEnumerable Parse(IEnumerable parts) + public void Parse(Section section, IEnumerable parts) { - var containerStack = new Stack(); - Container container = null; + if (section == null) + throw new ArgumentNullException("section"); + + var sectionStack = new Stack
(); + sectionStack.Push(section); foreach (var part in parts) { - if (container != null) - { - container.Add(part); - } - else - { - if (!(part is Container)) - { - yield return part; - } - } + section.Add(part); - if (part is Container) + if (part is Section) { - containerStack.Push(container); - container = (Container)part; + sectionStack.Push(section); + section = (Section)part; } else if (part is EndSection) { var endSection = (EndSection)part; - if (container == null) + if (sectionStack.Count == 1) { throw new NustacheException( string.Format( @@ -40,23 +34,16 @@ public IEnumerable Parse(IEnumerable parts) endSection.Name)); } - if (endSection.Name != container.Name) + if (endSection.Name != section.Name) { throw new NustacheException( string.Format( "End section {0} does not match start section {1}!", endSection.Name, - container.Name)); - } - - var lastStartSection = containerStack.Pop(); - - if (lastStartSection == null) - { - yield return container; + section.Name)); } - container = lastStartSection; + section = sectionStack.Pop(); } } } diff --git a/Nustache.Core/RenderContext.cs b/Nustache.Core/RenderContext.cs index 2380567..1f49efa 100644 --- a/Nustache.Core/RenderContext.cs +++ b/Nustache.Core/RenderContext.cs @@ -8,7 +8,7 @@ namespace Nustache.Core public class RenderContext { private const int IncludeLimit = 1024; - private readonly Stack _containerStack = new Stack(); + private readonly Stack
_sectionStack = new Stack
(); private readonly Stack _dataStack = new Stack(); private readonly TextWriter _writer; private readonly Func _templateLocator; @@ -16,7 +16,7 @@ public class RenderContext public RenderContext(Template template, object data, TextWriter writer, Func templateLocator) { - _containerStack.Push(template); + _sectionStack.Push(template); _dataStack.Push(data); _writer = writer; _templateLocator = templateLocator; @@ -120,9 +120,9 @@ public void Include(string templateName) private TemplateDefinition GetTemplateDefinition(string name) { - foreach (var container in _containerStack) + foreach (var section in _sectionStack) { - var templateDefinition = container.GetTemplateDefinition(name); + var templateDefinition = section.GetTemplateDefinition(name); if (templateDefinition != null) { @@ -133,15 +133,15 @@ private TemplateDefinition GetTemplateDefinition(string name) return null; } - public void Push(Container section, object data) + public void Push(Section section, object data) { - _containerStack.Push(section); + _sectionStack.Push(section); _dataStack.Push(data); } public void Pop() { - _containerStack.Pop(); + _sectionStack.Pop(); } } } \ No newline at end of file diff --git a/Nustache.Core/Scanner.cs b/Nustache.Core/Scanner.cs index b3177b5..58131ee 100644 --- a/Nustache.Core/Scanner.cs +++ b/Nustache.Core/Scanner.cs @@ -25,7 +25,7 @@ public IEnumerable Scan(string template) if (marker[0] == '#') { - yield return new StartSection(marker.Substring(1)); + yield return new Block(marker.Substring(1)); } else if (marker[0] == '<') { diff --git a/Nustache.Core/Container.cs b/Nustache.Core/Section.cs similarity index 91% rename from Nustache.Core/Container.cs rename to Nustache.Core/Section.cs index 4b823f8..fc3f3f4 100644 --- a/Nustache.Core/Container.cs +++ b/Nustache.Core/Section.cs @@ -2,14 +2,14 @@ namespace Nustache.Core { - public class Container : Part + public class Section : Part { private readonly string _name; private readonly List _children = new List(); private readonly Dictionary _templateDefinitions = new Dictionary(); - public Container(string name) + public Section(string name) { _name = name; } diff --git a/Nustache.Core/Template.cs b/Nustache.Core/Template.cs index 68aa89e..8f4b015 100644 --- a/Nustache.Core/Template.cs +++ b/Nustache.Core/Template.cs @@ -3,10 +3,10 @@ namespace Nustache.Core { - public class Template : Container + public class Template : Section { public Template() - : base("-root-") + : base("#template") // I'm not happy about this fake name. { } @@ -28,7 +28,7 @@ public void Load(TextReader reader) var scanner = new Scanner(); var parser = new Parser(); - Load(parser.Parse(scanner.Scan(template))); + parser.Parse(this, scanner.Scan(template)); } /// diff --git a/Nustache.Core/TemplateDefinition.cs b/Nustache.Core/TemplateDefinition.cs index cf1fd97..4062fdf 100644 --- a/Nustache.Core/TemplateDefinition.cs +++ b/Nustache.Core/TemplateDefinition.cs @@ -1,6 +1,6 @@ namespace Nustache.Core { - public class TemplateDefinition : Container + public class TemplateDefinition : Section { public TemplateDefinition(string name) : base(name) diff --git a/Nustache.Tests/Describe_Parser_Parse.cs b/Nustache.Tests/Describe_Parser_Parse.cs index 4a95433..67904f1 100644 --- a/Nustache.Tests/Describe_Parser_Parse.cs +++ b/Nustache.Tests/Describe_Parser_Parse.cs @@ -11,114 +11,117 @@ public class Describe_Parser_Parse public void It_combines_sections() { var parser = new Parser(); + var template = new Template(); - var parts = parser.Parse( - new Part[] - { - new LiteralText("before"), - new StartSection("foo"), - new LiteralText("inside"), - new EndSection("foo"), - new LiteralText("after") - }); + parser.Parse(template, + new Part[] + { + new LiteralText("before"), + new Block("foo"), + new LiteralText("inside"), + new EndSection("foo"), + new LiteralText("after") + }); CollectionAssert.AreEqual( new Part[] { new LiteralText("before"), - new StartSection("foo", + new Block("foo", new LiteralText("inside"), new EndSection("foo")), new LiteralText("after") }, - parts.ToArray()); + template.Children.ToArray()); } [Test] public void It_handles_nested_sections() { var parser = new Parser(); + var template = new Template(); - var parts = parser.Parse( - new Part[] - { - new LiteralText("before foo"), - new StartSection("foo"), - new LiteralText("before bar"), - new StartSection("bar"), - new LiteralText("inside bar"), - new EndSection("bar"), - new LiteralText("after bar"), - new EndSection("foo"), - new LiteralText("after foo") - }); + parser.Parse(template, + new Part[] + { + new LiteralText("before foo"), + new Block("foo"), + new LiteralText("before bar"), + new Block("bar"), + new LiteralText("inside bar"), + new EndSection("bar"), + new LiteralText("after bar"), + new EndSection("foo"), + new LiteralText("after foo") + }); CollectionAssert.AreEqual( new Part[] { new LiteralText("before foo"), - new StartSection("foo", + new Block("foo", new LiteralText("before bar"), - new StartSection("bar", + new Block("bar", new LiteralText("inside bar"), new EndSection("bar")), new LiteralText("after bar"), new EndSection("foo")), new LiteralText("after foo") }, - parts.ToArray()); + template.Children.ToArray()); } [Test] public void It_handles_nested_sections_with_the_same_name() { var parser = new Parser(); + var template = new Template(); - var parts = parser.Parse( - new Part[] - { - new LiteralText("before foo 1"), - new StartSection("foo"), - new LiteralText("before foo 2"), - new StartSection("foo"), - new LiteralText("inside foo 2"), - new EndSection("foo"), - new LiteralText("after foo 2"), - new EndSection("foo"), - new LiteralText("after foo 1") - }); + parser.Parse(template, + new Part[] + { + new LiteralText("before foo 1"), + new Block("foo"), + new LiteralText("before foo 2"), + new Block("foo"), + new LiteralText("inside foo 2"), + new EndSection("foo"), + new LiteralText("after foo 2"), + new EndSection("foo"), + new LiteralText("after foo 1") + }); CollectionAssert.AreEqual( new Part[] { new LiteralText("before foo 1"), - new StartSection("foo", + new Block("foo", new LiteralText("before foo 2"), - new StartSection("foo", + new Block("foo", new LiteralText("inside foo 2"), new EndSection("foo")), new LiteralText("after foo 2"), new EndSection("foo")), new LiteralText("after foo 1") }, - parts.ToArray()); + template.Children.ToArray()); } [Test] public void It_throws_when_the_end_section_does_not_match_the_start_section() { var parser = new Parser(); + var template = new Template(); - Assert.Catch(() => parser.Parse( - new Part[] - { - new LiteralText("before"), - new StartSection("foo"), - new LiteralText("inside"), - new EndSection("bar"), - new LiteralText("after") - }) - .ToArray()); + Assert.Catch(() => parser.Parse(template, + new Part[] + { + new LiteralText("before"), + new Block("foo"), + new LiteralText("inside"), + new EndSection("bar"), + new LiteralText("after") + })); } } } \ No newline at end of file diff --git a/Nustache.Tests/Describe_Scanner_Scan.cs b/Nustache.Tests/Describe_Scanner_Scan.cs index 637a34a..cfc332d 100644 --- a/Nustache.Tests/Describe_Scanner_Scan.cs +++ b/Nustache.Tests/Describe_Scanner_Scan.cs @@ -59,7 +59,7 @@ public void It_scans_sections() CollectionAssert.AreEqual(new Part[] { - new StartSection("foo"), + new Block("foo"), new LiteralText("inside"), new EndSection("foo"), }, diff --git a/Nustache.Tests/Describe_Container.cs b/Nustache.Tests/Describe_Section.cs similarity index 57% rename from Nustache.Tests/Describe_Container.cs rename to Nustache.Tests/Describe_Section.cs index f728b6b..7ba90e2 100644 --- a/Nustache.Tests/Describe_Container.cs +++ b/Nustache.Tests/Describe_Section.cs @@ -5,15 +5,15 @@ namespace Nustache.Tests { [TestFixture] - public class Describe_Container + public class Describe_Section { [Test] public void It_holds_parts_added_to_it() { - var container = new Container("foo"); + var section = new Section("foo"); - container.Add(new LiteralText("bar")); - container.Add(new LiteralText("baz")); + section.Add(new LiteralText("bar")); + section.Add(new LiteralText("baz")); CollectionAssert.AreEqual( new Part[] @@ -21,17 +21,17 @@ public void It_holds_parts_added_to_it() new LiteralText("bar"), new LiteralText("baz") }, - container.Children.ToArray()); + section.Children.ToArray()); } [Test] public void It_does_not_treat_template_definitions_as_children() { - var container = new Container("foo"); + var section = new Section("foo"); - container.Add(new LiteralText("bar")); - container.Add(new TemplateDefinition("baz")); - container.Add(new LiteralText("quux")); + section.Add(new LiteralText("bar")); + section.Add(new TemplateDefinition("baz")); + section.Add(new LiteralText("quux")); CollectionAssert.AreEqual( new Part[] @@ -39,17 +39,17 @@ public void It_does_not_treat_template_definitions_as_children() new LiteralText("bar"), new LiteralText("quux") }, - container.Children.ToArray()); + section.Children.ToArray()); } [Test] public void It_allows_you_to_look_up_template_definitions_by_name() { - var container = new Container("foo"); + var section = new Section("foo"); var templateDefinition = new TemplateDefinition("bar"); - container.Add(templateDefinition); + section.Add(templateDefinition); - var actual = container.GetTemplateDefinition(templateDefinition.Name); + var actual = section.GetTemplateDefinition(templateDefinition.Name); Assert.AreSame(templateDefinition, actual); } diff --git a/Nustache.Tests/Nustache.Tests.csproj b/Nustache.Tests/Nustache.Tests.csproj index 682b3d8..9f67f3a 100644 --- a/Nustache.Tests/Nustache.Tests.csproj +++ b/Nustache.Tests/Nustache.Tests.csproj @@ -49,7 +49,7 @@ - +