Skip to content

Commit 4401ce0

Browse files
feat: OpenPackage.View.Help.md ( Fixes #204 )
1 parent 6584f67 commit 4401ce0

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Types/OpenPackage.View/Help.md.ps1

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<#
2+
.SYNOPSIS
3+
Views Help as Markdown
4+
.DESCRIPTION
5+
Views PowerShell Command as markdown
6+
.INPUTS
7+
Management.Automation.CommandInfo
8+
#>
9+
param()
10+
11+
$allInputAndArgs = @($input) + $args
12+
13+
foreach ($in in $allInputAndArgs) {
14+
if ($in -isnot [Management.Automation.CommandInfo]) {
15+
continue
16+
}
17+
18+
$help = $null
19+
if ($in -is [Management.Automation.ExternalScriptInfo]) {
20+
$help = Get-Help $in.Source
21+
} elseif (
22+
$in -is [Management.Automation.FunctionInfo] -or
23+
$in -is [Management.Automation.AliasInfo] -or
24+
$in -is [Management.Automation.CmdletInfo]
25+
) {
26+
$help = Get-Help $in.Name
27+
}
28+
29+
30+
if (-not $help) {
31+
Write-Warning "Applications may not have help"
32+
continue
33+
}
34+
35+
# If the help is a string,
36+
if ($help -is [string]) {
37+
# make it preformatted text
38+
"~~~"
39+
"$export"
40+
"~~~"
41+
} else {
42+
# Otherwise, add list the export
43+
"### $($export)"
44+
45+
# And make it's synopsis a header
46+
"#### $($help.SYNOPSIS)"
47+
48+
# put the description below that
49+
"$($help.Description.text -join [Environment]::NewLine)"
50+
51+
# Show our examples
52+
"##### Examples"
53+
54+
$exampleNumber = 0
55+
foreach ($example in $help.examples.example) {
56+
$markdownLines = @()
57+
$exampleNumber++
58+
$nonCommentLine = $false
59+
"###### Example $exampleNumber"
60+
61+
# Combine the code and remarks
62+
$exampleLines =
63+
@(
64+
$example.Code
65+
foreach ($remark in $example.Remarks.text) {
66+
if (-not $remark) { continue }
67+
$remark
68+
}
69+
) -join ([Environment]::NewLine) -split '(?>\r\n|\n)' # and split into lines
70+
71+
# Go thru each line in the example as part of a loop
72+
$codeBlock = @(foreach ($exampleLine in $exampleLines) {
73+
# Any comments until the first uncommentedLine are markdown
74+
if ($exampleLine -match '^\#' -and -not $nonCommentLine) {
75+
$markdownLines += $exampleLine -replace '^\#\s{0,1}'
76+
} else {
77+
$nonCommentLine = $true
78+
$exampleLine
79+
}
80+
}) -join [Environment]::NewLine
81+
82+
$markdownLines
83+
"~~~PowerShell"
84+
$CodeBlock
85+
"~~~"
86+
}
87+
88+
$relatedUris = foreach ($link in $help.relatedLinks.navigationLink) {
89+
if ($link.uri) {
90+
$link.uri
91+
}
92+
}
93+
if ($relatedUris) {
94+
"#### Links"
95+
foreach ($related in $relatedUris) {
96+
"* [$related]($related)"
97+
}
98+
}
99+
100+
# Make a table of parameters
101+
if ($help.parameters.parameter) {
102+
"##### Parameters"
103+
104+
""
105+
106+
"|Name|Type|Description|"
107+
"|-|-|-|"
108+
foreach ($parameter in $help.Parameters.Parameter) {
109+
"|$($parameter.Name)|$($parameter.type.name)|$(
110+
$parameter.description.text -replace '(?>\r\n|\n)', '<br/>'
111+
)|"
112+
}
113+
114+
""
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)