Advertisement
Guest User

Highlight-Syntax.ps1

a guest
Jan 21st, 2020
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param($Code)
  3.  
  4. # Load required assemblies
  5. [void] [System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
  6.  
  7. # Generate an HTML span and append it to HTML string builder
  8.  
  9. Function Append-HtmlSpan ($block, $TokenClass)
  10. {
  11.     If ( ( $TokenClass -eq 'NewLine' ) -or ( $TokenClass -eq 'LineContinuation' ) )
  12.     {
  13.         If ( $TokenClass -eq 'LineContinuation' )
  14.         {
  15.             $null = $CodeBuilder.Append("`")
  16.         }
  17.  
  18.         $null = $CodeBuilder.Append("`n")
  19.     }
  20.     Else
  21.     {
  22.         $Block = [System.Web.HttpUtility]::HtmlEncode($Block)
  23.        
  24.         If ( -not $Block.Trim() )
  25.         {
  26.             $Block = $Block.Replace(' ', ' ')
  27.         }
  28.  
  29.         If ( $TokenColor -eq 'String' )
  30.         {
  31.             $Lines = $block -split "`n"
  32.             $Block = ""
  33.  
  34.             $MultipleLines = $False
  35.            
  36.             ForEach ( $Line in $Lines )
  37.             {
  38.                 If ( $MultipleLines )
  39.                 {
  40.                     $Block += "`n"
  41.                 }
  42.  
  43.                 $NewText = $line.TrimStart()
  44.                 $NewText = " " * ($Line.Length - $NewText.Length) + $NewText
  45.                 $Block += $NewText
  46.                 $MultipleLines = $True
  47.             }
  48.         }
  49.  
  50.         $null = $CodeBuilder.Append("<span class='ps-$TokenClass'>$block</span>")
  51.     }
  52. }
  53.  
  54. Function Main
  55. {
  56.     # Do syntax parsing.
  57.     $Errors = $null
  58.     $Tokens = [System.Management.Automation.PsParser]::Tokenize( $Code, [ref]$Errors )
  59.  
  60.     # Initialize HTML builder.
  61.     $CodeBuilder = New-Object System.Text.StringBuilder
  62.  
  63.     # Iterate over the tokens and set the colors appropriately.
  64.     $position = 0
  65.    
  66.     ForEach ( $Token in $Tokens)
  67.     {
  68.         If ( $Position -lt $Token.Start )
  69.         {
  70.             $Block = $Code.Substring( $Position, ($Token.Start-$Position) )
  71.             $TokenColor = 'Unknown'
  72.             Append-HtmlSpan $block $TokenColor
  73.         }
  74.  
  75.         $Block = $Code.Substring( $Token.Start, $Token.Length )
  76.        
  77.         $TokenColor = $Token.Type.ToString()
  78.         Append-HtmlSpan $Block $TokenColor
  79.  
  80.         $Position = $Token.Start + $Token.Length
  81.     }
  82.  
  83.     # Build the entire syntax-highlighted script
  84.     $Code = $CodeBuilder.ToString()
  85.    
  86.     # Replace some problematic characters
  87.     $Code = $Code -replace '\t', '&#9;' -replace '\$', "&#36;" -replace '\{', '&#123;' -replace '\}', '&#125;'
  88.  
  89.     # Output HTML
  90.     $Code
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement