As you might have noticed, I’m using jQuery.Syntax as my Syntax Highlighting engine. As I recently started writing PowerShell scripts, I wanted syntax hightlighting for it.
Since I knew, someone already wrote syntax highlighting for GeSHi for PowerShell (I am lazy after all!), I simply took the color scheme (shame on me), and made hacked ahead. After about three hours (yeah, I had some trouble with editing the wrong damn file), I came up with this:
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 |
Syntax.register('powershell', function(brush) { // PowerShell Cmdlets var cmdlets = [ "Add-Content", "Add-History", "Add-Member", "Add-PSSnapin", "Clear-Content", "Clear-Item", "Clear-ItemProperty", "Clear-Variable", "Compare-Object", "ConvertFrom-SecureString", "Convert-Path", "ConvertTo-Html", "ConvertTo-SecureString", "Copy-Item", "Copy-ItemProperty", "Export-Alias", "Export-Clixml", "Export-Console", "Export-Csv", "ForEach-Object", "Format-Custom", "Format-List", "Format-Table", "Format-Wide", "Get-Acl", "Get-Alias", "Get-AuthenticodeSignature", "Get-ChildItem", "Get-Command", "Get-Content", "Get-Credential", "Get-Culture", "Get-Date", "Get-EventLog", "Get-ExecutionPolicy", "Get-Help", "Get-History", "Get-Host", "Get-Item", "Get-ItemProperty", "Get-Location", "Get-Member", "Get-PfxCertificate", "Get-Process", "Get-PSDrive", "Get-PSProvider", "Get-PSSnapin", "Get-Service", "Get-TraceSource", "Get-UICulture", "Get-Unique", "Get-Variable", "Get-WmiObject", "Group-Object", "Import-Alias", "Import-Clixml", "Import-Csv", "Invoke-Expression", "Invoke-History", "Invoke-Item", "Join-Path", "Measure-Command", "Measure-Object", "Move-Item", "Move-ItemProperty", "New-Alias", "New-Item", "New-ItemProperty", "New-Object", "New-PSDrive", "New-Service", "New-TimeSpan", "New-Variable", "Out-Default", "Out-File", "Out-Host", "Out-Null", "Out-Printer", "Out-String", "Pop-Location", "Push-Location", "Read-Host", "Remove-Item", "Remove-ItemProperty", "Remove-PSDrive", "Remove-PSSnapin", "Remove-Variable", "Rename-Item", "Rename-ItemProperty", "Resolve-Path", "Restart-Service", "Resume-Service", "Select-Object", "Select-String", "Set-Acl", "Set-Alias", "Set-AuthenticodeSignature", "Set-Content", "Set-Date", "Set-ExecutionPolicy", "Set-Item", "Set-ItemProperty", "Set-Location", "Set-PSDebug", "Set-Service", "Set-TraceSource", "Set-Variable", "Sort-Object", "Split-Path", "Start-Service", "Start-Sleep", "Start-Transcript", "Stop-Process", "Stop-Service", "Stop-Transcript", "Suspend-Service", "Tee-Object", "Test-Path", "Trace-Command", "Update-FormatData", "Update-TypeData", "Where-Object", "Write-Debug", "Write-Error", "Write-Host", "Write-Output", "Write-Progress", "Write-Verbose", "Write-Warning" ]; brush.push(cmdlets, {klass: 'cmdlet'}); // Aliases var aliases = [ "ac", "asnp", "clc", "cli", "clp", "clv", "cpi", "cpp", "cvpa", "diff", "epal", "epcsv", "fc", "fl", "foreach", "ft", "fw", "gal", "gc", "gci", "gcm", "gdr", "ghy", "gi", "gl", "gm", "gp", "gps", "group", "gsv", "gsnp", "gu", "gv", "gwmi", "iex", "ihy", "ii", "ipal", "ipcsv", "mi", "mp", "nal", "ndr", "ni", "nv", "oh", "rdr", "ri", "rni", "rnp", "rp", "rsnp", "rv", "rvpa", "sal", "sasv", "sc", "select", "si", "sl", "sleep", "sort", "sp", "spps", "spsv", "sv", "tee", "?", "write", "cat", "cd", "clear", "cp", "h", "history", "kill", "lp", "ls", "mount", "mv", "popd", "ps", "pushd", "pwd", "r", "rm", "rmdir", "echo", "cls", "chdir", "copy", "del", "dir", "erase", "move", "rd", "ren", "set", "type" ]; brush.push(aliases, {klass: 'alias'}); // Reserved words var keywords = [ "break", "continue", "do", "for", "foreach", "exit", "default", "while", "if", "switch", "until", "where", "function", "filter", "else", "elseif", "in", "return", "param", "throw", "trap" ]; brush.push(keywords, {klass: 'keyword'}); // Options var options = [ "-Year", "-Wrap", "-Word", "-Width", "-WhatIf", "-Wait", "-View", "-Verbose", "-Verb", "-Variable", "-ValueOnly", "-Value", "-Unique", "-UFormat", "-TypeName", "-Trace", "-TotalCount", "-Title", "-TimestampServer", "-TargetObject", "-Syntax", "-SyncWindow", "-Sum", "-String", "-Strict", "-Stream", "-Step", "-Status", "-Static", "-StartupType", "-Start", "-StackName", "-Stack", "-SourceId", "-SimpleMatch", "-ShowError", "-Separator", "-SecureString", "-SecureKey", "-SecondValue", "-SecondsRemaining", "-Seconds", "-Second", "-Scope", "-Root", "-Role", "-Resolve", "-RemoveListener", "-RemoveFileListener", "-Registered", "-ReferenceObject", "-Recurse", "-RecommendedAction", "-ReadCount", "-Quiet", "-Query", "-Qualifier", "-PSSnapin", "-PSProvider", "-PSHost", "-PSDrive", "-PropertyType", "-Property", "-Prompt", "-Process", "-PrependPath", "-PercentComplete", "-Pattern", "-PathType", "-Path", "-PassThru", "-ParentId", "-Parent", "-Parameter", "-Paging", "-OutVariable", "-OutBuffer", "-Option", "-OnType", "-Off", "-Object", "-Noun", "-NoTypeInformation", "-NoQualifier", "-NoNewline", "-NoElement", "-NoClobber", "-NewName", "-Newest", "-Namespace", "-Name", "-Month", "-Minutes", "-Minute", "-Minimum", "-Milliseconds", "-Message", "-MemberType", "-Maximum", "-LogName", "-LiteralPath", "-LiteralName", "-ListenerOption", "-List", "-Line", "-Leaf", "-Last", "-Key", "-ItemType", "-IsValid", "-IsAbsolute", "-InputObject", "-IncludeEqual", "-IncludeChain", "-Include", "-IgnoreWhiteSpace", "-Id", "-Hours", "-Hour", "-HideTableHeaders", "-Head", "-GroupBy", "-Functionality", "-Full", "-Format", "-ForegroundColor", "-Force", "-First", "-FilterScript", "-Filter", "-FilePath", "-Expression", "-ExpandProperty", "-Expand", "-ExecutionPolicy", "-ExcludeProperty", "-ExcludeDifferent", "-Exclude", "-Exception", "-Examples", "-ErrorVariable", "-ErrorRecord", "-ErrorId", "-ErrorAction", "-End", "-Encoding", "-DisplayName", "-DisplayHint", "-DisplayError", "-DifferenceObject", "-Detailed", "-Destination", "-Description", "-Descending", "-Depth", "-DependsOn", "-Delimiter", "-Debugger", "-Debug", "-Days", "-Day", "-Date", "-CurrentOperation", "-Culture", "-Credential", "-Count", "-Container", "-Confirm", "-ComputerName", "-Component", "-Completed", "-ComObject", "-CommandType", "-Command", "-Column", "-Class", "-ChildPath", "-Character", "-Certificate", "-CategoryTargetType", "-CategoryTargetName", "-CategoryReason", "-CategoryActivity", "-Category", "-CaseSensitive", "-Body", "-BinaryPathName", "-Begin", "-BackgroundColor", "-Average", "-AutoSize", "-Audit", "-AsString", "-AsSecureString", "-AsPlainText", "-As", "-ArgumentList", "-AppendPath", "-Append", "-Adjust", "-Activity", "-AclObject" ]; brush.push(options, {klass: 'option'}); // Operators var operators = [ "-eq", "-ne", "-gt", "-ge", "-lt", "-le", "-ieq", "-ine", "-igt", "-ige", "-ilt", "-ile", "-ceq", "-cne", "-cgt", "-cge", "-clt", "-cle", "-like", "-notlike", "-match", "-notmatch", "-ilike", "-inotlike", "-imatch", "-inotmatch", "-clike", "-cnotlike", "-cmatch", "-cnotmatch", "-contains", "-notcontains", "-icontains", "-inotcontains", "-ccontains", "-cnotcontains", "-isnot", "-is", "-as", "-replace", "-ireplace", "-creplace", "-and", "-or", "-band", "-bor", "-not", "-bnot", "-f", "-casesensitive", "-exact", "-file", "-regex", "-wildcard", "+=", "-=", "*=", "/=", "%=", "*", "/", "%", "=", "!" , "+", "-" ]; brush.push(operators, {klass: 'operator'}); // Symbols var symbols = [ "(", ")", "[", "]", "{", "}", "", "|", "&", "@" ]; brush.push(symbols, {klass: 'symbol'}); // Constants var constants = [ "SilentlyContinue" ]; brush.push(constants, {klass: 'constant'}); // Variables brush.push({ pattern: /($|@|%)w+/gi, klass: 'variable' }); // Comments brush.push(Syntax.lib.perlStyleComment); brush.push(Syntax.lib.webLink); // Strings brush.push(Syntax.lib.singleQuotedString); brush.push(Syntax.lib.doubleQuotedString); brush.push(Syntax.lib.stringEscape); // Numbers brush.push(Syntax.lib.decimalNumber); brush.push(Syntax.lib.hexNumber); // Functions brush.push(Syntax.lib.cStyleFunction); }); |
You’ll also need the fitting cascading style sheet for this:
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 |
.syntax .powershell .cmdlet, .syntax .powershell .alias { color: #008080; font-weight: bold; } .syntax .powershell .symbol { color: pink; } .syntax .powershell .variable { color: #800080; font-style: normal; font-weight: bold; } .syntax .powershell .constant { color: #ff3333; } .syntax .powershell .keyword { color: #0000FF; } .syntax .powershell .operator { color: #FF0000; } .syntax .powershell .option { color: #008080; font-style: italic; } .syntax .powershell .comment { color: #008000; font-style: normal; } .syntax .powershell .string { color: #800000; } .syntax .powershell .number { color: #000000; } |
I was also trying to create an “addon language” for PowerShell, PowerCLI. But that didn’t work right and I guess I’ll have to talk to the guy writing jQuery.Syntax on how to properly use Syntax.brushes.dependency.