Permalink
Browse files

Some renaming and refactoring.

  • Loading branch information...
1 parent e88302c commit 4b61e74490f6b62bd7aeefcbc0f1d9cd4e2f439d @jdiamond jdiamond committed Oct 28, 2010
@@ -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
@@ -45,7 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="Container.cs" />
+ <Compile Include="Section.cs" />
<Compile Include="TemplateDefinition.cs" />
<Compile Include="Render.cs" />
<Compile Include="RenderContext.cs" />
@@ -55,7 +55,7 @@
<Compile Include="Parser.cs" />
<Compile Include="Part.cs" />
<Compile Include="Scanner.cs" />
- <Compile Include="StartSection.cs" />
+ <Compile Include="Block.cs" />
<Compile Include="Template.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TemplateInclude.cs" />
View
@@ -1,62 +1,49 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
namespace Nustache.Core
{
public class Parser
{
- public IEnumerable<Part> Parse(IEnumerable<Part> parts)
+ public void Parse(Section section, IEnumerable<Part> parts)
{
- var containerStack = new Stack<Container>();
- Container container = null;
+ if (section == null)
+ throw new ArgumentNullException("section");
+
+ var sectionStack = new Stack<Section>();
+ 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(
"End section {0} does not match any start section!",
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();
}
}
}
@@ -8,15 +8,15 @@ namespace Nustache.Core
public class RenderContext
{
private const int IncludeLimit = 1024;
- private readonly Stack<Container> _containerStack = new Stack<Container>();
+ private readonly Stack<Section> _sectionStack = new Stack<Section>();
private readonly Stack<object> _dataStack = new Stack<object>();
private readonly TextWriter _writer;
private readonly Func<string, Template> _templateLocator;
private int _includeLevel;
public RenderContext(Template template, object data, TextWriter writer, Func<string, Template> 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();
}
}
}
View
@@ -25,7 +25,7 @@ public IEnumerable<Part> Scan(string template)
if (marker[0] == '#')
{
- yield return new StartSection(marker.Substring(1));
+ yield return new Block(marker.Substring(1));
}
else if (marker[0] == '<')
{
@@ -2,14 +2,14 @@
namespace Nustache.Core
{
- public class Container : Part
+ public class Section : Part
{
private readonly string _name;
private readonly List<Part> _children = new List<Part>();
private readonly Dictionary<string, TemplateDefinition> _templateDefinitions =
new Dictionary<string, TemplateDefinition>();
- public Container(string name)
+ public Section(string name)
{
_name = name;
}
@@ -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));
}
/// <summary>
@@ -1,6 +1,6 @@
namespace Nustache.Core
{
- public class TemplateDefinition : Container
+ public class TemplateDefinition : Section
{
public TemplateDefinition(string name)
: base(name)
Oops, something went wrong.

0 comments on commit 4b61e74

Please sign in to comment.