forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-ps2md.ps1
executable file
·143 lines (124 loc) · 3.56 KB
/
convert-ps2md.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<#
.SYNOPSIS
Converts a PowerShell script to Markdown
.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.ps1 myscript.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$filename = "")
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 ($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 {
if ($filename -eq "") { $filename = Read-Host "Enter path to PowerShell script" }
$ScriptName = (Get-Item "$filename").Name
$full = Get-Help $filename -Full
"The *$($ScriptName)* Script"
"==========================="
$Description = ($full.description | Out-String).Trim()
if ($Description -ne "") {
""
"$Description"
} else {
""
"$($full.Synopsis)"
}
""
"Parameters"
"----------"
"``````powershell"
$Syntax = (($full.syntax | Out-String) -replace "`r`n", "`r`n").Trim()
$Syntax = (($Syntax | Out-String) -replace "/home/mf/Repos/PowerShell/scripts/", "PS> ./")
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) {
""
"Inputs"
"------"
"$($input.type.name)"
}
foreach($output in $full.outputTypes.outputType) {
""
"Outputs"
"-------"
"$($output.type.name)"
}
foreach($example in $full.examples.example) {
""
"Example"
"-------"
"``````powershell"
"$(GetCode $example)"
"``````"
}
$Notes = ($full.alertSet.alert | Out-String).Trim()
if ($Notes -ne "") {
""
"Notes"
"-----"
"$Notes"
}
$Links = ($full.relatedlinks | Out-String).Trim()
if ($Links -ne "") {
""
"Related Links"
"-------------"
"$Links"
}
""
"Script Content"
"--------------"
"``````powershell"
$Lines = Get-Content -path "$filename"
foreach($Line in $Lines) {
"$Line"
}
"``````"
""
$now = [datetime]::Now
"*(generated by convert-ps2md.ps1 as of $now)*"
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}