Skip to content

Commit

Permalink
Some renaming and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiamond committed Oct 28, 2010
1 parent e88302c commit 4b61e74
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 119 deletions.
12 changes: 6 additions & 6 deletions Nustache.Core/StartSection.cs → 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);
Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Nustache.Core/Nustache.Core.csproj
Expand Up @@ -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" />
Expand All @@ -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" />
Expand Down
45 changes: 16 additions & 29 deletions Nustache.Core/Parser.cs
@@ -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();
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions Nustache.Core/RenderContext.cs
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
}
}
}
2 changes: 1 addition & 1 deletion Nustache.Core/Scanner.cs
Expand Up @@ -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] == '<')
{
Expand Down
4 changes: 2 additions & 2 deletions Nustache.Core/Container.cs → Nustache.Core/Section.cs
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions Nustache.Core/Template.cs
Expand Up @@ -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.
{
}

Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion 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)
Expand Down

0 comments on commit 4b61e74

Please sign in to comment.