Skip to content

Commit

Permalink
Merge pull request #82 from jdiamond/dev
Browse files Browse the repository at this point in the history
Release: 1.13.9.3

Continuous Integration Setup and Nustache.Compilation Release
  • Loading branch information
Romanx committed Mar 29, 2015
2 parents bdd6f8c + a12e282 commit b834abb
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Nustache.Compilation.Tests/Compiled_Templates_Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void Multiple_Levels_of_Enumerable_Sections()
new SubObject { SubText = "c" },
}
});
Assert.AreEqual("A template witha TrueBlahThirdc ", result.Replace("\r\n", ""));
Assert.AreEqual("A template witha TrueBlahThirdc ", result.Replace("\r\n", "").Replace("\n", "").Replace("\r", ""));
}

[Test]
Expand Down Expand Up @@ -231,7 +231,7 @@ public void Internal_Templates_AKA_Inline_Partials()
var compiled = template.Compile<TestObject>(null);

var result = compiled(new TestObject { Sub = new SubObject { SubText = "Byaaah" } });
Assert.AreEqual("in partial Byaaah after partial", result.Replace("\r\n", ""));
Assert.AreEqual("in partial Byaaah after partial", result.Replace("\r\n", "").Replace("\n", "").Replace("\r", ""));
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions Nustache.Compilation.Tests/Mustache_Spec/MustacheSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static JObject GetSpecs(JToken[] specDefinition)

private static string StripReservedWords(string s)
{
s = new Regex(@"\{\s*[#\/\^]?string\s*\}\}").Replace(s, eval => eval.Value.Replace("string", "string1"));
s = new Regex(@"\{\s*[#\/\^]?bool\s*\}\}").Replace(s, eval => eval.Value.Replace("bool", "bool1"));
s = new Regex(@"\{\{\s*[#\/\^&]?\s*string\s*\}\}").Replace(s, eval => eval.Value.Replace("string", "string1"));
s = new Regex(@"\{\{\s*[#\/\^&]?\s*bool\s*\}\}").Replace(s, eval => eval.Value.Replace("bool", "bool1"));

return s.Replace("\"string\"", "\"string1\"")
.Replace("\"bool\"", "\"bool1\"");
Expand Down
16 changes: 13 additions & 3 deletions Nustache.Compilation.Tests/Mustache_Spec/Official_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ private static void RunMustacheSpecs(MustacheSpec.MustacheTest test)
!test.Name.ToLower().Contains("broken chain") &&
!(test.SpecName == "inverted" && (test.Name == "List" || test.Name == "Context")))
{
var compiledTemplate = templ.Compile(
try
{
var compiledTemplate = templ.Compile(
test.StronglyTypedExample != null ? test.StronglyTypedExample.GetType() : typeof(object),
testDataTemplateLocator);
rendered = compiledTemplate(test.StronglyTypedExample);
Assert.AreEqual(test.Expected, rendered, "Compiled Template rendering failed for " + test.Description);
rendered = compiledTemplate(test.StronglyTypedExample);
Assert.AreEqual(test.Expected, rendered, "Compiled Template rendering failed for " + test.Description);
}
catch (Nustache.Core.NustacheException ex)
{
if (ex.Message.StartsWith("Unsupported:"))
{
Assert.Inconclusive(ex.Message);
}
}
}
else
{
Expand Down
12 changes: 10 additions & 2 deletions Nustache.Compilation/CompileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
using System.Linq.Expressions;
using System.Collections;
using Nustache.Core;
using System.Text.RegularExpressions;

namespace Nustache.Compilation
{
public class CompileContext
{
private int IncludeLimit = 255;
private int _includeLevel = 0;
public string _indent;
public readonly Regex _indenter = new Regex("\n(?!$)");
public bool _lineEnded;

private readonly Type targetType;
private readonly RenderContext renderContext;
Expand Down Expand Up @@ -174,7 +178,7 @@ internal void Pop()
_targetObjectStack.Pop();
}

internal Expression Include(string templateName)
internal Expression Include(string templateName, string indent)
{
if (_includeLevel >= IncludeLimit)
{
Expand All @@ -184,12 +188,15 @@ internal Expression Include(string templateName)

_includeLevel++;

var oldIndent = _indent;
_indent = (_indent ?? "") + (indent ?? "");

Expression compiled = null;

TemplateDefinition templateDefinition = GetTemplateDefinition(templateName);

if (_includedTemplates.Contains(templateName))
throw new NustacheException("Compiled recursive templates will be supported in a later release");
throw new NustacheException("Unsupported: Compiled recursive templates will be supported in a later release");
_includedTemplates.Add(templateName);

if (templateDefinition != null)
Expand All @@ -205,6 +212,7 @@ internal Expression Include(string templateName)
compiled = template.Compile(this);
}
}
_indent = oldIndent;

_includeLevel--;

Expand Down
13 changes: 8 additions & 5 deletions Nustache.Compilation/CompilePartVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ public void Visit(Block block)

return expression;
}));

}

public void Visit(LiteralText literal)
{
parts.Add(Expression.Constant(literal.Text, typeof(string)));
var text = literal.Text;

parts.Add(CompoundExpression.IndentCheck(Expression.Constant(text, typeof(string)), context));

context._lineEnded = text.Length > 0 && text[text.Length - 1] == '\n';
}

public void Visit(EndSection endSections)
Expand All @@ -105,7 +108,7 @@ public void Visit(InvertedBlock invertedBlock)

public void Visit(TemplateInclude include)
{
parts.Add(context.Include(include.Name));
parts.Add(context.Include(include.Name, include.Indent));
}

public void Visit(VariableReference variable)
Expand All @@ -116,11 +119,11 @@ public void Visit(VariableReference variable)

if (variable.Escaped)
{
parts.Add(Expression.Call(null, typeof(Encoders).GetMethod("DefaultHtmlEncode"), getter));
parts.Add(CompoundExpression.IndentOnLineEnd(Expression.Call(null, typeof(Encoders).GetMethod("DefaultHtmlEncode"), getter), context));
}
else
{
parts.Add(getter);
parts.Add(CompoundExpression.IndentOnLineEnd(getter, context));
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions Nustache.Compilation/CompoundExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Linq.Expressions;
using System.Collections;
using System.Text.RegularExpressions;

namespace Nustache.Compilation
{
Expand Down Expand Up @@ -56,6 +57,38 @@ public static Expression Enumerator(Func<Expression, Expression> itemCallback, E
return block;
}

public static Expression IndentCheck(Expression expression, CompileContext context)
{
if (context._indent == null) return expression;

var regex = Expression.Variable(typeof(Regex));

return Expression.Block(
new[] { regex },
Expression.Assign(regex, Expression.New(typeof(Regex).GetConstructor(new[] { typeof(String) }), new List<Expression>() { Expression.Constant("\n(?!$)", typeof(String)) })),
Expression.Call(regex, typeof(Regex).GetMethod("Replace", new[] { typeof(String), typeof(String) }), new List<Expression>()
{
IndentOnLineEnd(expression, context),
Expression.Constant("\n" + context._indent)
})
);
}

public static Expression IndentOnLineEnd(Expression expression, CompileContext context)
{
if (context._indent != null && context._lineEnded)
{
var expr = Expression.Call(typeof(String).GetMethod("Concat", new [] { typeof(String), typeof(String) }), new List<Expression> {
Expression.Constant(context._indent, typeof(String)),
expression
});
context._lineEnded = false;
return expr;
}

return expression;
}

internal static Expression NullCheck(Expression expression, string nullValue = "", Expression returnIfNotNull = null)
{
if (returnIfNotNull == null) returnIfNotNull = expression;
Expand Down
1 change: 1 addition & 0 deletions Nustache.Compilation/Nustache.Compilation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Nustache.Compilation.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
18 changes: 18 additions & 0 deletions Nustache.Compilation/Nustache.Compilation.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<package>
<metadata>
<id>Nustache.Compilation</id>
<version>$version$</version>
<authors>Jason Diamond</authors>
<owners>Jason Diamond</owners>
<licenseUrl>https://raw.github.com/jdiamond/Nustache/master/LICENSE.txt</licenseUrl>
<projectUrl>https://github.com/jdiamond/Nustache</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<tags>Template Mustache</tags>
<dependencies>
<dependency id="Nustache" version="1.12.2.12" />
<dependency id="Newtonsoft.Json" version="5.0.6" />
</dependencies>
</metadata>
</package>
10 changes: 5 additions & 5 deletions Nustache.Compilation/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Nustache.Compilation")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Compliation for Logic-less templates for .NET")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyCompany("Jason Diamond")]
[assembly: AssemblyProduct("Nustache.Compilation")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2013")]
[assembly: AssemblyCopyright("Copyright © 2010-2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.13.8.22")]
[assembly: AssemblyFileVersion("1.13.8.22")]
1 change: 1 addition & 0 deletions Nustache.Mvc3.Example/Nustache.Mvc3.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>4.0</OldToolsVersion>
<MvcProjectUpgradeChecked>true</MvcProjectUpgradeChecked>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
30 changes: 29 additions & 1 deletion Nustache.Mvc3/Nustache.Mvc3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,38 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.Helpers.dll</HintPath>
</Reference>
<Reference Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.3.0.50813.1\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Razor.1.0.20105.408\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Deployment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.Deployment.dll</HintPath>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="NustacheView.cs" />
Expand All @@ -52,6 +79,7 @@
<None Include="Nustache.Mvc3.nuspec">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
7 changes: 7 additions & 0 deletions Nustache.Mvc3/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="3.0.50813.1" targetFramework="net40" developmentDependency="true" />
<package id="Microsoft.AspNet.Razor" version="1.0.20105.408" targetFramework="net40" developmentDependency="true" />
<package id="Microsoft.AspNet.WebPages" version="1.0.20105.408" targetFramework="net40" developmentDependency="true" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" developmentDependency="true" />
</packages>
6 changes: 3 additions & 3 deletions Nustache.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9D6A76C1-E840-4BB4-AAA6-CA39BECEAF21}"
ProjectSection(SolutionItems) = preProject
HISTORY.txt = HISTORY.txt
Expand Down Expand Up @@ -42,9 +44,7 @@ Global
{872BE5F3-E52A-45C8-BEEB-1A9540F24153}.Release|Any CPU.ActiveCfg = Release|Any CPU
{872BE5F3-E52A-45C8-BEEB-1A9540F24153}.Release|Any CPU.Build.0 = Release|Any CPU
{326AEAC1-FBCD-4FB2-8141-20F873CFF68F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{326AEAC1-FBCD-4FB2-8141-20F873CFF68F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{326AEAC1-FBCD-4FB2-8141-20F873CFF68F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{326AEAC1-FBCD-4FB2-8141-20F873CFF68F}.Release|Any CPU.Build.0 = Release|Any CPU
{BB62330D-3604-40EB-9B76-39CE36F1AD6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB62330D-3604-40EB-9B76-39CE36F1AD6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB62330D-3604-40EB-9B76-39CE36F1AD6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Loading

0 comments on commit b834abb

Please sign in to comment.