-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EXPERIMENTAL: Add Handlebars Support (#32)
Prototyping and exploration of what Handlebars would look likein. Handlebars.Net as the supported engine Handlebar content can be rendered using files with `.hbs` extension in VSCode extension. Enables nested JSON types in the attributes to support more complex attributes
- Loading branch information
1 parent
ed70dea
commit a20f550
Showing
28 changed files
with
300 additions
and
34 deletions.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
src/Sage.Engine.Tests/Corpus/Handlebars/eachHelper.subscribercontext.json
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,10 @@ | ||
{ | ||
"people": [ | ||
{ | ||
"name": "Adam" | ||
}, | ||
{ | ||
"name": "Howard" | ||
} | ||
] | ||
} |
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,6 @@ | ||
=========== | ||
eachHelper | ||
=========== | ||
{{#each people}}<h1>{{name}}</h1>{{/each}} | ||
++++++++++ | ||
<h1>Adam</h1><h1>Howard</h1> |
4 changes: 4 additions & 0 deletions
4
src/Sage.Engine.Tests/Corpus/Handlebars/ifHelper.subscribercontext.json
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,4 @@ | ||
{ | ||
"isActiveFalse": false, | ||
"isActiveTrue": true | ||
} |
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,12 @@ | ||
=========== | ||
If True Block | ||
=========== | ||
{{#if isActiveTrue}}hello{{else}}world{{/if}} | ||
++++++++++ | ||
hello | ||
=========== | ||
If False Block | ||
=========== | ||
{{#if isActiveFalse}}hello{{else}}world{{/if}} | ||
++++++++++ | ||
world |
4 changes: 4 additions & 0 deletions
4
src/Sage.Engine.Tests/Corpus/Handlebars/stringEncoding.subscribercontext.json
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,4 @@ | ||
{ | ||
"titleTags": "First Template <p> Tags", | ||
"titleNoTags": "First Template No Tags" | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Sage.Engine.Tests/Corpus/Handlebars/stringEncoding.txt
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,12 @@ | ||
=========== | ||
Escaping | ||
=========== | ||
{{titleTags}} | ||
++++++++++ | ||
First Template <p> Tags | ||
=========== | ||
No Escaping | ||
=========== | ||
{{titleNoTags}} | ||
++++++++++ | ||
First Template No Tags |
4 changes: 4 additions & 0 deletions
4
src/Sage.Engine.Tests/Corpus/Handlebars/unlessHelper.subscribercontext.json
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,4 @@ | ||
{ | ||
"isActiveTrue": true, | ||
"isActiveFalse": false | ||
} |
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,11 @@ | ||
=========== | ||
Unless Helper False | ||
=========== | ||
{{#unless isActiveFalse}}hello{{/unless}} | ||
++++++++++ | ||
hello | ||
=========== | ||
Unless Helper True | ||
=========== | ||
{{#unless isActiveTrue}}hello{{/unless}} | ||
++++++++++ |
6 changes: 6 additions & 0 deletions
6
src/Sage.Engine.Tests/Corpus/Handlebars/withHelper.subscribercontext.json
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,6 @@ | ||
{ | ||
"primary": { | ||
"name": "Adam" | ||
}, | ||
"name": "Howard" | ||
} |
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,6 @@ | ||
=========== | ||
WithHelper Tests | ||
=========== | ||
{{#with primary}}{{name}}{{/with}} | ||
++++++++++ | ||
Adam |
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,32 @@ | ||
// Copyright (c) 2022, salesforce.com, inc. | ||
// All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/Apache-2.0 | ||
|
||
namespace Sage.Engine.Tests | ||
{ | ||
using NUnit.Framework; | ||
|
||
/// <summary> | ||
/// Basic tests for the language features and runtime | ||
/// </summary> | ||
public class HandlebarTests : SageTest | ||
{ | ||
[Test] | ||
[RuntimeTest("Handlebars")] | ||
public EngineTestResult TestHandlebars(CorpusData test) | ||
{ | ||
try | ||
{ | ||
var result = TestUtils.GetOutputFromHandlebarTest(_serviceProvider, test); | ||
Assert.That(result.Output, Is.EqualTo(test.Output)); | ||
return result; | ||
} | ||
catch (CompileCodeException e) | ||
{ | ||
Assert.Fail(e.Message); | ||
return new EngineTestResult("!"); | ||
} | ||
} | ||
} | ||
} |
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
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
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,44 @@ | ||
// Copyright (c) 2023, salesforce.com, inc. | ||
// All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/Apache-2.0 | ||
|
||
using HandlebarsDotNet; | ||
using HandlebarsDotNet.Extension.Json; | ||
using Sage.Engine.Compiler; | ||
using Sage.Engine.Runtime; | ||
|
||
namespace Sage.Engine.Handlebars | ||
{ | ||
/// <summary> | ||
/// Uses HandlebarsDotNet to compile content to a string. | ||
/// | ||
/// Does not support AMPscript embedded in the content. | ||
/// </summary> | ||
internal class HandlebarsCompiler : IHandlebarsCompiler | ||
{ | ||
private IHandlebars _handlebar; | ||
|
||
public HandlebarsCompiler() | ||
{ | ||
_handlebar = HandlebarsDotNet.Handlebars.Create(); | ||
_handlebar.Configuration.UseJson(); | ||
} | ||
|
||
public bool CanCompile(IContent content) | ||
{ | ||
return content.ContentType == ContentType.Handlebars; | ||
} | ||
|
||
public string Compile(CompilationOptions options, RuntimeContext runtimeContext, SubscriberContext? subscriberContext) | ||
{ | ||
var template = _handlebar.Compile(options.Content.GetTextReader()); | ||
|
||
StringWriter writer = new StringWriter(); | ||
|
||
template(writer, subscriberContext?.GetRichAttributes()); | ||
|
||
return writer.ToString(); | ||
} | ||
} | ||
} |
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,14 @@ | ||
// Copyright (c) 2023, salesforce.com, inc. | ||
// All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/Apache-2.0 | ||
|
||
namespace Sage.Engine.Handlebars | ||
{ | ||
/// <summary> | ||
/// Renders a piece of Handlebars content to a string. | ||
/// </summary> | ||
public interface IHandlebarsCompiler : ICompiler | ||
{ | ||
} | ||
} |
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.