Skip to content

Commit

Permalink
feat: Porting JSON Template to Language Function (Fixes #605)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Nov 11, 2023
1 parent cfc00dc commit 5b5c10e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 90 deletions.
46 changes: 46 additions & 0 deletions Commands/Languages/JSON/JSON-Language.ps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Language function JSON {
<#
.SYNOPSIS
JSON PipeScript Transpiler.
.DESCRIPTION
Allows PipeScript to generate JSON.
Multiline comments blocks like ```/*{}*/``` will be treated as blocks of PipeScript.
String output from these blocks will be embedded directly. All other output will be converted to JSON.
Multiline comments can be preceeded or followed by 'empty' syntax, which will be ignored.
* ```null```
* ```""```
* ```{}```
* ```[]```
.EXAMPLE
a.json template "{
procs : null/*{Get-Process | Select Name, ID}*/
}"
#>
[ValidatePattern('\.json$')]
param(
)

# We start off by declaring a number of regular expressions:
$startComment = '/\*' # * Start Comments ```\*```
$endComment = '\*/' # * End Comments ```/*```
$Whitespace = '[\s\n\r]{0,}'
# * IgnoredContext ```String.empty```, ```null```, blank strings and characters
$IgnoredContext = "(?<ignore>(?>$('null', '""', '\{\}', '\[\]' -join '|'))\s{0,}){0,1}"

$StartPattern = "(?<PSStart>${IgnoredContext}${startComment}\{$Whitespace)"
$EndPattern = "(?<PSEnd>$Whitespace\}${endComment}\s{0,}${IgnoredContext})"

$ForeachObject = {
$in = $_
if (($in -is [string]) -or
($in.GetType -and $in.GetType().IsPrimitive)) {
$in
} else {
ConvertTo-Json -Depth 100 -InputObject $in
}
}
}
90 changes: 0 additions & 90 deletions Transpilers/Templates/Json.Template.psx.ps1

This file was deleted.

0 comments on commit 5b5c10e

Please sign in to comment.