mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-18 18:51:05 +01:00
121 lines
2.4 KiB
PowerShell
121 lines
2.4 KiB
PowerShell
<#
|
|
.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
|
|
#>
|
|
|
|
param([string]$script)
|
|
|
|
function EncodePartOfHtml {
|
|
param (
|
|
[string]
|
|
$Value
|
|
)
|
|
|
|
($Value -replace '<', '<') -replace '>', '>'
|
|
}
|
|
|
|
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 {
|
|
$full = Get-Help $script -Full
|
|
|
|
@"
|
|
# $($full.Name)
|
|
$($full.Synopsis)
|
|
|
|
## Description
|
|
$(($full.description | Out-String).Trim())
|
|
|
|
## Syntax
|
|
``````powershell
|
|
$((($full.syntax | Out-String) -replace "`r`n", "`r`n`r`n").Trim())
|
|
``````
|
|
|
|
## Parameters
|
|
"@ + $(foreach ($parameter in $full.parameters.parameter) {
|
|
@"
|
|
|
|
### -$($parameter.name) <$($parameter.type.name)>
|
|
$(($parameter.description | Out-String).Trim())
|
|
``````
|
|
$(((($parameter | Out-String).Trim() -split "`r`n")[-5..-1] | % { $_.Trim() }) -join "`r`n")
|
|
``````
|
|
|
|
"@
|
|
}) + @"
|
|
|
|
## Inputs
|
|
$($full.inputTypes.inputType.type.name)
|
|
|
|
## Outputs
|
|
|
|
## Examples
|
|
"@ + $(foreach ($example in $full.examples.example) {
|
|
@"
|
|
|
|
### $(($example.title -replace '-*', '').Trim())
|
|
``````powershell
|
|
$(GetCode $example)
|
|
``````
|
|
$(GetRemark $example)
|
|
|
|
## Notes
|
|
$(($full.alertSet.alert | Out-String).Trim())
|
|
|
|
## Related Links
|
|
$(($full.relatedlinks | Out-String).Trim())
|
|
|
|
"@
|
|
}) + @"
|
|
|
|
"@
|
|
|
|
} finally {
|
|
}
|