forked from jdiamond/Nustache
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
152 additions
and
114 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.