-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path100.html
More file actions
385 lines (330 loc) · 12.6 KB
/
100.html
File metadata and controls
385 lines (330 loc) · 12.6 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="PoshCode - Community resources for PowerShell coders">
<meta name="author" content="Jeff Hillman">
<title>Highlight-Syntax 1.0 - PoshCode</title>
<link rel="stylesheet" href="https://poshcode.org/css/superhero.min.css">
<link rel="stylesheet" href="https://poshcode.org/css/main.css">
<link rel="stylesheet" href="https://poshcode.org/css/highlight/arta.css">
<style>
body {
padding-top: 50px;
padding-bottom: 20px;
}
</style>
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm fixed-top navbar-dark bg-dark">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="https://PoshCode.org/">PoshCode</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div id="navbarResponsive" class="collapse navbar-collapse navbar-responsive-collapse navbar-right">
<ul class="nav navbar-nav nav-tabs ml-auto" id="tabs">
<li class="nav-item"><a class="nav-link" href="https://poshcode.org/">Join Us!</a></li>
<li class="nav-item"><a class="nav-link active show" href="https://poshcode.org/scripts" data-toggle="tab">Scripts</a></li>
<li class="nav-item"><a class="nav-link" href="https://poshcode.org/video">Videos</a></li>
</ul>
</div><!--/.navbar-collapse -->
</div>
</nav>
</header>
<div class="container">
<div class="blog-header">
<h1 class="blog-title">PoshCode</h1>
<p class="lead blog-description">Community resources for PowerShell coders</p>
</div>
<div class="row">
<div class="blog-post">
<h2 class="blog-post-title">Highlight-Syntax 1.0</h2>
<p class="blog-post-meta">
<span class="blog-post-time">2008-01-07</span> by <a class="blog-post-author">Jeff Hillman</a>
</p>
<h3>Download <a href="https://poshcode.org/scripts/100.ps1">Highlight-Syntax 1.0.ps1</a> - <a href="https://poshcode.org/scripts/99.html">parent</a></h3>
<p>This script uses regular expressions to highlight PowerShell syntax with HTML.</p>
<pre><code class="language-powershell"># Highlight-Syntax.ps1
# version 1.0
# by Jeff Hillman
#
# this script uses regular expressions to highlight PowerShell
# syntax with HTML.
param( [string] $code, [switch] $LineNumbers )
if ( Test-Path $code -ErrorAction SilentlyContinue )
{
$code = Get-Content $code | Out-String
}
$backgroundColor = "#DDDDDD"
$foregroundColor = "#000000"
$stringColor = "#800000"
$commentColor = "#008000"
$operatorColor = "#C86400"
$numberColor = "#800000"
$keywordColor = "#C86400"
$typeColor = "#404040"
$variableColor = "#000080"
$cmdletColor = "#C86400"
$lineNumberColor = "#404040"
filter Html-Encode( [switch] $Regex )
{
# some regular expressions operate on strings that have already
# been through this filter, so the patterns need to be updated
# to look for the encoded characters instead of the literal ones.
# we do it with this filter instead of directly in the regular
# expression so the expressions can be a bit more readable (ha!)
$_ = $_ -replace "&", "&amp;"
if ( $Regex )
{
$_ = $_ -replace "(?<!\(\?)<", "&lt;"
$_ = $_ -replace "(?<!\(\?)>", "&gt;"
}
else
{
$_ = $_ -replace "\t", " "
$_ = $_ -replace " ", "&nbsp;"
$_ = $_ -replace "<", "&lt;"
$_ = $_ -replace ">", "&gt;"
}
$_
}
# regular expressions
$operatorRegex = @"
((?x:
(?# assignment operators)
=|\+=|-=|\*=|/=|%=|
(?# arithmatic operators)
(?<!\de)
(\+|-|\*|/|%)(?![a-z])|
(?# unary operators)
\+\+|\-\-|
(?# logical operators)
(-and|-or|-not)\b|!|
(?# bitwise operators)
(-band|-bor)\b|
(?# redirection and pipeline operators)
2>>|>>|2>&1|1>&2|2>|>|<|\||
(?# comparison operators)
(
-[ci]? (?# case and case-insensitive variants)
(eq|ne|ge|gt|lt|le|like|notlike|match|notmatch|replace|contains|notcontains)\b
)|
(?# type operators)
(-is|-isnot|-as)\b|
(?# range and miscellaneous operators)
\.\.|(?<!\d)\.(?!\d)|&|::|:|,|``|
(?# string formatting operator)
-f\b
))
"@ | Html-Encode -Regex
$numberRegex = @"
((?x:
(
(?# hexadecimal numbers)
(\b0x[0-9a-f]+)|
(?# regular numbers)
(?<!&)
((\b[0-9]+(\.(?!\.))?[0-9]*)|((?<!\.)\.[0-9]+))
(?!(>>|>&[12]|>))
(?# scientific notation)
(e(\+|-)?[0-9]+)?
)
(
(?# type specifiers)
(l|ul|u|f|ll|ull)?
(?# size shorthand)
(b|kb|mb|gb)?
\b
)?
))
"@ | Html-Encode -Regex
$keyWordRegex = @"
((?x:
\b(
(?# don't match anything that looks like a variable or a parameter)
(?<![-$])
(
(?# condition keywords)
if|else|elseif|(?<!\[)switch(?!\])|
(?# loop keywords)
for|(?<!\|</span>&nbsp;)foreach(?!-object)|in|do|while|until|default|break|continue|
(?# scope keywords)
global|script|local|private|
(?# block keywords)
begin|process|end|
(?# other keywords)
function|filter|param|throw|trap|return
)
)\b
))
"@
$typeRegex = @"
((?x:
\[
(
(?# primitive types and arrays of those types)
((int|long|string|char|bool|byte|double|decimal|float|single)(\[\])?)|
(?# other types)
regex|array|xml|scriptblock|switch|hashtable|type|ref|psobject|wmi|wmisearcher|wmiclass
)
\]
))
"@
$cmdletNames = Get-Command -Type Cmdlet | Foreach-Object { $_.Name }
function Highlight-Other( [string] $code )
{
$highlightedCode = $code | Html-Encode
# operators
$highlightedCode = $highlightedCode -replace
$operatorRegex, "<span style='color: $operatorColor'>`$1</span>"
# numbers
$highlightedCode = $highlightedCode -replace
$numberRegex, "<span style='color: $numberColor'>`$1</span>"
# keywords
$highlightedCode = $highlightedCode -replace
$keyWordRegex, "<span style='color: $keywordColor'>`$1</span>"
# types
$highlightedCode = $highlightedCode -replace
$typeRegex, "<span style='color: $typeColor'>`$1</span>"
# Cmdlets
$cmdletNames | Foreach-Object {
$highlightedCode = $highlightedCode -replace
"\b($_)\b", "<span style='color: $cmdletColor'>`$1</span>"
}
$highlightedCode
}
$RegexOptions = [System.Text.RegularExpressions.RegexOptions]
$highlightedCode = ""
# we treat variables, strings, and comments differently because we don't
# want anything inside them to be highlighted. we combine the regular
# expressions so they are mutually exclusive
$variableRegex = '(\$(\w+|{[^}`]*(`.[^}`]*)*}))'
$stringRegex = @"
(?x:
(?# here strings)
@[`"'](.|\n)*?^[`"']@|
(?# double-quoted strings)
`"[^`"``]*(``.[^`"``]*)*`"|
(?# single-quoted strings)
'[^'``]*(``.[^'``]*)*'
)
"@
$commentRegex = "#[^\r\n]*"
[regex]::Matches( $code,
"(?<before>(.|\n)*?)" +
"((?<variable>$variableRegex)|" +
"(?<string>$stringRegex)|" +
"(?<comment>$commentRegex))",
$RegexOptions::MultiLine ) | Foreach-Object {
# highlight everything before the variable, string, or comment
$highlightedCode += Highlight-Other $_.Groups[ "before" ].Value
if ( $_.Groups[ "variable" ].Value )
{
$highlightedCode +=
"<span style='color: $variableColor'>" +
( $_.Groups[ 'variable' ].Value | Html-Encode ) +
"</span>"
}
elseif ( $_.Groups[ "string" ].Value )
{
$string = $_.Groups[ 'string' ].Value | Html-Encode
$string = "<span style='color: $stringColor'>$string</span>"
# we have to highlight each piece of multi-line strings
if ( $string -match "\r\n" )
{
# highlight any line continuation characters as operators
$string = $string -replace
"(``)(?=\r\n)", "<span style='color: $operatorColor'>``</span>"
$string = $string -replace
"\r\n", "</span>`r`n<span style='color: $stringColor'>"
}
$highlightedCode += $string
}
else
{
$highlightedCode +=
"<span style='color: $commentColor'>" +
$( $_.Groups[ 'comment' ].Value | Html-Encode ) +
"</span>"
}
# we need to keep track of the last position of a variable, string,
# or comment, so we can highlight everything after it
$lastMatch = $_
}
if ( $lastMatch )
{
# highlight everything after the last variable, string, or comment
$highlightedCode += Highlight-Other $code.SubString( $lastMatch.Index + $lastMatch.Length )
}
else
{
$highlightedCode = Highlight-Other $code
}
# add line breaks
$highlightedCode =
[regex]::Replace( $highlightedCode, '(?=\r\n)', '<br />', $RegexOptions::MultiLine )
# put the highlighted code in the pipeline
"<div style='width: 100%; " +
"/*height: 100%;*/ " +
"overflow: auto; " +
"font-family: Consolas, `"Courier New`", Courier, mono; " +
"font-size: 12px; " +
"background-color: $backgroundColor; " +
"color: $foregroundColor; " +
"padding: 2px 2px 2px 2px; white-space: nowrap'>"
if ( $LineNumbers )
{
$digitCount =
( [regex]::Matches( $highlightedCode, "^", $RegexOptions::MultiLine ) ).Count.ToString().Length
$highlightedCode = [regex]::Replace( $highlightedCode, "^",
"<li style='color: $lineNumberColor; padding-left: 5px'><span style='color: $foregroundColor'>",
$RegexOptions::MultiLine )
$highlightedCode = [regex]::Replace( $highlightedCode, "<br />", "</span><br />",
$RegexOptions::MultiLine )
"<ol start='1' style='border-left: " +
"solid 1px $lineNumberColor; " +
"margin-left: $( ( $digitCount * 10 ) + 15 )px; " +
"padding: 0px;'>"
}
$highlightedCode
if ( $LineNumbers )
{
"</ol>"
}
"</div>"
</code></pre>
</div>
<!-- sidebar? -->
</div>
<hr>
<footer class="blog-footer">
<p>Generated by Joel "Jaykul" Bennett - 2018</p>
</footer>
</div> <!-- /container -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="/js/main.js"></script>
<script src="/js/vendor/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59988721-1', 'auto');
ga('send', 'pageview');
$(function () {
$('#contentTabs a:first').tab('show')
})
</script>
</body>
</html>