More PowerShell Syntax Highlighting
Tuesday, 3 February 2009
Vladimir Averkin recently wrote a series of posts that show how to export code with syntax highlighting into HTML and RTF formats. It works great in Outlook, but was causing Windows Live Writer to crash. The reason is that the HTML stream of the clipboard isn’t just a blob of HTML – it’s supposed to be placed into the clipboard as CF_HTML. Investigation of that issue gave enough information to exactly pinpoint the crash in Live Writer, which they were quick to resolve once we pointed out. So it was a positive thing after all :)
While fixing the script, I took the opportunity to make the HTML prettier, work from both the ISE and the command-line, and fix a few bugs. I’ve posted it here: http://www.leeholmes.com/projects/scripts/Set-ClipboardScript.ps1.txt, as well as below.
|
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 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 |
################################################################################
# Set-ClipboardScript.ps1 # # The script entire contents of the currently selected editor window to system # clipboard. The copied data can be pasted into any application that supports # pasting in UnicodeText, RTF or HTML format. Text pasted in RTF or HTML # format will be colorized. # # See also: # http://blogs.msdn.com/powershell/archive/2009/01/13/ # how-to-copy-colorized-script-from-powershell-ise.aspx # http://www.leeholmes.com/blog/SyntaxHighlightingInPowerShell.aspx # http://www.leeholmes.com/blog/RealtimeSyntaxHighlightingInYourPowerShellConsole.aspx # ################################################################################ [CmdletBinding()] function Get-ScriptName if($path -and ([Threading.Thread]::CurrentThread.ApartmentState -ne "STA")) $tokenColours = @{ if($psise) Add-Type -Assembly System.Web # Create RTF block from text using named console colors. # Generate an HTML span and append it to HTML string builder $htmlColor = $tokenColours[$tokenColor].ToString().Replace(‘#FF’, ‘#’) if($tokenColor -eq ‘String’) $multipleLines = $false $newText = $line.TrimStart() function GetHtmlClipboardFormat($html) $header = $header.Replace("__LINES__", $lineBuilder.ToString()) function Main trap { break } # Do syntax parsing. # Initialize HTML builder. # Initialize RTF builder. # Append RTF color table which will contain all Powershell console colors. # Append RTF ending brace. [Windows.Clipboard]::SetDataObject($dataObject, $true) . Main |


Subscribe to this blog.
No. 1 — February 17th, 2009 at 1:16 pm
Hi,
thx for the improved script. Just one minor issue.
In line 127 $header = @" has one space character after the "
best regards
Rolf
PowerShell Usergroup Germany/Swiss/Austria
No. 2 — February 19th, 2009 at 6:47 pm
hi,
great job, thx for this update
one suggestion though
if you change line 96 to this: if (($tokenColor -eq ‘String’) -or ($tokenColor -eq ‘Comment’))
you will handle also the multiline comments correctly ^_^
No. 3 — April 10th, 2009 at 4:14 pm
Thanks! this worked really well.
No. 4 — November 27th, 2009 at 6:48 pm
I added lined 79 and 80 as follows:
$null = $codeBuilder.Append("<br />`r`n")
$null = $lineBuilder.Append("<BR />" -f $currentLine)
This is in:
if($tokenColor -eq ‘LineContinuation’)
{
$null = $codeBuilder.Append(‘`’)
$null = $codeBuilder.Append("<br />`r`n")
$null = $lineBuilder.Append("<BR />" -f $currentLine)
}
Now lines that are continued don’t get their own line numbers.
Karl
No. 5 — November 30th, 2009 at 6:25 pm
I also modified the function Main as follows: (Lines 204 and 210 in your above script, 206 and 212 if you add in my previous additions)
204 or 206 if (-not $psise.CurrentFile)
210 or 212 $text = $psise.CurrentFile.Editor.Text
Karl
No. 6 — March 6th, 2010 at 1:02 pm
Sorry but how do I install this script to ISE?
No. 7 — October 25th, 2010 at 7:45 pm
Lee;
I love this script, but I code exclusivly in PowerGUI, so I turned it into a script editor add-on for PowerGUI.
I thought I’d mention it, since I “borrowed” it from you ;)
http://www.powergui.org/entry.jspa?externalID=3021&categoryID=389
Karl
No. 8 — October 29th, 2010 at 7:53 pm
[...] on Lee Holmes Set-ClipboardScript located here: http://www.leeholmes.com/blog/2009/02/03/more-powershell-syntax-highlighting/This script editor add-on will copy the current window to HTML ready to post on a blog or web [...]
No. 9 — March 18th, 2011 at 10:55 am
Any chance this could be updated to reflect the RTM version of Powershell 2.0?
And how it might run as a menu item in the ISE?
No. 10 — May 1st, 2011 at 4:03 pm
[...] tools somehow completely ignored PowerShell, so I found PowerShell scripts that would do the job: PowerShell Syntax Highlighting by Lee Holmes, an extended version by Helge Klein, and different implementation, which will only [...]
No. 11 — December 30th, 2011 at 6:57 pm
[...] This is based on Lee Holmes Set-ClipboardScript located here: http://www.leeholmes.com/blog/2009/02/03/more-powershell-syntax-highlighting/ [...]