Skip to content

Commit

Permalink
Lots of renaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiamond committed Oct 27, 2010
1 parent f5bac4b commit 5c2d2db
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 114 deletions.
33 changes: 0 additions & 33 deletions Nustache.Core/DefaultContext.cs

This file was deleted.

4 changes: 1 addition & 3 deletions Nustache.Core/EndSection.cs
@@ -1,5 +1,3 @@
using System.IO;

namespace Nustache.Core
{
public class EndSection : Part
Expand All @@ -16,7 +14,7 @@ public string Name
get { return _name; }
}

public override void Render(TextWriter writer, IContext context)
public override void Render(RenderContext context)
{
}

Expand Down
8 changes: 0 additions & 8 deletions Nustache.Core/IContext.cs

This file was deleted.

6 changes: 2 additions & 4 deletions Nustache.Core/LiteralText.cs
@@ -1,5 +1,3 @@
using System.IO;

namespace Nustache.Core
{
public class LiteralText : Part
Expand All @@ -11,9 +9,9 @@ public LiteralText(string text)
_text = text;
}

public override void Render(TextWriter writer, IContext context)
public override void Render(RenderContext context)
{
writer.Write(_text);
context.Write(_text);
}

#region Boring stuff
Expand Down
3 changes: 1 addition & 2 deletions Nustache.Core/Nustache.Core.csproj
Expand Up @@ -45,9 +45,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DefaultContext.cs" />
<Compile Include="RenderContext.cs" />
<Compile Include="EndSection.cs" />
<Compile Include="IContext.cs" />
<Compile Include="LiteralText.cs" />
<Compile Include="NustacheException.cs" />
<Compile Include="Parser.cs" />
Expand Down
4 changes: 1 addition & 3 deletions Nustache.Core/Part.cs
@@ -1,9 +1,7 @@
using System.IO;

namespace Nustache.Core
{
public abstract class Part
{
public abstract void Render(TextWriter writer, IContext context);
public abstract void Render(RenderContext context);
}
}
79 changes: 79 additions & 0 deletions Nustache.Core/RenderContext.cs
@@ -0,0 +1,79 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Nustache.Core
{
public class RenderContext
{
private const BindingFlags DefaultBindingFlags =
BindingFlags.Instance |
BindingFlags.Public;

private readonly Stack<object> _stack = new Stack<object>();
private object _data;
private readonly TextWriter _writer;

public RenderContext(object data, TextWriter writer)
{
_data = data;
_writer = writer;
}

public object GetValue(string name)
{
if (name == ".") return _data;

if (_data == null) return null;

var propertyInfo = _data.GetType().GetProperty(name, DefaultBindingFlags);

if (propertyInfo == null) return "";

var value = propertyInfo.GetValue(_data, null);

return value;
}

public IEnumerable<object> GetValues(string name)
{
object value = GetValue(name);

if (value is bool)
{
if ((bool)value)
{
yield return value;
}
}
else if (value is IEnumerable && !(value is string))
{
foreach (var item in ((IEnumerable)value))
{
yield return item;
}
}
else if (value != null)
{
yield return value;
}
}

public void Push(object data)
{
_stack.Push(_data);
_data = data;
}

public void Pop()
{
_data = _stack.Pop();
}

public void Write(string text)
{
_writer.Write(text);
}
}
}
40 changes: 6 additions & 34 deletions Nustache.Core/StartSection.cs
@@ -1,6 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Nustache.Core
{
Expand All @@ -19,46 +17,20 @@ public string Name
get { return _name; }
}

public List<Part> Children { get { return _children; } }
public IList<Part> Children { get { return _children; } }

public override void Render(TextWriter writer, IContext context)
public override void Render(RenderContext context)
{
object value = context.GetValue(_name);

object current = context.Current;

foreach (var item in GetItems(value))
foreach (var value in context.GetValues(_name))
{
context.Current = item;
context.Push(value);

foreach (var child in _children)
{
child.Render(writer, context);
child.Render(context);
}
}

context.Current = current;
}

private static IEnumerable<object> GetItems(object value)
{
if (value is bool)
{
if ((bool)value)
{
yield return value;
}
}
else if (value is IEnumerable && !(value is string))
{
foreach (var item in ((IEnumerable)value))
{
yield return item;
}
}
else if (value != null)
{
yield return value;
context.Pop();
}
}

Expand Down
52 changes: 45 additions & 7 deletions Nustache.Core/Template.cs
@@ -1,25 +1,63 @@
using System.IO;
using System;
using System.Collections.Generic;
using System.IO;

namespace Nustache.Core
{
public static class Template
public class Template
{
public static string Render(string template, object data)
private IEnumerable<Part> _parts;

public void Load(TextReader reader)
{
var writer = new StringWriter();
var context = data as IContext ?? new DefaultContext(data);
string template = reader.ReadToEnd();

var scanner = new Scanner();
var parser = new Parser();

foreach (var part in parser.Parse(scanner.Scan(template)))
_parts = parser.Parse(scanner.Scan(template));
}

public void Render(object data, TextWriter writer)
{
var context = new RenderContext(data, writer);

foreach (var part in _parts)
{
part.Render(writer, context);
part.Render(context);
}

writer.Flush();
}

public static string RenderStringToString(string template, object data)
{
var reader = new StringReader(template);
var writer = new StringWriter();
Render(reader, data, writer);
return writer.GetStringBuilder().ToString();
}

public static string RenderFileToString(string templatePath, object data)
{
throw new NotImplementedException();
}

public static void RenderStringToFile(string template, object data, string outputPath)
{
throw new NotImplementedException();
}

public static void RenderFileToFile(string templatePath, object data, string outputPath)
{
throw new NotImplementedException();
}

public static void Render(TextReader reader, object data, TextWriter writer)
{
var template = new Template();
template.Load(reader);
template.Render(data, writer);
}
}
}
3 changes: 1 addition & 2 deletions Nustache.Core/TemplateInclude.cs
@@ -1,5 +1,4 @@
using System;
using System.IO;

namespace Nustache.Core
{
Expand All @@ -12,7 +11,7 @@ public TemplateInclude(string name)
_name = name;
}

public override void Render(TextWriter writer, IContext context)
public override void Render(RenderContext context)
{
throw new NotImplementedException();
}
Expand Down
6 changes: 2 additions & 4 deletions Nustache.Core/VariableMarker.cs
@@ -1,5 +1,3 @@
using System.IO;

namespace Nustache.Core
{
public class VariableMarker : Part
Expand All @@ -11,13 +9,13 @@ public VariableMarker(string name)
_name = name;
}

public override void Render(TextWriter writer, IContext context)
public override void Render(RenderContext context)
{
object value = context.GetValue(_name);

if (value != null)
{
writer.Write(value.ToString());
context.Write(value.ToString());
}
}

Expand Down

0 comments on commit 5c2d2db

Please sign in to comment.