#requires -version 2.0 # Highlight-Syntax.ps1 # version 2.0 # by Jeff Hillman # # this script uses the System.Management.Automation.PsParser class # 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" $lineNumberColor = "#404040" $PSTokenType = [System.Management.Automation.PSTokenType] $colorHash = @{ # $PSTokenType::Unknown = $foregroundColor; $PSTokenType::Command = "#C86400"; # $PSTokenType::CommandParameter = $foregroundColor; # $PSTokenType::CommandArgument = $foregroundColor; $PSTokenType::Number = "#800000"; $PSTokenType::String = "#800000"; $PSTokenType::Variable = "#000080"; # $PSTokenType::Member = $foregroundColor; # $PSTokenType::LoopLabel = $foregroundColor; # $PSTokenType::Attribute = $foregroundColor; $PSTokenType::Type = "#404040"; $PSTokenType::Operator = "#C86400"; # $PSTokenType::GroupStart = $foregroundColor; # $PSTokenType::GroupEnd = $foregroundColor; $PSTokenType::Keyword = "#C86400"; $PSTokenType::Comment = "#008000"; $PSTokenType::StatementSeparator = "#C86400"; # $PSTokenType::NewLine = $foregroundColor; $PSTokenType::LineContinuation = "#C86400"; # $PSTokenType::Position = $foregroundColor; } filter Html-Encode { $_ = $_ -replace "&", "&" $_ = $_ -replace " ", " " $_ = $_ -replace "<", "<" $_ = $_ -replace ">", ">" $_ } # replace the tabs with spaces $code = $code -replace "\t", ( " " * 4 ) if ( $LineNumbers ) { $highlightedCode = "