Skip to content

Commit 1fba54a

Browse files
feat: `_includes/Help.ps1 ( Fixes #136 )
1 parent 396c47c commit 1fba54a

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

psturtle.com/_includes/Help.ps1

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<#
2+
.SYNOPSIS
3+
Includes Help Content
4+
.DESCRIPTION
5+
Includes the Help for a Command.
6+
#>
7+
param(
8+
# The command
9+
[PSObject]
10+
$Command,
11+
12+
# If set, will invoke examples.
13+
# This can be as dangerous as your examples.
14+
[switch]
15+
$InvokeExample
16+
)
17+
18+
# Try to get command help
19+
$CommandHelp = Get-Help -Name $Command -ErrorAction Ignore
20+
# If we cannot get help, return
21+
if (-not $CommandHelp) { return }
22+
23+
# If the help was a string
24+
if ($CommandHelp -is [string]) {
25+
# return preformatted text.
26+
return "<pre>$([Web.HttpUtility]::HtmlEncode($CommandHelp))</pre>"
27+
}
28+
29+
# Get the core parts of help we need.
30+
$synopsis = $CommandHelp.Synopsis
31+
$description = $CommandHelp.description.text -join [Environment]::NewLine
32+
$notes = $CommandHelp.alertSet.alert.text -join [Environment]::NewLine
33+
34+
# Display the command name, synopsis, and description
35+
"<h1>$([Web.HttpUtility]::HtmlEncode($CommandHelp.Name))</h1>"
36+
"<h2>$([Web.HttpUtility]::HtmlEncode($synopsis))</h2>"
37+
"<h3>$([Web.HttpUtility]::HtmlEncode($description))</h3>"
38+
39+
if ($notes) {
40+
# If there were notes, convert them from markdown
41+
(ConvertFrom-Markdown -InputObject $notes).Html
42+
}
43+
44+
#region Grid Styles
45+
"<style>"
46+
".example-grid { display: grid; place-items: center; gap: 2em; padding: 1em; margin: 2.5em}"
47+
"</style>"
48+
#endregion Grid Styles
49+
50+
# Create a grid for examples
51+
"<div class='example-grid'>"
52+
$exampleNumber = 0
53+
# Walk over each example
54+
foreach ($example in $CommandHelp.examples.example) {
55+
$exampleNumber++
56+
# Combine the code and remarks
57+
$exampleLines =
58+
@(
59+
$example.Code
60+
foreach ($remark in $example.Remarks.text) {
61+
if (-not $remark) { continue }
62+
$remark
63+
}
64+
) -join ([Environment]::NewLine) -split '(?>\r\n|\n)' # and split into lines
65+
66+
# Anything until the first non-comment line is a markdown predicate to the example
67+
$nonCommentLine = $false
68+
$markdownLines = @()
69+
70+
# Go thru each line in the example as part of a loop
71+
$codeBlock = @(foreach ($exampleLine in $exampleLines) {
72+
# Any comments until the first uncommentedLine are markdown
73+
if ($exampleLine -match '^\#' -and -not $nonCommentLine) {
74+
$markdownLines += $exampleLine -replace '^\#' -replace '^\s+'
75+
} else {
76+
$nonCommentLine = $true
77+
$exampleLine
78+
}
79+
}) -join [Environment]::NewLine
80+
81+
# Join all of our markdown lines together
82+
$Markdown = $markdownLines -join [Environment]::NewLine
83+
84+
# and start our example div
85+
"<div class='example'>"
86+
# If we had markdown, output it
87+
if ($markdown) {
88+
(ConvertFrom-Markdown -InputObject $Markdown).Html
89+
}
90+
# followed by our sample code
91+
"<div class='sampleCode'>"
92+
"<pre>"
93+
"<code class='language-powershell'>"
94+
[Web.HttpUtility]::HtmlEncode($codeBlock)
95+
"</code>"
96+
"</pre>"
97+
"</div>"
98+
"</div>"
99+
100+
# If we do not want to invoke examples, we can continue to the next example.
101+
if (-not $InvokeExample) { continue }
102+
103+
# Otherwise, try to make our example a script block
104+
$exampleCode =
105+
try {
106+
[scriptblock]::Create($codeBlock)
107+
} catch {
108+
Write-Warning "Unable to convert $($example.code) to a script"
109+
continue
110+
}
111+
112+
# then run it and capture the output
113+
$exampleOutputs = @(. $exampleCode)
114+
115+
# Keep track of our example output count
116+
$exampleOutputNumber = 0
117+
# and start walking thru the list
118+
foreach ($exampleOutput in $exampleOutputs) {
119+
$exampleOutputNumber++
120+
# Each output goes in a div
121+
"<div class='example-output'>"
122+
# if the output was a file
123+
if ($exampleOutput -is [IO.FileInfo]) {
124+
# and that file is SVG
125+
if ($exampleOutput.Extension -eq '.svg') {
126+
# include it inline.
127+
Get-Content $exampleOutput.FullName -Raw
128+
}
129+
} else {
130+
# If the output was a turtle object
131+
if ($exampleOutput.pstypenames -contains 'Turtle') {
132+
# set it's ID
133+
$exampleOutput.ID = "$($Command)-Example-$exampleCounter"
134+
if ($exampleOutputs.Length -gt 1) {
135+
# If we have more than out output,
136+
# attach our example counter
137+
$exampleOutput.ID += "-$($exampleOutputNumber)"
138+
}
139+
}
140+
# Include our example output inline
141+
if ($exampleOutput.ToHtml.Invoke) {
142+
$exampleOutput.ToHtml()
143+
} else {
144+
"$exampleOutput"
145+
}
146+
}
147+
"</div>"
148+
}
149+
}
150+
"</div>"

0 commit comments

Comments
 (0)