Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions azure-pipeline-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ steps:
displayName: 'Begin analysis on SonarCloud'
condition: and(succeeded(), eq(variables['RUN_SONAR'], 'yes'), ne(variables['Build.Reason'], 'PullRequest')) # Do not run for PullRequests

# Build source, tests and run tests for net452 and net8.0 (with coverage)
# Build source, tests and run tests for net8.0 (with coverage)
- script: |
dotnet test ./test/Handlebars.Net.Helpers.Tests/Handlebars.Net.Helpers.Tests.csproj --configuration Debug --framework net452
dotnet test ./test/Handlebars.Net.Helpers.Tests/Handlebars.Net.Helpers.Tests.csproj --configuration Debug --framework net8.0 --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
displayName: 'Build source, tests and run tests for net452 and net8.0 (with coverage)'
displayName: 'Build source, tests and run tests (with coverage)'

- script: |
dotnet sonarscanner end /d:sonar.token="$(SONAR_TOKEN)"
Expand Down
123 changes: 76 additions & 47 deletions examples/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using HandlebarsDotNet;
using HandlebarsDotNet.Helpers;
using HandlebarsDotNet.Helpers.Enums;
using HandlebarsDotNet.Helpers.Options;
using Newtonsoft.Json.Linq;

namespace ConsoleApp;
Expand Down Expand Up @@ -47,7 +50,19 @@ static void Main(string[] args)
var template1 = handlebars.Compile("{{ifCond MyData eq 01 X Y}}");
var result1 = template1.Invoke(new { MyData = "01" });

HandlebarsHelpers.Register(handlebars, options => { options.UseCategoryPrefix = false; });
HandlebarsHelpers.Register(handlebars, options =>
{
var categories = HandlebarsHelpersOptions.DefaultAllowedHandlebarsHelpers.ToList();
categories.Add(Category.Environment);
categories.Add(Category.DynamicLinq);

options.DynamicLinqHelperOptions = new HandlebarsDynamicLinqHelperOptions
{
AllowEqualsAndToStringMethodsOnObject = true
};
options.Categories = categories.ToArray();
options.UseCategoryPrefix = false;
});

//handlebars.RegisterHelper("ArrayTest", (context, arguments) =>
//{
Expand Down Expand Up @@ -146,9 +161,9 @@ static void Main(string[] args)
"{{Count a }}",
"{{Count a 'it.Contains(\"o\")'}}",
"{{Count a 'it.Contains(\"z\")'}}",
"{{DynamicLinq.Max a }}",
"{{DynamicLinq.Max a 'it.Contains(\"o\")'}}",
"{{DynamicLinq.Max a 'it.Contains(\"z\")'}}",
"{{DynamicLinq.Min i }}",
"{{DynamicLinq.Max i }}",
"{{DynamicLinq.Sum i }}",
"{{Any a 'it.Contains(\"o\")'}}",
"{{All a 'it.Contains(\"o\")'}}",
"{{All a 'it.Length > 1'}}",
Expand All @@ -161,7 +176,6 @@ static void Main(string[] args)
"{{OrderBy a 'it'}}",
"{{OrderBy a 'it desc'}}",
"{{OfType a 'int'}}",
"{{DynamicLinq.Sum i}}",

"JObject {{Count o.a }}",
"JObject {{Where o.a 'it.Contains(\"s\")'}}",
Expand All @@ -171,41 +185,42 @@ static void Main(string[] args)
"{{a.length}}"
};

foreach (string test in tests)
var p1 = new JObject
{
var p1 = new JObject
{
{ "X", new JValue("x") }
};
var p2 = new JObject
{
{ "X", new JValue("y") }
};
var o = new JObject
{
{ "Id", new JValue(9) },
{ "Name", new JValue("Test") },
{ "a", new JArray("stef", "test", "other") },
{ "a2", new JArray(p1, p2) }
};
{ "X", new JValue("x") }
};
var p2 = new JObject
{
{ "X", new JValue("y") }
};
var o = new JObject
{
{ "Id", new JValue(9) },
{ "Name", new JValue("Test") },
{ "a", new JArray("stef", "test", "other") },
{ "a2", new JArray(p1, p2) }
};

var dictionary = new Dictionary<string, object>
{
{ "1", "one" },
{ "2", "two" },
{ "d", "ddd" }
};
var dictionary = new Dictionary<string, object>
{
{ "1", "one" },
{ "2", "two" },
{ "d", "ddd" }
};

var t = new
{
x = DateTime.Now,
i = new[] { 1, 2, 4 },
a = new[] { "stef", "test", "other" },
dup = new[] { "stef", "stef", "other" },
d = new[] { new DateTime(2022, 1, 1), DateTime.Now },
o = o,
data = dictionary
};
var t = new
{
x = DateTime.Now,
i = new[] { 1, 2, 4 },
a = new[] { "stef", "test", "other" },
dup = new[] { "stef", "stef", "other" },
d = new[] { new DateTime(2022, 1, 1), DateTime.Now },
o = o,
data = dictionary
};

foreach (string test in tests)
{
var template = handlebars.Compile(test);
var result = template.Invoke(t);
Console.WriteLine($"{test} : '{result}'");
Expand All @@ -214,7 +229,20 @@ static void Main(string[] args)
Console.WriteLine(new string('-', 80));

var handlebars2 = Handlebars.Create();
HandlebarsHelpers.Register(handlebars2, options => { options.UseCategoryPrefix = true; });
HandlebarsHelpers.Register(handlebars2, options =>
{
var categories = HandlebarsHelpersOptions.DefaultAllowedHandlebarsHelpers.ToList();
categories.Add(Category.Environment);
categories.Add(Category.DynamicLinq);

options.DynamicLinqHelperOptions = new HandlebarsDynamicLinqHelperOptions
{
AllowEqualsAndToStringMethodsOnObject = true
};
options.Categories = categories.ToArray();
options.UseCategoryPrefix = true;
});


var tests2 = new[]
{
Expand All @@ -224,18 +252,19 @@ static void Main(string[] args)
"{{[DynamicLinq.Sum] i}}"
};

var t2 = new
{
x = DateTime.Now,
i = new[] { 1, 2, 4 },
a = new[] { "stef", "test", "other" },
dup = new[] { "stef", "stef", "other" },
d = new[] { new DateTime(2022, 1, 1), DateTime.Now }
};

foreach (string test in tests2)
{
var t = new
{
x = DateTime.Now,
i = new[] { 1, 2, 4 },
a = new[] { "stef", "test", "other" },
dup = new[] { "stef", "stef", "other" },
d = new[] { new DateTime(2022, 1, 1), DateTime.Now }
};
var template = handlebars2.Compile(test);
var result = template.Invoke(t);
var result = template.Invoke(t2);
Console.WriteLine($"{test} : '{result}'");
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>net451;net452;net46;netstandard1.3;netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<VersionPrefix>2.5.5</VersionPrefix>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
<VersionPrefix>2.6.0</VersionPrefix>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<Copyright>Copyright © 2020-2025 Stef Heyenrath</Copyright>
<Copyright>Copyright © 2020-2026 Stef Heyenrath</Copyright>
<Authors>Stef Heyenrath</Authors>
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/Handlebars-Net/Handlebars.Net.Helpers/master/resources/hbnet-icon.png</PackageIconUrl>
<PackageIcon>hbnet-icon.png</PackageIcon>
<PackageProjectUrl>https://github.com/Handlebars-Net/Handlebars.Net.Helpers</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Handlebars-Net/Handlebars.Net.Helpers</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<None Include="../../resources/hbnet-icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<!-- CVE-2019-0820 -->
<!--<ItemGroup>
--><!-- CVE-2019-0820 --><!--
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
</ItemGroup>-->

<Choose>
<!-- The environment variable `Prerelease` is set in the azure-pipelines.yml file. -->
Expand Down
6 changes: 1 addition & 5 deletions src/Handlebars.Net.Helpers.Core/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
global using global::System;
global using global::System.IO;
#if !(NET35 || NET40)
global using global::System.Threading;
global using global::System.Threading.Tasks;
#endif
global using global::System.IO;
44 changes: 16 additions & 28 deletions src/Handlebars.Net.Helpers.Core/Handlebars.Net.Helpers.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>HandlebarsDotNet.Helpers</RootNamespace>
<AssemblyName>HandlebarsDotNet.Helpers.Core</AssemblyName>
<Description>Core functionality for Handlebars.Net.Helpers</Description>
<PackageId>Handlebars.Net.Helpers.Core</PackageId>
<PackageTags>handlebars;helpers;extensions;core;models;interfaces;common</PackageTags>
<PackageTags>handlebars;helper;models;interfaces</PackageTags>
<ProjectGuid>{7CBD113C-A754-480B-B0D0-AA9DF734F612}</ProjectGuid>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../Handlebars.Net.Helpers.snk</AssemblyOriginatorKeyFile>
<PropertyGroup>
<RootNamespace>HandlebarsDotNet.Helpers</RootNamespace>
<AssemblyName>HandlebarsDotNet.Helpers.Core</AssemblyName>
<Description>Core functionality for Handlebars.Net.Helpers</Description>
<PackageId>Handlebars.Net.Helpers.Core</PackageId>
<PackageTags>handlebars;helpers;extensions;core;models;interfaces;common</PackageTags>
<PackageTags>handlebars;helper;models;interfaces</PackageTags>
<ProjectGuid>{7CBD113C-A754-480B-B0D0-AA9DF734F612}</ProjectGuid>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../Handlebars.Net.Helpers.snk</AssemblyOriginatorKeyFile>
<DefineConstants>$(DefineConstants);SIMPLE_JSON_INTERNAL</DefineConstants>
</PropertyGroup>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
<PackageReference Include="Nullable" Version="1.3.1">
<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="2.4.3" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Stef.Validation" Version="0.1.1" />
</ItemGroup>
<PackageReference Include="Stef.Validation" Version="0.3.0" />
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net451'">
<DefineConstants>$(DefineConstants);SIMPLE_JSON_DATACONTRACT;SIMPLE_JSON_DYNAMIC</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<DefineConstants>$(DefineConstants);SIMPLE_JSON_TYPEINFO</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
</ItemGroup>

</Project>
23 changes: 16 additions & 7 deletions src/Handlebars.Net.Helpers.Core/IO/PassthroughTextEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ public class PassthroughTextEncoder : ITextEncoder
/// </summary>
/// <param name="text">The StringBuilder containing the text.</param>
/// <param name="target">The TextWriter to write the text to.</param>
public void Encode(StringBuilder text, TextWriter target)
public void Encode(StringBuilder? text, TextWriter target)
{
target.Write(text);
if (text != null)
{
target.Write(text);
}
}

/// <summary>
/// Writes the given string text directly to the target TextWriter.
/// </summary>
/// <param name="text">The string containing the text.</param>
/// <param name="target">The TextWriter to write the text to.</param>
public void Encode(string text, TextWriter target)
public void Encode(string? text, TextWriter target)
{
target.Write(text);
if (text != null)
{
target.Write(text);
}
}

/// <summary>
Expand All @@ -34,11 +40,14 @@ public void Encode(string text, TextWriter target)
/// <typeparam name="T">The type of the text enumerator.</typeparam>
/// <param name="text">The enumerator containing the text.</param>
/// <param name="target">The TextWriter to write the text to.</param>
public void Encode<T>(T text, TextWriter target) where T : IEnumerator<char>
public void Encode<T>(T? text, TextWriter target) where T : IEnumerator<char>
{
while (text.MoveNext())
if (text != null)
{
target.Write(text.Current);
while (text.MoveNext())
{
target.Write(text.Current);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;

namespace HandlebarsDotNet.Helpers.Json;
namespace HandlebarsDotNet.Helpers.Json;

#if SIMPLE_JSON_DATACONTRACT
[GeneratedCode("simple-json", "1.0.0")]
Expand Down
1 change: 0 additions & 1 deletion src/Handlebars.Net.Helpers.Core/Json/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using Stef.Validation;

namespace HandlebarsDotNet.Helpers.Json;
Expand Down
4 changes: 2 additions & 2 deletions src/Handlebars.Net.Helpers.Core/Parsers/ArgumentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace HandlebarsDotNet.Helpers.Parsers;
public static class ArgumentsParser
{
private static readonly Type[] SupportedTypes = { typeof(int), typeof(long), typeof(double) };

public static List<object?> Parse(IHandlebars context, ParameterInfo[] parameters, Arguments arguments)
{
var result = new List<(object? Argument, object? DefaultValue)>();
Expand Down Expand Up @@ -52,7 +52,7 @@ public static class ArgumentsParser
}
}

return argument;
return argument ?? defaultValue;
}

public static object ParseAsIntLongOrDouble(IHandlebars context, object? value)
Expand Down
1 change: 0 additions & 1 deletion src/Handlebars.Net.Helpers.Core/Utils/DefaultValueCache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Concurrent;
using System.Reflection;

namespace HandlebarsDotNet.Helpers.Utils;

Expand All @@ -25,7 +24,7 @@
// Uses reflection to create a generic method call
return typeof(DefaultValueCreator<>)
.MakeGenericType(type)
.GetMethod("GetDefault")!

Check warning on line 27 in src/Handlebars.Net.Helpers.Core/Utils/DefaultValueCache.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this null-forgiving operator; nullable warnings are disabled here.

See more on https://sonarcloud.io/project/issues?id=Handlebars-Net_Handlebars.Net.Helpers&issues=AZ_nos1e86MzHGZiCRDJ&open=AZ_nos1e86MzHGZiCRDJ&pullRequest=139
.Invoke(null, null);
}

Expand Down
Loading