PowerShell/Docs/convert-ps2md.md

189 lines
4.6 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*convert-ps2md.ps1*
================
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
This PowerShell script converts the comment-based help of a PowerShell script to Markdown.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./convert-ps2md.ps1 [[-filename] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-filename <String>
Specifies the path to the PowerShell script
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
PS> ./convert-ps2md myscript.ps1
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
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
<#
.SYNOPSIS
2022-12-04 10:40:18 +01:00
Converts a PowerShell script to Markdown
2022-11-17 20:02:26 +01:00
.DESCRIPTION
This PowerShell script converts the comment-based help of a PowerShell script to Markdown.
.PARAMETER filename
Specifies the path to the PowerShell script
.EXAMPLE
PS> ./convert-ps2md myscript.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$filename = "")
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 ($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> "
$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 {
2023-07-29 09:55:25 +02:00
if ($filename -eq "") { $filename = Read-Host "Enter path to PowerShell script" }
2022-11-17 20:02:26 +01:00
$ScriptName = (Get-Item "$filename").Name
$full = Get-Help $filename -Full
2023-07-29 10:34:04 +02:00
"*$($ScriptName)*"
"================"
2022-11-17 20:02:26 +01:00
$Description = ($full.description | Out-String).Trim()
if ($Description -ne "") {
""
"$Description"
} else {
""
"$($full.Synopsis)"
}
""
2023-07-29 10:04:38 +02:00
"Parameters"
"----------"
2022-11-17 20:02:26 +01:00
"``````powershell"
$Syntax = (($full.syntax | Out-String) -replace "`r`n", "`r`n").Trim()
2023-07-29 10:15:44 +02:00
$Syntax = (($Syntax | Out-String) -replace "/home/mf/Repos/PowerShell/Scripts/", "PS> ./")
2022-11-17 20:02:26 +01:00
if ($Syntax -ne "") {
"$Syntax"
}
foreach($parameter in $full.parameters.parameter) {
"$(((($parameter | Out-String).Trim() -split "`r`n")[-5..-1] | % { $_.Trim() }) -join "`r`n")"
""
}
"[<CommonParameters>]"
" This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, "
" WarningVariable, OutBuffer, PipelineVariable, and OutVariable."
"``````"
foreach($input in $full.inputTypes.inputType) {
""
2023-07-29 10:04:38 +02:00
"Inputs"
"------"
2022-11-17 20:02:26 +01:00
"$($input.type.name)"
}
foreach($output in $full.outputTypes.outputType) {
""
2023-07-29 10:04:38 +02:00
"Outputs"
"-------"
2022-11-17 20:02:26 +01:00
"$($output.type.name)"
}
foreach($example in $full.examples.example) {
""
2023-07-29 10:04:38 +02:00
"Example"
"-------"
2022-11-17 20:02:26 +01:00
"``````powershell"
"$(GetCode $example)"
"``````"
}
$Notes = ($full.alertSet.alert | Out-String).Trim()
if ($Notes -ne "") {
""
2023-07-29 10:04:38 +02:00
"Notes"
"-----"
2022-11-17 20:02:26 +01:00
"$Notes"
}
$Links = ($full.relatedlinks | Out-String).Trim()
if ($Links -ne "") {
""
2023-07-29 10:04:38 +02:00
"Related Links"
"-------------"
2022-11-17 20:02:26 +01:00
"$Links"
}
""
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
"``````"
2022-11-17 20:02:26 +01: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)*"
2022-11-17 20:02:26 +01:00
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-07-29 10:34:04 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of convert-ps2md.ps1 as of 07/29/2023 10:33:44)*