Improve convert-ps2md.ps1

This commit is contained in:
Markus Fleschutz 2021-08-28 12:37:13 +02:00
parent d02909fbb5
commit 48ea2d6fe6

View File

@ -1,59 +1,18 @@
#
# File: Get-HelpByMarkdown.ps1
#
# Author: Akira Sugiura (urasandesu@gmail.com)
#
#
# Copyright (c) 2014 Akira Sugiura
#
# This software is MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
<# <#
.SYNOPSIS .SYNOPSIS
Gets the comment-based help and converts to GitHub Flavored Markdown. convert-ps2md.ps1 [<script>]
.DESCRIPTION
.PARAMETER Name Converts the comment-based help of a PowerShell script to Markdown
A command name to get comment-based help.
.EXAMPLE .EXAMPLE
& .\Get-HelpByMarkdown.ps1 Select-Object > .\Select-Object.md PS> .\convert-ps2md.ps1 myscript.ps1
.NOTES
DESCRIPTION Author: Markus Fleschutz
----------- License: CC0
This example gets comment-based help of `Select-Object` command, and converts GitHub Flavored Markdown format, then saves it to `Select-Object.md` in current directory. .LINK
https://github.com/fleschutz/PowerShell
.INPUTS
System.String
.OUTPUTS
System.String
#> #>
[CmdletBinding()] param([string]$script)
param (
[Parameter(Mandatory = $True)]
$Name
)
function EncodePartOfHtml { function EncodePartOfHtml {
param ( param (
@ -104,30 +63,21 @@ function GetRemark {
} }
try { try {
if ($Host.UI.RawUI) { $full = Get-Help $script -Full
$rawUI = $Host.UI.RawUI
$oldSize = $rawUI.BufferSize
$typeName = $oldSize.GetType().FullName
$newSize = New-Object $typeName (500, $oldSize.Height)
$rawUI.BufferSize = $newSize
}
$full = Get-Help $Name -Full
@" @"
# $($full.Name) # $($full.Name)
## SYNOPSIS
$($full.Synopsis) $($full.Synopsis)
## SYNTAX ## Description
$(($full.description | Out-String).Trim())
## Syntax
``````powershell ``````powershell
$((($full.syntax | Out-String) -replace "`r`n", "`r`n`r`n").Trim()) $((($full.syntax | Out-String) -replace "`r`n", "`r`n`r`n").Trim())
`````` ``````
## DESCRIPTION ## Parameters
$(($full.description | Out-String).Trim())
## PARAMETERS
"@ + $(foreach ($parameter in $full.parameters.parameter) { "@ + $(foreach ($parameter in $full.parameters.parameter) {
@" @"
@ -140,16 +90,12 @@ $(((($parameter | Out-String).Trim() -split "`r`n")[-5..-1] | % { $_.Trim() }) -
"@ "@
}) + @" }) + @"
## INPUTS ## Inputs
$($full.inputTypes.inputType.type.name) $($full.inputTypes.inputType.type.name)
## OUTPUTS ## Outputs
$($full.returnValues.returnValue[0].type.name)
## NOTES ## Examples
$(($full.alertSet.alert | Out-String).Trim())
## EXAMPLES
"@ + $(foreach ($example in $full.examples.example) { "@ + $(foreach ($example in $full.examples.example) {
@" @"
@ -159,14 +105,16 @@ $(GetCode $example)
`````` ``````
$(GetRemark $example) $(GetRemark $example)
## Notes
$(($full.alertSet.alert | Out-String).Trim())
## Related Links
$(($full.relatedlinks | Out-String).Trim())
"@ "@
}) + @" }) + @"
"@ "@
} finally { } finally {
if ($Host.UI.RawUI) {
$rawUI = $Host.UI.RawUI
$rawUI.BufferSize = $oldSize
}
} }