Skip to content

Commit

Permalink
Merge branch 'perf/tokenizer'
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaris committed Nov 18, 2024
2 parents f4742a3 + 06d44e6 commit 8c6ad14
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 25 deletions.
2 changes: 1 addition & 1 deletion libs/FuManchu/Parser/SyntaxTree/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Block : SyntaxTreeNode
public Block(BlockBuilder source)
: this(
source.Type, source.Name, source.Children, source.Descriptor,
source.IsPartialBlock, source.IsPartialBlockContent)
source.IsPartialBlock, source.IsImplicitPartialBlockContent)
{
source.Reset();
}
Expand Down
2 changes: 1 addition & 1 deletion libs/FuManchu/Renderer/BlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void Render(Block target, RenderContext context, TextWriter writ
{
Block parametersBlock = target;

if (target.Type == BlockType.Tag)
if (target.Type == BlockType.Tag || target.Type == BlockType.PartialBlock || target.Type == BlockType.PartialBlockContent)
{
// The arguments will be provided by the TagElement instance.
parametersBlock = (Block)target.Children.First();
Expand Down
30 changes: 23 additions & 7 deletions libs/FuManchu/Renderer/PartialBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,35 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren
{
using (var scope = context.BeginScope(model))
{
if (maps is { Count: >0 })
{
scope.ScopeContext.SetParameters(maps);
}

context.Service.RunPartial(name, scope.ScopeContext, writer);
}
}
else if (maps is { Count: > 0 })
//else if (maps is { Count: > 0 })
//{
// using (var scope = context.BeginScope(maps))
// {
// context.Service.RunPartial(name, scope.ScopeContext, writer);
// }
//}
else
{
using (var scope = context.BeginScope(maps))
if (maps is { Count: > 0 })
{
context.Service.RunPartial(name, scope.ScopeContext, writer);
using (var scope = context.BeginScope(context.TemplateData.Model))
{
scope.ScopeContext.SetParameters(maps);
context.Service.RunPartial(name, scope.ScopeContext, writer);
}
}
else
{
context.Service.RunPartial(name, context, writer);
}
}
else
{
context.Service.RunPartial(name, context, writer);
}
}

Expand Down
47 changes: 45 additions & 2 deletions libs/FuManchu/Renderer/RenderContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This work is licensed under the terms of the MIT license.

// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

namespace FuManchu.Renderer;
Expand All @@ -16,7 +17,8 @@ namespace FuManchu.Renderer;
/// </summary>
public class RenderContext
{
private readonly Map _variables = new Map();
readonly Map _variables = new Map();
Map _parameters = new Map();

/// <summary>
/// Initializes a new instance of the <see cref="RenderContext"/> class.
Expand Down Expand Up @@ -103,6 +105,23 @@ public RenderContextScope BeginScope(object? model)
return @default;
}

/// <summary>
/// Gets the parameter with the given name.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="default">[Optional] The default value for the parameter.</param>
/// <returns>The parameter value.</returns>
public object? GetParameter(string name, object? @default)
{
object? value;
if (_variables.TryGetValue(name, out value))
{
return value;
}

return @default;
}

/// <summary>
/// Resolves the value represented by the given span.
/// </summary>
Expand Down Expand Up @@ -274,6 +293,11 @@ public RenderContextScope BeginScope(object? model)
expression = expression.Substring(5);
}

if (_parameters.TryGetValue(expression, out var parameterValue))
{
return parameterValue;
}

var modelMetadata = ExpressionMetadataProvider.FromStringExpression(expression, templateData, context.ModelMetadataProvider);
if (modelMetadata == null || !modelMetadata.Valid)
{
Expand All @@ -295,4 +319,23 @@ public void SetVariable(string name, object value)
{
_variables[name] = value;
}

/// <summary>
/// Sets the parameter with the given name.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="value">The variable value.</param>
public void SetParameter(string name, object value)
{
_parameters[name] = value;
}

/// <summary>
/// Sets all parameters for the current context.
/// </summary>
/// <param name="parameters">The parameter map.</param>
public void SetParameters(Map parameters)
{
_parameters = parameters ?? new();
}
}
2 changes: 1 addition & 1 deletion libs/FuManchu/Renderer/ZoneBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren

if (context.ParentRenderContext is not null)
{
return TryGetZone(name, context);
return TryGetZone(name, context.ParentRenderContext);
}

return (null, null);
Expand Down
6 changes: 3 additions & 3 deletions tests/FuManchu.Tests/Content/Addresses.partial.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
{{>content}}

{{#if billing}}
{{#>Column}}
{{#>Column width="25%"}}
{{>content}}
{{>Address title="Billing Address" address=billing}}
{{/content}}
{{/Column}}
{{/if}}

{{#if shipping}}
{{#>Column}}
{{#>Column width="75%"}}
{{>content}}
{{>Address title="Shipping Address" address=shipping}}
{{/content}}
{{/Column}}
{{/if}}

{{/content}}
{{/Row}}
{{/Row}}
4 changes: 2 additions & 2 deletions tests/FuManchu.Tests/Content/Column.partial.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<td style="overflow-wrap:break-word;word-break:break-word;padding:30px 15px;font-family:'Quicksand',sans-serif;" align="left" width="50%">
<td style="overflow-wrap:break-word;word-break:break-word;padding:30px 15px;font-family:'Quicksand',sans-serif;" align="left" width="{{#if width}}{{width}}{{^}}50%{{/if}}">
<div style="font-size: 14px; line-height: 160%; word-wrap: break-word;">
{{<content}}
</div>
</td>
</td>
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,19 @@



<td style="overflow-wrap:break-word;word-break:break-word;padding:30px 15px;font-family:'Quicksand',sans-serif;" align="left" width="50%">
<td style="overflow-wrap:break-word;word-break:break-word;padding:30px 15px;font-family:'Quicksand',sans-serif;" align="left" width="25%">
<div style="font-size: 14px; line-height: 160%; word-wrap: break-word;">

<div style="font-size: 14px; line-height: 160%; word-wrap: break-word;">
<strong>Billing Address</strong>
<div>

Matthew Abbott,<br />
1 Somestreet Lane,<br />

Somewhere,<br />
Co. Somewhere,<br />
AA11 1AA,<br />United Kingdom

</div>
</div>

</div>
</td>




Expand All @@ -290,6 +284,7 @@
</tbody>
</table>



<p><strong>Booked with:</strong> Jo</p>

Expand Down

0 comments on commit 8c6ad14

Please sign in to comment.