-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSMustache.psm1
710 lines (664 loc) · 32.3 KB
/
PSMustache.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#Requires -Version 5.1
enum MustacheTagType {
Root
Text
Comment
Interpolation
Delimiter
Partial
SectionStart
SectionEnd
InvertedStart
}
class MustacheTag {
[MustacheTag]$Parent = $null
[MustacheTagType]$Type = [MustacheTagType]::Root
[string]$Content = ''
[array]$Childs = @()
[bool]$Unescape = $false
[int]$TagStartPos
[int]$TagEndPos
[string]$DelimiterLeft = [PSMustache]::DelimiterLeftDefault
[string]$DelimiterRight = [PSMustache]::DelimiterRightDefault
[string] GetOuterContent() {
if ($this.Type -eq [MustacheTagType]::Root) {
return $this.Content
}
else {
# Find Root-Node
$rootParent = $this.Parent
while (($null -ne $rootParent.Parent) -and ($rootParent.Type -ne [MustacheTagType]::Root)) {
$rootParent = $rootParent.Parent
}
# Select Last ChildNode and Outer Content including Closing Tag in Sections/Inverted Sections
if ($this.Type -in [MustacheTagType]::SectionStart,[MustacheTagType]::InvertedStart) {
return $rootParent.Content.Substring($this.TagStartPos, ($this.Childs[$this.Childs.Length-1].TagEndPos) - $this.TagStartPos)
} else {
# Get Outer Content of regular Nodes
return $rootParent.Content.Substring($this.TagStartPos, $this.TagEndPos - $this.TagStartPos)
}
}
}
[string] GetInnerContent() {
if ($this.Type -notin [MustacheTagType]::SectionStart,[MustacheTagType]::InvertedStart) {
return $this.Content
}
else {
# No childs, no inner content...
if ($this.Childs.Length -le 1) {
return [string]::Empty
}
# Find Root-Node
$rootParent = $this.Parent
while (($null -ne $rootParent.Parent) -and ($rootParent.Type -ne [MustacheTagType]::Root)) {
$rootParent = $rootParent.Parent
}
# Select Content from beginning of first Child until end of last content tag (very last is Section End)
return $rootParent.Content.Substring($this.Childs[0].TagStartPos, $this.Childs[$this.Childs.Length - 1].TagStartPos - $this.Childs[0].TagStartPos)
}
}
}
class PSMustache {
static [string]$DelimiterLeftDefault = '{{'
static [string]$DelimiterRightDefault = '}}'
static [string]$DelimiterLeftUnescapeDefault = '{'
static [string]$DelimiterRightUnescapeDefault = '}'
static [string]$regexTag = "\s*(?<Operator>[!>=#&^/])?\s*(?<TagContent>.+)"
<#
\s* # Whitespaces before Operator
(?<Operator>[!>=#&^/])? # Operator of Tag, if any
\s* # Whitespaces after Operator
(?<TagContent>.+) # Tag-Content, normally Tag-Name (except comment-Tags)
#>
static [string] RenderTemplate([MustacheTag] $leaf, $valueStack, $partials) {
$retValue = ''
switch ($Leaf.Type) {
Text {
$retValue = $Leaf.Content
break
}
Interpolation {
$retValue = [PSMustache]::GetValue($Leaf, $ValueStack)
if (-not $Leaf.Unescape) {
$retValue = [System.Net.WebUtility]::HtmlEncode($retValue)
}
break
}
SectionStart {
$sectionValue = [PSMustache]::GetValue($Leaf, $ValueStack)
if ($sectionValue -is [MustacheTag]) {
$retValue += [PSMustache]::RenderTemplate($sectionValue, $valueStack, $null)
break
}
if ($sectionValue -is [string]) {
# When string, check if the string is not empty, otherwise it is equal to false
if (-not [string]::IsNullOrEmpty($sectionValue)) {
$sectionValue = @($sectionValue)
}
else {
$sectionValue = @()
}
}
elseif ($sectionValue -is [bool]) {
# When bool, check if it is true
if ($sectionValue) {
$sectionValue = @($sectionValue)
}
else {
$sectionValue = @()
}
}
# Ensure it is an array, if not already
if (-not ($sectionValue -is [array])) {
if ($null -ne $sectionValue) {
$sectionValue = @($sectionValue)
}
else {
$sectionValue = @()
}
}
# Loop through each array variable
foreach ($curVar in $sectionValue) {
# Attach each Child of the section template to the result set for each variable in the stack
foreach ($curChild in $Leaf.Childs) {
if ($curVar -is [array]) {
# Nested Arrays need to join as such
$retValue += [PSMustache]::RenderTemplate($curChild, @($curVar, $ValueStack), $partials)
}
else {
$retValue += [PSMustache]::RenderTemplate($curChild, @($curVar) + $ValueStack, $partials)
}
}
}
break
}
InvertedStart {
$sectionValue = [PSMustache]::GetValue($Leaf, $ValueStack)
if ($sectionValue -is [string]) {
# When string, check if the string is not empty, otherwise it is equal to false
$outputInverted = [string]::IsNullOrEmpty($sectionValue)
}
elseif ($sectionValue -is [bool]) {
# When bool, check if it is true
$outputInverted = -not $sectionValue
}
elseif ($sectionValue -is [array]) {
$outputInverted = $sectionValue.Length -eq 0
}
else {
$outputInverted = $null -eq $sectionValue
}
if ($outputInverted) {
foreach ($curChild in $Leaf.Childs) {
if ($sectionValue -is [array]) {
# Nested Arrays need to join as such
$retValue += [PSMustache]::RenderTemplate($curChild, @($sectionValue, $ValueStack), $partials)
}
else {
$retValue += [PSMustache]::RenderTemplate($curChild, @($sectionValue) + $ValueStack, $partials)
}
}
}
break
}
Partial {
$partial = $partials.($Leaf.Content.Trim())
if (-not [string]::IsNullOrEmpty($partial)) {
if ($leaf.Childs.Length -gt 0) {
# Append intentation to every newline except the last one when ending with a newline
$partial = $partial -replace "(\n)(?!$)", "`n$($leaf.Childs[0].Content)"
}
$tags = [PSMustache]::ParseTemplate($partial)
# Check if intendation Text Tag is appended
if ($leaf.Childs.Length -gt 0) {
# Prepend Intentation bevore the first element to preserve it
$tags.Childs = @($leaf.Childs[0]) + $tags.Childs
}
$retValue += [PSMustache]::RenderTemplate($tags, $ValueStack, $partials)
}
break
}
Root {
foreach ($curChild in $Leaf.Childs) {
$retValue += [PSMustache]::RenderTemplate($curChild, $ValueStack, $partials)
}
break
}
Default {}
}
return $retValue
}
static [object] GetValue([MustacheTag]$currentTag, [array]$valueStack) {
$valueName = $currentTag.Content.Trim() # Remove Whitespaces from variable name
if ($valueName -eq '.') {
# Return directly if dotted names
if ($valueStack.Count -gt 1) {
return $valueStack[0]
}
return $valueStack
}
# Loop through valueStack to find the value
foreach ($curValue in $valueStack) {
$curValueNamePart = $valueName.Split('.')
# Lookup if first part of the name in in the current Stack position
# If it matches, return the value down the dots, otherwise go stack up
if ((($curValue -is [hashtable]) -and ($curValueNamePart[0] -in $curValue.Keys)) -or (($curValue | Get-Member -MemberType NoteProperty | Where-Object Name -eq $curValueNamePart[0]).count -eq 1)) {
$curValueNamePart | ForEach-Object { $curValue = $curValue.$_ }
if ($curValue -is [scriptblock]) {
# ScriptBlock / Lambda-Expression
try {
$functionValue =''
if ($currentTag.Type -eq [MustacheTagType]::SectionStart) {
# Sections are returned as Tags as they replace the original Childs in the node
$functionValue = $curValue.Invoke($currentTag.GetInnerContent())
return [PSMustache]::ParseTemplate($functionValue, $currentTag.DelimiterLeft, $currentTag.DelimiterRight)
} else {
$functionValue = $curValue.Invoke()
$template = [PSMustache]::ParseTemplate($functionValue)
return [PSMustache]::RenderTemplate($template, $valueStack, $null)
}
}
catch {
return "ERROR";
}
}
else {
return $curValue
}
}
}
return $null
}
static [MustacheTag] ParseTemplate([string]$template) {
# Evaluate newLine
if ($Template -match "(?<newLine>\r?\n)") {
$templateNewLine = $Matches['newLine']
}
else {
$templateNewLine = $null
}
# Go with defaults
return [PSMustache]::ParseTemplate($template, [PSMustache]::DelimiterLeftDefault, [PSMustache]::DelimiterRightDefault, [PSMustache]::DelimiterLeftUnescapeDefault, [PSMustache]::DelimiterRightUnescapeDefault, $templateNewLine)
}
static [MustacheTag] ParseTemplate([string]$template, [string]$delimiterLeft, [string]$delimiterRight) {
# Evaluate newLine
if ($Template -match "(?<newLine>\r?\n)") {
$templateNewLine = $Matches['newLine']
}
else {
$templateNewLine = $null
}
# Go with defaults
return [PSMustache]::ParseTemplate($template, $delimiterLeft, $delimiterRight, [PSMustache]::DelimiterLeftUnescapeDefault, [PSMustache]::DelimiterRightUnescapeDefault, $templateNewLine)
}
static [MustacheTag] ParseTemplate([string]$template, [string]$delimiterLeft, [string]$delimiterRight, [string]$delimiterLeftUnescape, [string]$delimiterRightUnescape, [string]$templateNewLine) {
$parseTree = [MustacheTag]@{
Type = [MustacheTagType]::Root
Content = $Template
TagStartPos = 0
TagEndPos = $Template.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
$rootParent = $parseTree
$lastPosition = 0
$openTagPosition = -1
$closeTagPosition = 0
while (($openTagPosition = $Template.IndexOf($delimiterLeft, $lastPosition)) -ge 0) {
# Open Delimiter found, check if Unescape is used
$isUnescapeByTriple = $openTagPosition -eq $Template.IndexOf($delimiterLeft + $delimiterLeftUnescape, $openTagPosition, ($delimiterLeft + $delimiterLeftUnescape).Length)
if ($isUnescapeByTriple) {
# Find Close Delimiter incl. Unescape-Delimiter
$closeTagPosition = $Template.IndexOf($delimiterRight + $delimiterRightUnescape, $openTagPosition + $delimiterLeft.Length ) # Find Close Del, pos after Open Del
# Get Content excl. Unescape-Delimiter
$tagContent = $Template.Substring($openTagPosition + $delimiterLeft.Length + $delimiterLeftUnescape.Length, $closeTagPosition - $openTagPosition - $delimiterLeft.Length - $delimiterLeftUnescape.Length)
}
else {
# Find Close Delimiter
$closeTagPosition = $Template.IndexOf($delimiterRight, $openTagPosition + $delimiterLeft.Length) # Find Close Del, pos after Open Del
$tagContent = $Template.Substring($openTagPosition + $delimiterLeft.Length, $closeTagPosition - $openTagPosition - $delimiterLeft.Length)
}
$tagContent -match [PSMustache]::regexTag | Out-Null
$tagContentMatch = $Matches
if (($null -eq $tagContentMatch['Operator']) -or ($tagContentMatch['Operator'] -eq '&')) {
## Interpolation
# Text-Content before Tag, if any
if ($openTagPosition - $lastPosition -gt 0) {
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Text
Content = $Template.Substring($lastPosition, $openTagPosition - $lastPosition)
TagStartPos = $lastPosition
TagEndPos = $openTagPosition
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
}
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Interpolation
Content = $tagContentMatch['TagContent']
Unescape = ($isUnescapeByTriple -or ($tagContentMatch['Operator'] -eq '&'))
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length - 1
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
# Set Position after closing Tag
$lastPosition = $closeTagPosition + $delimiterRight.Length
if ($isUnescapeByTriple) {
$lastPosition += $delimiterRightUnEscape.Length
}
}
else {
# On other Tags than Interpolation the complete Line should be removed if standalone in a line
# Evaluate Position of previous and next Linebreak from perspective of captured Tag
if (-not ([string]::IsNullOrEmpty($templateNewLine))) {
## Variable previous LineBreak
# Pos. of previous Linebreak
$posPreviousLineBreak = $Template.LastIndexOf($templateNewLine, $openTagPosition)
# Pos of content beginning in current line
$posPreviousContentInLineBegin = -1
# Content in line before Opening Tag
$previousContentInLine = ''
## Variables next linebreak
# Pos. of next LineBreak
$posNextLineBreak = $Template.IndexOf($templateNewLine, $closeTagPosition)
# Pos of content beginning after linebreak
$posNextLineBreakContentBegin = -1
# Content in line after Closing Tag
$nextContentInLine = ''
if ($posPreviousLineBreak -eq -1) {
# Begin of String, no previous newline
$posPreviousContentInLineBegin = 0
}
else {
# Content begin after the previous
$posPreviousContentInLineBegin = $posPreviousLineBreak + $templateNewLine.Length
}
$previousContentInLine = $Template.Substring($posPreviousContentInLineBegin, $openTagPosition - $posPreviousContentInLineBegin)
# Pos. of next Linebreak
if ($posNextLineBreak -eq -1) {
# No upcoming linebreak
$nextContentInLine = $Template.Substring($closeTagPosition + $delimiterRight.Length)
}
else {
$posNextLineBreakContentBegin = $posNextLineBreak + $templateNewLine.Length # Content begin after linebreak
$nextContentInLine = $Template.Substring($closeTagPosition + $delimiterRight.Length, ($posNextLineBreak - ($closeTagPosition + $delimiterRight.Length))) # Get Content till linebreak, but not including
}
# Join previous and trailing content in line to check if it constists only out of whitespaces
$isWhiteSpaceLine = ($previousContentInLine + $nextContentInLine) -match "^\s*$"
if ($isWhiteSpaceLine) {
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Text
Content = $Template.Substring($lastPosition, $posPreviousContentInLineBegin - $lastPosition)
TagStartPos = $lastPosition
TagEndPos = $posPreviousContentInLineBegin
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
if ($posNextLineBreakContentBegin -eq -1) {
# Last line is a whitespace line, set lastPos to end of Template
$lastPosition = $Template.Length
}
else {
# Set LastPos to begin of Content after LineBreak
$lastPosition = $posNextLineBreakContentBegin
}
}
}
else {
$isWhiteSpaceLine = $false # No Whitespace line, since single line
$previousContentInLine = '' # Used for partial tag
}
# If whitespace-Line, add the Text of the previous line until the previous LineBreak and set position after newline of current line
if (-not $isWhiteSpaceLine) {
# No whitespace-Line, add content until opening tag and set lastpos after closing Tag
if ($openTagPosition - $lastPosition -gt 0) {
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Text
Content = $Template.Substring($lastPosition, $openTagPosition - $lastPosition)
TagStartPos = $lastPosition
TagEndPos = $openTagPosition
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
}
# Set Position after closing Tag
$lastPosition = $closeTagPosition + $delimiterRight.Length
}
# Add Tag itself
switch ($tagContentMatch['Operator']) {
'!' {
# Comment
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Comment
Content = $tagContent
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
break
}
'#' {
# Section start
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::SectionStart
Content = $tagContentMatch['TagContent']
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
# Set new Parent
$rootParent = $rootParent.Childs[$rootParent.Childs.Length - 1]
break
}
'^' {
# Inverted start
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::InvertedStart
Content = $tagContentMatch['TagContent']
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
# Set new Parent
$rootParent = $rootParent.Childs[$rootParent.Childs.Length - 1]
break
}
'/' {
# Section or inverted end
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::SectionEnd
Content = $tagContentMatch['TagContent']
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
# Revert Parent
$rootParent = $rootParent.Parent
break
}
'>' {
# Partials
$newPartial = [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Partial
Content = $tagContentMatch['TagContent']
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
if ($isWhiteSpaceLine -and $previousContentInLine -match "(\s+)") {
# Check if we have intended before the partial tag
$newPartial.Childs += [MustacheTag]@{
Parent = $newPartial
Type = [MustacheTagType]::Text
Content = $previousContentInLine
}
}
$rootParent.Childs += $newPartial
break
}
'=' {
# Delimiters
if ($tagContentMatch['TagContent'] -match "^(?<delLeft>\S+)\s+(?<delRight>\S+)\s*=$") {
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Delimiter
Content = $tagContentMatch['TagContent']
TagStartPos = $openTagPosition
TagEndPos = $closeTagPosition + $delimiterRight.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
$delimiterLeft = $Matches['delLeft']
$delimiterRight = $Matches['delRight']
}
}
}
}
}
# Add trailing text
if ($lastPosition -lt $template.Length) {
$rootParent.Childs += [MustacheTag]@{
Parent = $rootParent
Type = [MustacheTagType]::Text
Content = $Template.Substring($lastPosition)
TagStartPos = $lastPosition
TagEndPos = $template.Length
DelimiterLeft = $delimiterLeft
DelimiterRight = $delimiterRight
}
}
return $parseTree
}
}
<#
.SYNOPSIS
Parses a Mustache-Template, renders it with the given values and returns the result
.DESCRIPTION
PSMustache supports all mustache Tags excepts lambas.
Please refer to the official mustache documentation for more examples and details regarding the syntax.
Tag Reference in short:
- Interpolations: {{firstname}} is replaced by a value with name 'firstname'
- Sections: {{#persons}}Hi {{firstname}}! {{/persons}} is looped vor every member of the array 'persons'
- Inverteds: {{^persons}}No persons here.{{/persons}} is only rendered when a value with the name 'persons' does not exists or is empty
- Comments: {{! This is a comment }} will be removed
- Partials: {{> mypartial }} is replaced with the content of a partial with the name 'mypartial'
- Delimiters: {{=<% %>=}} set new delimiters which are used beyond that tag.
Details of the processing:
- Whitespaces in the tags are mostly ignored so {{ # persons }} and {{#persons}} are equal.
- All Tags except interpolation are completely removed when placed in a standalone line with only whitespaces.
- When partials are intended, the intentation is applied to each linebreak in the partial to preserve the intentation.
- {{.}} Can be used as a shortcut for the current element in a section
- All content is HTML-Encoded, when not excluded by triple Delimiter {{{rawContent}}} or the ampersand {{& rawContent}}
- If the delimiters are changed to a two length variant with identical chars, the Unescape-Delimiter will also be changed
e.g. {{=[[ ]]=}} changes the delimiters to [[content]] and [[[rawContent]]] for unescaped Content
.PARAMETER Template
A Mustache Template as string or a already processed template from Get-MustacheTemplate
.PARAMETER Values
All values which shall be used while rendering the template.
Values should be defined as hashtables and can include nested elements e.g.
@{
'Name' = 'Joe',
'Repos' = @('Repo1', 'Repo2', 'Repo3')
}
.PARAMETER Partials
Partials should be defined as hashtable, e.g. @{'myPartial' = 'Hi {{name}}'}
.PARAMETER DelimiterLeft
Custom Start-Delimiter which shall be used for processing
When DelimiterLeft is defined, DelimiterRight is also mandatory!
.PARAMETER DelimiterRight
Custom End-Delimiter which shall be used for processing
When DelimiterRight is defined, DelimiterLeft is also mandatory!
.EXAMPLE
PS> # Very Basic Interpolation:
PS> ConvertFrom-MustacheTemplate -Template 'Hi {{Name}}!' -Values @{Name='Joe'}
Hi Joe!
.EXAMPLE
PS> # Define Sections Template:
PS> $template = @'
>> My Repos:
>> {{#repos}}
>> * {{.}}
>> {{/repos}}
>> '@
PS> # Define Values:
PS> $values = @{Repos = @('PSMustache', 'AnotherRepo', 'Third Repo')}
PS> # Invoke with values
PS> ConvertFrom-MustacheTemplate -Template $template -Values $values
My Repos:
* PSMustache
* AnotherRepo
* Third Repo
PS> # Invoke without values
PS> ConvertFrom-MustacheTemplate -Template $template
My Repos:
.EXAMPLE
PS> # Define Sections with second inverted Section.
PS> # The inverted section is only evaluated when repos is empty or not found
PS> $template = @'
>> My Repos:
>> {{#repos}}
>> * {{.}}
>> {{/repos}}
>> {{^repos}}
>> - No Repos. :-(
>> {{/repos}}
>> '@
# Invoke without values
PS> ConvertFrom-MustacheTemplate -Template $template -
My Repos:
- No Repos. :-(
#>
function ConvertFrom-MustacheTemplate {
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([string])]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'Default')]
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter')]
[ValidateNotNull()]
[ValidateScript({($_ -is [string]) -or ($_ -is [MustacheTag])})]
$Template,
[Parameter(Mandatory = $true, ParameterSetName = 'Default', ValueFromPipeline = $true)]
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter', ValueFromPipeline = $true)]
$Values,
[Parameter(Mandatory = $false, ParameterSetName = 'Default')]
[Parameter(Mandatory = $false, ParameterSetName = 'Delimiter')]
[array]
$Partials,
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter')]
[string]
$DelimiterLeft,
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter')]
[string]
$DelimiterRight
)
begin {
if ($Template -is [MustacheTag]) {
$parseTree = $Template
} else {
if ($PsCmdlet.ParameterSetName -eq 'Delimiter') {
$parseTree = Get-MustacheTemplate -Template $Template -DelimiterLeft $DelimiterLeft -DelimiterRight $DelimiterRight
} else {
$parseTree = Get-MustacheTemplate -Template $Template
}
}
}
process {
return [PSMustache]::RenderTemplate($parseTree, $Values, $Partials)
}
}
<#
.SYNOPSIS
Parses a Template and returns the Template Object
.DESCRIPTION
Parses a Template to use it multiple times with ConvertFrom-MustacheTemplate.
Most processing time is used to parse the template, to it makes sense when the template is used multiple times.
.PARAMETER Template
A Mustache Template as string
.PARAMETER DelimiterLeft
Custom Start-Delimiter which shall be used for processing
When DelimiterLeft is defined, DelimiterRight is also mandatory!
.PARAMETER DelimiterRight
Custom End-Delimiter which shall be used for processing
When DelimiterRight is defined, DelimiterLeft is also mandatory!
.EXAMPLE
$template = Get-MustacheTemplate $myTemplate
$valueCollecton | ConvertFrom-MustacheTemplate -Template $template
#>
function Get-MustacheTemplate {
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([MustacheTag])]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'Default', ValueFromPipeline = $true)]
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter', ValueFromPipeline = $true)]
[string]
$Template,
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter')]
[string]
$DelimiterLeft,
[Parameter(Mandatory = $true, ParameterSetName = 'Delimiter')]
[string]
$DelimiterRight
)
process {
if ($PsCmdlet.ParameterSetName -eq 'Delimiter') {
return [PSMustache]::ParseTemplate($Template, $DelimiterLeft, $DelimiterRight)
} else {
return [PSMustache]::ParseTemplate($Template)
}
}
}