-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathWrite-WalkthruHTML.ps1
182 lines (153 loc) · 7.33 KB
/
Write-WalkthruHTML.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function Write-WalkthruHTML
{
<#
.Synopsis
Writes a walkthru HTML file
.Description
Writes a section of HTML to walk thru a set of code.
.Example
Write-WalkthruHTML -Text @"
#a simple demo
Get-Help about_walkthruFiles
"@
.Link
Get-Walkthru
Write-ScriptHTML
#>
[CmdletBinding(DefaultParameterSetName='Text')]
[OutputType([string])]
param(
# The text used to generate walkthrus
[Parameter(Position=0,Mandatory=$true,
ParameterSetName="Text",
ValueFromPipeline=$true)]
[ScriptBlock]$ScriptBlock,
# A walkthru object, containing a source file and a property named
# walkthru with several walkthru steps
[Parameter(Position=0,Mandatory=$true,
ParameterSetName="Walkthru",
ValueFromPipeline=$true)]
[PSObject]$WalkThru,
# with a different step on each layer
[Parameter(Position=1)]
[Switch]$StepByStep,
# If set, will run each demo step
[Parameter(Position=2)]
[Switch]$RunDemo,
# If set, output will be treated as HTML. Otherwise, output will be piped to Out-String and embedded in <pre> tags.
[Parameter(Position=3)]
[Switch]$OutputAsHtml,
# If set, will start with walkthru with a <h3></h3> tag, or include the walkthru name on each step
[Parameter(Position=4)]
[string]$WalkthruName,
# If set, will embed the explanation as text, instead of converting it to markdown.
[Parameter(Position=5)]
[switch]$DirectlyEmbedExplanation,
# If provided, will only include certain steps
[Parameter(Position=6)]
[Uint32[]]$OnlyStep
)
process {
if ($psCmdlet.ParameterSetName -eq 'Text') {
Write-WalkthruHTML -Walkthru (Get-Walkthru -Text "$ScriptBlock") -StepByStep:$stepByStep
} elseif ($psCmdlet.ParameterSetName -eq 'Walkthru') {
$NewRegionParameters = @{
Layer = @{}
Order = @()
HorizontalRuleUnderTitle = $true
}
$walkThruHTML = New-Object Text.StringBuilder
$count = 1
$total = @($walkThru).Count
foreach ($step in $walkThru) {
# if it's provided, skip stuff that's not in OnlyStep
if ($OnlySteps) {
if ($OnlySteps -notcontains $count){
continue
}
}
# If we're going step by step, then we need to reset the string builder each time
if ($stepByStep) {
$walkThruHTML = New-Object Text.StringBuilder
}
if ($DirectlyEmbedExplanation -or $step.Explanation -like "*<*") {
$null = $walkThruHtml.Append("
<div class='ModuleWalkthruExplanation'>
$($step.Explanation.Replace([Environment]::newline, '<BR/>'))
</div>")
} else {
$null = $walkThruHtml.Append("
<div class='ModuleWalkthruExplanation'>
$(ConvertFrom-Markdown -Markdown "$($step.Explanation) ")
</div>")
}
if ($step.VideoFile -and $step.VideoFile -like "http*") {
if ($step.VideoFile -like "http://www.youtube.com/watch?v=*") {
$uri = $step.VideoFile -as [uri]
$type, $youTubeId = $uri.Query -split '='
$type = $type.Trim("?")
$null =
$walkThruHtml.Append(@"
<br/>
<embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/${type}/${youTubeId}?hl=en&fs=1&modestbranding=true" allowscriptaccess="always" allowfullscreen="true">
"@)
} elseif ($step.VideoFile -like "http://player.vimeo.com/video/*") {
$vimeoId = ([uri]$step.VideoFile).Segments[-1]
$null =
$walkThruHtml.Append(@"
<br/>
<iframe src="http://player.vimeo.com/video/${vimeoId}?title=0&byline=0&portrait=0" width="400" height="245" frameborder="0">
</iframe><p><a href="http://vimeo.com/{$vimeoId}">$($walkThru.Explanation)</a></p>
"@)
} else {
$null =
$walkThruHtml.Append("
<br/>
<a class='ModuleWalkthruVideoLink' href='$($step.VideoFile)'>Watch Video</a>")
}
}
$null = $walkThruHtml.Append("<br/></p>")
if (("$($step.Script)".Trim())-and ("$($step.Script)".Trim() -ne '$null')) {
if ($step.Script -is [ScriptBlock]) {
$scriptHtml = Write-ScriptHTML -Text $step.Script
} else {
$scriptHtml = Write-ScriptHTML -Script $step.Script
}
$null = $walkThruHtml.Append(@"
<p class='ModuleWalkthruStep'>
$scriptHtml
</p>
"@)
}
if ($RunDemo) {
$outText = . $step.Script
if (-not $OutputAsHtml) {
$null = $walkThruHtml.Append("<pre class='ModuleWalkthruOutput' foreground='white' background='#012456'>$([Security.SecurityElement]::Escape(($outText | Out-String)))</pre>")
} else {
if ($outText -is [Hashtable]) {
$null = $walkThruHtml.Append("$(Write-PowerShellHashtable -inputObject $OutText) ")
} elseif ($outText -is [ScriptBlock]) {
$null = $walkThruHtml.Append("$(Write-ScriptHtml -Text $OutText) ")
} else {
$null = $walkThruHtml.Append("$OutText")
}
}
}
if ($stepByStep) {
$NewRegionParameters.Layer."$Count of $Total" = "<div style='margin-left:15px;margin-top:15px;'>$walkThruHTML</div>"
$NewRegionParameters.Order+= "$Count of $Total"
}
$Count++
}
if (-not $stepByStep) {
"$walkThruHTML"
} else {
if ($WalkthruName) {
New-Region @newRegionParameters -AsFeaturette -ShowLayerTitle -LayerId "Walkthru_$WalkthruName"
} else {
New-Region @newRegionParameters -AsFeaturette -ShowLayerTitle -LayerUrl "RandomWalkthru_$(Get-random)"
}
}
}
}
}