Skip to content

Commit 8c6ad14

Browse files
committed
Merge branch 'perf/tokenizer'
2 parents f4742a3 + 06d44e6 commit 8c6ad14

File tree

8 files changed

+79
-25
lines changed

8 files changed

+79
-25
lines changed

libs/FuManchu/Parser/SyntaxTree/Block.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Block : SyntaxTreeNode
2323
public Block(BlockBuilder source)
2424
: this(
2525
source.Type, source.Name, source.Children, source.Descriptor,
26-
source.IsPartialBlock, source.IsPartialBlockContent)
26+
source.IsPartialBlock, source.IsImplicitPartialBlockContent)
2727
{
2828
source.Reset();
2929
}

libs/FuManchu/Renderer/BlockRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public override void Render(Block target, RenderContext context, TextWriter writ
2121
{
2222
Block parametersBlock = target;
2323

24-
if (target.Type == BlockType.Tag)
24+
if (target.Type == BlockType.Tag || target.Type == BlockType.PartialBlock || target.Type == BlockType.PartialBlockContent)
2525
{
2626
// The arguments will be provided by the TagElement instance.
2727
parametersBlock = (Block)target.Children.First();

libs/FuManchu/Renderer/PartialBlockRenderer.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,35 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren
7070
{
7171
using (var scope = context.BeginScope(model))
7272
{
73+
if (maps is { Count: >0 })
74+
{
75+
scope.ScopeContext.SetParameters(maps);
76+
}
77+
7378
context.Service.RunPartial(name, scope.ScopeContext, writer);
7479
}
7580
}
76-
else if (maps is { Count: > 0 })
81+
//else if (maps is { Count: > 0 })
82+
//{
83+
// using (var scope = context.BeginScope(maps))
84+
// {
85+
// context.Service.RunPartial(name, scope.ScopeContext, writer);
86+
// }
87+
//}
88+
else
7789
{
78-
using (var scope = context.BeginScope(maps))
90+
if (maps is { Count: > 0 })
7991
{
80-
context.Service.RunPartial(name, scope.ScopeContext, writer);
92+
using (var scope = context.BeginScope(context.TemplateData.Model))
93+
{
94+
scope.ScopeContext.SetParameters(maps);
95+
context.Service.RunPartial(name, scope.ScopeContext, writer);
96+
}
97+
}
98+
else
99+
{
100+
context.Service.RunPartial(name, context, writer);
81101
}
82-
}
83-
else
84-
{
85-
context.Service.RunPartial(name, context, writer);
86102
}
87103
}
88104

libs/FuManchu/Renderer/RenderContext.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// This work is licensed under the terms of the MIT license.
1+

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

45
namespace FuManchu.Renderer;
@@ -16,7 +17,8 @@ namespace FuManchu.Renderer;
1617
/// </summary>
1718
public class RenderContext
1819
{
19-
private readonly Map _variables = new Map();
20+
readonly Map _variables = new Map();
21+
Map _parameters = new Map();
2022

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

108+
/// <summary>
109+
/// Gets the parameter with the given name.
110+
/// </summary>
111+
/// <param name="name">The parameter name.</param>
112+
/// <param name="default">[Optional] The default value for the parameter.</param>
113+
/// <returns>The parameter value.</returns>
114+
public object? GetParameter(string name, object? @default)
115+
{
116+
object? value;
117+
if (_variables.TryGetValue(name, out value))
118+
{
119+
return value;
120+
}
121+
122+
return @default;
123+
}
124+
106125
/// <summary>
107126
/// Resolves the value represented by the given span.
108127
/// </summary>
@@ -274,6 +293,11 @@ public RenderContextScope BeginScope(object? model)
274293
expression = expression.Substring(5);
275294
}
276295

296+
if (_parameters.TryGetValue(expression, out var parameterValue))
297+
{
298+
return parameterValue;
299+
}
300+
277301
var modelMetadata = ExpressionMetadataProvider.FromStringExpression(expression, templateData, context.ModelMetadataProvider);
278302
if (modelMetadata == null || !modelMetadata.Valid)
279303
{
@@ -295,4 +319,23 @@ public void SetVariable(string name, object value)
295319
{
296320
_variables[name] = value;
297321
}
322+
323+
/// <summary>
324+
/// Sets the parameter with the given name.
325+
/// </summary>
326+
/// <param name="name">The name of the variable.</param>
327+
/// <param name="value">The variable value.</param>
328+
public void SetParameter(string name, object value)
329+
{
330+
_parameters[name] = value;
331+
}
332+
333+
/// <summary>
334+
/// Sets all parameters for the current context.
335+
/// </summary>
336+
/// <param name="parameters">The parameter map.</param>
337+
public void SetParameters(Map parameters)
338+
{
339+
_parameters = parameters ?? new();
340+
}
298341
}

libs/FuManchu/Renderer/ZoneBlockRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren
4444

4545
if (context.ParentRenderContext is not null)
4646
{
47-
return TryGetZone(name, context);
47+
return TryGetZone(name, context.ParentRenderContext);
4848
}
4949

5050
return (null, null);

tests/FuManchu.Tests/Content/Addresses.partial.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
{{>content}}
33

44
{{#if billing}}
5-
{{#>Column}}
5+
{{#>Column width="25%"}}
66
{{>content}}
77
{{>Address title="Billing Address" address=billing}}
88
{{/content}}
99
{{/Column}}
1010
{{/if}}
1111

1212
{{#if shipping}}
13-
{{#>Column}}
13+
{{#>Column width="75%"}}
1414
{{>content}}
1515
{{>Address title="Shipping Address" address=shipping}}
1616
{{/content}}
1717
{{/Column}}
1818
{{/if}}
1919

2020
{{/content}}
21-
{{/Row}}
21+
{{/Row}}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<td style="overflow-wrap:break-word;word-break:break-word;padding:30px 15px;font-family:'Quicksand',sans-serif;" align="left" width="50%">
1+
<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}}">
22
<div style="font-size: 14px; line-height: 160%; word-wrap: break-word;">
33
{{<content}}
44
</div>
5-
</td>
5+
</td>

tests/FuManchu.Tests/Verification/ComplexDocumentFacts.CanVerifyComplexDocument.verified.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,19 @@
262262

263263

264264

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

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

272-
Matthew Abbott,<br />
273-
1 Somestreet Lane,<br />
274-
275-
Somewhere,<br />
276-
Co. Somewhere,<br />
277-
AA11 1AA,<br />United Kingdom
278-
279272
</div>
280273
</div>
281274

282275
</div>
283276
</td>
277+
284278

285279

286280

@@ -290,6 +284,7 @@
290284
</tbody>
291285
</table>
292286

287+
293288

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

0 commit comments

Comments
 (0)