[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$Output, [char]$Delimiter = ',' ) $ErrorActionPreference = 'Stop' $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop $rows = @(Import-Csv -LiteralPath $resolved -Delimiter $Delimiter) if ($rows.Count -eq 0) { throw 'CSV не содержит строк' } $headers = @($rows[0].PSObject.Properties.Name) function Escape-Cell([object]$Value) { if ($null -eq $Value) { return '' } $text = [string]$Value $text = $text.Replace('|', '\|') $text = $text.Replace("`r", ' ') $text = $text.Replace("`n", '
') return $text.Trim() } $headerLine = '| ' + (($headers | ForEach-Object { Escape-Cell $_ }) -join ' | ') + ' |' $alignCells = foreach ($header in $headers) { $values = @($rows | ForEach-Object { $_.$header } | Where-Object { $null -ne $_ -and ([string]$_).Trim() -ne '' }) $numeric = $values.Count -gt 0 foreach ($value in $values) { if ([string]$value -notmatch '^-?\d+([.,]\d+)?$') { $numeric = $false break } } if ($numeric) { '---:' } else { '---' } } $alignLine = '| ' + ($alignCells -join ' | ') + ' |' $lines = New-Object System.Collections.Generic.List[string] $lines.Add($headerLine) $lines.Add($alignLine) foreach ($row in $rows) { $cells = foreach ($header in $headers) { Escape-Cell $row.$header } $lines.Add('| ' + ($cells -join ' | ') + ' |') } $markdown = ($lines -join [Environment]::NewLine) + [Environment]::NewLine if ($Output) { $target = [IO.Path]::GetFullPath($Output) if ($target -eq $resolved.Path) { throw 'Выходной файл совпадает с входным' } [IO.File]::WriteAllText($target, $markdown, (New-Object System.Text.UTF8Encoding($false))) Write-Host "Таблица сохранена: $target" } else { Write-Output $markdown }