private/Normalize-WpcSequenceIndentation.ps1
|
function Normalize-WpcSequenceIndentation { param( [Parameter(Mandatory = $true)] [string] $YamlText, [Parameter(Mandatory = $true)] [string] $KeyName ) $lines = [System.Collections.Generic.List[string]]::new() foreach ($line in ($YamlText -split "`r?`n")) { $lines.Add($line) } for ($lineIndex = 0; $lineIndex -lt $lines.Count; $lineIndex++) { $match = [regex]::Match($lines[$lineIndex], "^([ ]*)$([regex]::Escape($KeyName)):\s*$") if (-not $match.Success) { continue } $baseIndent = $match.Groups[1].Value $expectedItemIndent = "${baseIndent} " for ($itemIndex = $lineIndex + 1; $itemIndex -lt $lines.Count; $itemIndex++) { $currentLine = $lines[$itemIndex] if ($currentLine -match "^\s*$") { continue } if ($currentLine.StartsWith("$expectedItemIndent- ")) { continue } if ($currentLine.StartsWith("$baseIndent- ")) { $lines[$itemIndex] = "$expectedItemIndent$($currentLine.Substring($baseIndent.Length))" continue } break } } return ($lines -join "`n") } |