Syntax Highlighting PowerShell Code in HTML - With a PowerShell Script
|
by Helge Klein on 01/18/2010 | 2 Comments | 2,320 Views
|
[Update 2010-06-14: fixed a bug in the script. It now has the version 1.1]
When you are publishing PowerShell code on the web, you soon discover that it looks much nicer with syntax highlighting - as did I. There are several ways to add syntax highlighting to your blog or web site, most of which rely on external (Wordpress) plugins or JavaScript on the page. I do not like that approach too much. I prefer to have the syntax highlighting embedded in the HTML code. That way I have full control over the appearance (and the highlighting also works in Google Reader). The logical solution is to use a script that embeds HTML coloring tags directly into the script.
Highlight-Syntax.ps1
My script Highlight-Syntax.ps1 is a much stripped down and slightly changed version of Lee Holmes' Set-ClipboardScript.ps1 - thank you for making it available, Lee!
You call Highlight-Syntax.ps1 with the name of a .PS1 script which it parses and then generates an HTML file with a colorized version of that script. If you input "Get-AppVPackageDependencies.ps1" you will get "Get-AppVPackageDependencies.ps1.html". That is it. The code in the HTML file can then easily be pasted into your favorite blog editor.
Highlight-Syntax.ps1 does not create fully-featured HTML files with head and body tags. That is by design. It simply takes your code and adds color information to it.
The Code - And a Sample
Here is the script which doubles as a sample of what its output looks like.
#
# Original Author: Lee Holmes, http://www.leeholmes.com/blog/MorePowerShellSyntaxHighlighting.aspx
#
# Modified by: Helge Klein, http://blogs.sepago.de/helge/
#
#
# Syntax highlights a PowerShell script.
#
# Usage: Supply the script to syntax hightligh as first and only parameter
#
# Output: Copy of original script with extension ".html"
#
# Example: .\Highlight-Syntax.ps1 .\Get-AppVPackageDependencies.ps1
#
[CmdletBinding()]
param($path)
$tokenColours = @{
'Attribute' = '#FFADD8E6'
'Command' = '#FF0000FF'
'CommandArgument' = '#FF8A2BE2'
'CommandParameter' = '#FF000080'
'Comment' = '#FF006400'
'GroupEnd' = '#FF000000'
'GroupStart' = '#FF000000'
'Keyword' = '#FF00008B'
'LineContinuation' = '#FF000000'
'LoopLabel' = '#FF00008B'
'Member' = '#FF000000'
'NewLine' = '#FF000000'
'Number' = '#FF800080'
'Operator' = '#FFA9A9A9'
'Position' = '#FF000000'
'StatementSeparator' = '#FF000000'
'String' = '#FF8B0000'
'Type' = '#FF008080'
'Unknown' = '#FF000000'
'Variable' = '#FFFF4500'
}
# Generate an HTML span and append it to HTML string builder
$currentLine = 1
function Append-HtmlSpan ($block, $tokenColor)
{
if (($tokenColor -eq 'NewLine') -or ($tokenColor -eq 'LineContinuation'))
{
if($tokenColor -eq 'LineContinuation')
{
$null = $codeBuilder.Append('`')
}
$null = $codeBuilder.Append("<br />`r`n")
}
else
{
$block = [System.Web.HttpUtility]::HtmlEncode($block)
if (-not $block.Trim())
{
$block = $block.Replace(' ', ' ')
}
$htmlColor = $tokenColours[$tokenColor].ToString().Replace('#FF', '#')
if($tokenColor -eq 'String')
{
$lines = $block -split "`r`n"
$block = ""
$multipleLines = $false
foreach($line in $lines)
{
if($multipleLines)
{
$block += "<BR />`r`n"
}
$newText = $line.TrimStart()
$newText = " " * ($line.Length - $newText.Length) + $newText
$block += $newText
$multipleLines = $true
}
}
$null = $codeBuilder.Append("<span style='color:$htmlColor'>$block</span>")
}
}
function Main
{
$text = $null
if($path)
{
$text = (Get-Content $path) -join "`r`n"
}
else
{
Write-Error 'Please supply the path to the PowerShell script to syntax highlight as first (and only) parameter.'
return
}
trap { break }
# Do syntax parsing.
$errors = $null
$tokens = [system.management.automation.psparser]::Tokenize($Text, [ref] $errors)
# Initialize HTML builder.
$codeBuilder = new-object system.text.stringbuilder
# Iterate over the tokens and set the colors appropriately.
$position = 0
foreach ($token in $tokens)
{
if ($position -lt $token.Start)
{
$block = $text.Substring($position, ($token.Start - $position))
$tokenColor = 'Unknown'
Append-HtmlSpan $block $tokenColor
}
$block = $text.Substring($token.Start, $token.Length)
$tokenColor = $token.Type.ToString()
Append-HtmlSpan $block $tokenColor
$position = $token.Start + $token.Length
}
# Build the entire syntax-highlighted script
$code = $codeBuilder.ToString()
# Replace tabs with three blanks
$code = $code -replace "\t"," "
# Write the HTML to a file
$code | set-content -path "$path.html"
}
. Main
Download
You can download a digitally signed version of the script below.
| Attachment | Size |
|---|---|
| Highlight-Syntax_v1.1.zip | 5.04 KB |
- ‹‹‹ previous Article
- 114 of 129
- next Article ›››
[Your opportunity] IT-Competence and work-live-balance is our USP. Continue your career with sepago! Immediate job offers
2 responses for "Syntax Highlighting PowerShell Code in HTML - With a PowerShell Script" |
Add Comment
![]() |
Helge Klein IT-Architect Blogs about Windows, Terminal Services and other things Personal Profile Personal Blog RSS-Feed ![]() Twitter: HelgeKlein ![]() |
|
Related Jobs
Latest posts
| 07/23/2010 | How to Modify Default Share Permissions and Other Tweaks |
| 07/12/2010 | Will We Use Cloud-Based Virtual Desktops in 2020? |
| 06/21/2010 | Registry Tricks |
Most viewed
| Title | |
|---|---|
| 32,780 Views |
Where is the Hosts File on Windows x64? |
| 27,441 Views |
Deleting a Local User Profile - Not as easy as one Might Assume |
| 19,463 Views |
How to Reduce the Size of Roaming Profiles |





