PowerShell/Scripts/convert-ps2md.ps1

138 lines
2.9 KiB
PowerShell
Raw Normal View History

2021-08-28 11:44:08 +02:00
<#
2021-08-28 12:37:13 +02:00
.SYNOPSIS
convert-ps2md.ps1 [<script>]
.DESCRIPTION
Converts the comment-based help of a PowerShell script to Markdown
.EXAMPLE
PS> .\convert-ps2md.ps1 myscript.ps1
.NOTES
Author: Markus Fleschutz
License: CC0
.LINK
https://github.com/fleschutz/PowerShell
2021-08-28 11:44:08 +02:00
#>
2021-08-28 14:20:05 +02:00
param([string]$script = "")
2021-08-28 11:44:08 +02:00
function EncodePartOfHtml {
param (
[string]
$Value
)
($Value -replace '<', '&lt;') -replace '>', '&gt;'
}
function GetCode {
param (
$Example
)
$codeAndRemarks = (($Example | Out-String) -replace ($Example.title), '').Trim() -split "`r`n"
$code = New-Object "System.Collections.Generic.List[string]"
for ($i = 0; $i -lt $codeAndRemarks.Length; $i++) {
if ($codeAndRemarks[$i] -eq 'DESCRIPTION' -and $codeAndRemarks[$i + 1] -eq '-----------') {
break
}
if (1 -le $i -and $i -le 2) {
continue
}
$code.Add($codeAndRemarks[$i])
}
$code -join "`r`n"
}
function GetRemark {
param (
$Example
)
$codeAndRemarks = (($Example | Out-String) -replace ($Example.title), '').Trim() -split "`r`n"
$isSkipped = $false
$remark = New-Object "System.Collections.Generic.List[string]"
for ($i = 0; $i -lt $codeAndRemarks.Length; $i++) {
if (!$isSkipped -and $codeAndRemarks[$i - 2] -ne 'DESCRIPTION' -and $codeAndRemarks[$i - 1] -ne '-----------') {
continue
}
$isSkipped = $true
$remark.Add($codeAndRemarks[$i])
}
$remark -join "`r`n"
}
try {
2021-08-28 14:20:05 +02:00
if ($script -eq "") { $script = read-host "Enter path to PowerShell script" }
2021-08-28 12:58:48 +02:00
$full = Get-Help $script -Full
2021-08-28 14:20:05 +02:00
"# $($full.Synopsis)"
2021-08-28 12:58:48 +02:00
$Description = ($full.description | Out-String).Trim()
if ($Description -ne "") {
"$Description"
}
$Syntax = (($full.syntax | Out-String) -replace "`r`n", "`r`n`r`n").Trim()
if ($Syntax -ne "") {
""
"## Syntax"
"``````powershell"
"$Syntax"
"``````"
}
2021-08-28 11:44:08 +02:00
2021-08-28 14:20:05 +02:00
foreach($parameter in $full.parameters.parameter) {
""
"## Parameter -$($parameter.name) &lt;$($parameter.type.name)&gt;"
"$(($parameter.description | Out-String).Trim())"
"``````"
"$(((($parameter | Out-String).Trim() -split "`r`n")[-5..-1] | % { $_.Trim() }) -join "`r`n")"
"``````"
}
2021-08-28 11:44:08 +02:00
2021-08-28 14:20:05 +02:00
foreach($input in $full.inputTypes) {
""
"## Inputs"
"$($input.inputType.type.name)"
}
2021-08-28 11:44:08 +02:00
2021-08-28 14:20:05 +02:00
foreach($output in $full.outputTypes) {
""
"## Outputs"
"$($output.outputType.type.name)"
}
@"
2021-08-28 11:44:08 +02:00
2021-08-28 12:37:13 +02:00
## Examples
2021-08-28 11:44:08 +02:00
"@ + $(foreach ($example in $full.examples.example) {
@"
### $(($example.title -replace '-*', '').Trim())
``````powershell
$(GetCode $example)
``````
$(GetRemark $example)
"@
}) + @"
"@
2021-08-28 12:58:48 +02:00
$Notes = ($full.alertSet.alert | Out-String).Trim()
if ($Notes -ne "") {
""
"## Notes"
"$Notes"
}
$Links = ($full.relatedlinks | Out-String).Trim()
if ($Links -ne "") {
""
"## Related Links"
"$Links"
}
2021-08-28 14:20:05 +02:00
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}