Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active January 27, 2022 17:03
Show Gist options
  • Select an option

  • Save ninmonkey/dda8badfb6a7230d198516791c73118f to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/dda8badfb6a7230d198516791c73118f to your computer and use it in GitHub Desktop.
Show-XmlPreview
function ConvertTo-RegexLiteral {
<#
.synopsis
sugar to quickly escape values to thier regex-literal
.description
.example
$PS> Relit 'something'
.example
$pattern = re 'something' -AsRipGrep
rg @('-i', $Pattern)
.notes
.outputs
a literl escaped for regex
#>
[alias('ReLit', 'ReLiteral')]
[CmdletBinding(PositionalBinding = $false)]
param(
# Text to convert to a literal
[Alias('InputObject')]
[Parameter( Mandatory, Position = 0, ValueFromPipeline)]
[string[]]$Text
)
process {
$Text | ForEach-Object {
[regex]::Escape($_)
}
}
}
function Show-XmlPreview {
<#
.synopsis
simple quick way to preview xml docs, even memory only
.description
uses bat for colorizing XML
.example
PS> $doc = [xml]'<?xml version="1.0" encoding="utf-8"?> <AssetFile> <Asset> <Role>None</Role> <AssetType>Computing</AssetType> </Asset> </AssetFile>'
PS> $doc | ShowXml
#>
[Alias('ShowXml')]
param(
# Either an [XmlDocument] or filepath to one
[Parameter(Mandatory, ValueFromPipeline, Position = 0)]
[object]$InputObject
)
process {
if (Test-Path $InputObject) {
Write-Debug 'showXml [from file]'
$DisplayPath = $InputObject
$DisplayPath = $DisplayPath -replace (Relit (Get-Item Temp:\)), 'Temp:\'
$DisplayPath = $DisplayPath -replace (relit $Env:LocalAppData), '$Env:LocalAppData'
Get-Content $InputObject
| bat -l xml --style 'header,grid' --file-name ($DisplayPath)
Write-Debug "Should be: $($InputObject) "
return
}
if ($InputObject -is 'System.Xml.XmlDocument') {
$doc = $InputObject
Write-Debug 'showXml [in memory]'
$TempDest = Join-Path (Get-Item temp:) 'render.xml'
$doc.save( $tempDest ) #| Out-Null
showXml (Get-Item $TempDest)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment