PowerShell/scripts/convert-ps2md.ps1

144 lines
3.6 KiB
PowerShell
Raw Normal View History

2023-10-31 12:12:58 +01:00
<#
2021-08-28 12:37:13 +02:00
.SYNOPSIS
2022-12-04 10:40:18 +01:00
Converts a PowerShell script to Markdown
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script converts the comment-based help of a PowerShell script to Markdown.
2021-10-06 10:58:19 +02:00
.PARAMETER filename
Specifies the path to the PowerShell script
2021-08-28 12:37:13 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./convert-ps2md.ps1 myscript.ps1
2021-08-28 12:37:13 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-08-28 11:44:08 +02:00
#>
2021-08-30 15:48:23 +02:00
param([string]$filename = "")
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
function EncodePartOfHtml { param([string]$Value)
2021-08-28 11:44:08 +02:00
($Value -replace '<', '&lt;') -replace '>', '&gt;'
}
2021-08-28 17:48:14 +02:00
function GetCode { param($Example)
2021-08-28 11:44:08 +02:00
$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++) {
2021-10-17 11:56:55 +02:00
if ($codeAndRemarks[$i] -eq 'DESCRIPTION' -and $codeAndRemarks[$i + 1] -eq '-----------') { break }
if ($codeAndRemarks[$i] -eq '' -and $codeAndRemarks[$i + 1] -eq '') { continue }
if (1 -le $i -and $i -le 2) { continue }
$codeAndRemarks[$i] = ($codeAndRemarks[$i] | Out-String) -replace "PS>","PS> "
2021-08-28 11:44:08 +02:00
$code.Add($codeAndRemarks[$i])
}
$code -join "`r`n"
}
2021-08-28 17:48:14 +02:00
function GetRemark { param($Example)
2021-08-28 11:44:08 +02:00
$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 {
2023-07-29 09:55:25 +02:00
if ($filename -eq "") { $filename = Read-Host "Enter path to PowerShell script" }
2021-08-28 14:20:05 +02:00
$ScriptName = (Get-Item "$filename").Name
2021-08-30 15:48:23 +02:00
$full = Get-Help $filename -Full
2021-08-28 17:48:14 +02:00
"Script: *$($ScriptName)*"
"========================"
2021-08-28 12:58:48 +02:00
$Description = ($full.description | Out-String).Trim()
if ($Description -ne "") {
2021-08-28 17:48:14 +02:00
""
2021-08-28 12:58:48 +02:00
"$Description"
2022-11-17 20:02:26 +01:00
} else {
""
"$($full.Synopsis)"
2021-08-28 12:58:48 +02:00
}
2021-10-06 11:17:09 +02:00
""
2023-07-29 10:04:38 +02:00
"Parameters"
"----------"
2021-10-06 11:17:09 +02:00
"``````powershell"
2021-10-06 11:52:39 +02:00
$Syntax = (($full.syntax | Out-String) -replace "`r`n", "`r`n").Trim()
2023-10-31 11:32:46 +01:00
$Syntax = (($Syntax | Out-String) -replace "/home/mf/Repos/PowerShell/scripts/", "PS> ./")
2021-08-28 12:58:48 +02:00
if ($Syntax -ne "") {
"$Syntax"
}
2021-08-28 11:44:08 +02:00
2021-08-28 14:20:05 +02:00
foreach($parameter in $full.parameters.parameter) {
"$(((($parameter | Out-String).Trim() -split "`r`n")[-5..-1] | % { $_.Trim() }) -join "`r`n")"
2021-10-06 11:52:39 +02:00
""
2021-08-28 14:20:05 +02:00
}
2021-08-29 18:21:53 +02:00
"[<CommonParameters>]"
2021-10-06 13:16:21 +02:00
" This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, "
2021-08-29 18:12:58 +02:00
" WarningVariable, OutBuffer, PipelineVariable, and OutVariable."
"``````"
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
foreach($input in $full.inputTypes.inputType) {
2021-08-28 14:20:05 +02:00
""
2023-07-29 10:04:38 +02:00
"Inputs"
"------"
2021-08-28 17:48:14 +02:00
"$($input.type.name)"
2021-08-28 14:20:05 +02:00
}
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
foreach($output in $full.outputTypes.outputType) {
2021-08-28 14:20:05 +02:00
""
2023-07-29 10:04:38 +02:00
"Outputs"
"-------"
2021-08-28 17:48:14 +02:00
"$($output.type.name)"
2021-08-28 14:20:05 +02:00
}
2021-08-28 11:44:08 +02:00
2021-08-28 17:48:14 +02:00
foreach($example in $full.examples.example) {
""
2023-07-29 10:04:38 +02:00
"Example"
"-------"
2021-08-28 17:48:14 +02:00
"``````powershell"
"$(GetCode $example)"
"``````"
}
2021-08-28 11:44:08 +02:00
2021-08-28 12:58:48 +02:00
$Notes = ($full.alertSet.alert | Out-String).Trim()
if ($Notes -ne "") {
""
2023-07-29 10:04:38 +02:00
"Notes"
"-----"
2021-08-28 12:58:48 +02:00
"$Notes"
}
$Links = ($full.relatedlinks | Out-String).Trim()
if ($Links -ne "") {
""
2023-07-29 10:04:38 +02:00
"Related Links"
"-------------"
2021-08-28 12:58:48 +02:00
"$Links"
}
2021-08-29 12:44:05 +02:00
2022-11-17 20:02:26 +01:00
""
2023-07-29 10:04:38 +02:00
"Script Content"
"--------------"
2022-11-17 20:05:34 +01:00
"``````powershell"
2022-11-17 20:02:26 +01:00
$Lines = Get-Content -path "$filename"
foreach($Line in $Lines) {
"$Line"
}
2022-11-17 20:05:34 +01:00
"``````"
2021-08-29 12:44:05 +02:00
""
2023-07-29 09:55:25 +02:00
$now = [datetime]::Now
"*(generated by convert-ps2md.ps1 using the comment-based help of $ScriptName as of $now)*"
2021-08-28 14:20:05 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-08-28 14:20:05 +02:00
exit 1
2021-08-29 12:44:05 +02:00
}