9. February 2009
Andy Schneider
Lee Holmes recently wrote a post on PowerShell Syntax Highlighting. I have been using Windows Live Writer for a while now and have used a variety of ways to post code snippets, functions, and scripts.
I really like the look and feel of Lee's output. Nice and clean, easy to copy and paste, line numbers and color coding.
So of course I went and tried it using his script. I was able to copy and paste into word without a problem and get all the cool nice highlighting, but as soon as I tried to paste in Live Writer, it just pasted in what seemed to be raw text.
I went digging around and found an option in Live Writer called "Paste Special."
The default was "Thinned HTML."
Using "Keep Formatting" allowed me to paste in the code just as expected.
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
| function Set-File { param ( [parameter(Mandatory=$true,ValueFromPipeline=$true)] [string] $file )
$psise.CurrentOpenedRunspace.OpenedFiles.Add($file) }
function invoke-caretline { invoke-expression $([Regex]::Split($psISE.CurrentOpenedFile.Editor.text,"`r`n" )[$psISE.CurrentOpenedFile.Editor.caretline-1]) }
function invoke-region{ param ( [int] $num ) $ed = $psISE.CurrentOpenedFile.Editor $lines = [Regex]::Split($ed.text,"`r`n" ) $foundfirst = -1 $foundlast = -1
for($count = 0;$count -le $lines.length-1;$count++) { if ($lines[$count].startswith("#region") -and $lines[$count].contains("@$num")) { $foundfirst = $count;break} } if($foundfirst -gt -1) { for ($count = $foundfirst; $count -le $lines.length-1;$count++) { if ($lines[$count].startswith("#endregion") ) {$foundlast = $count;break} } # end For $lines if ($foundlast -gt -1) { $torun = "" $lines[$foundfirst..$foundlast] | % { $torun+=$_ + "`r`n"} invoke-expression $torun } # end if $foundLast } # End for $count } # End Function Invoke-Region
function Insert-Text{ param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [string] $text ) $currentFilePath = $psise.CurrentOpenedFile.FullPath $currentFile = $psIse.CurrentOpenedRunspace.OpenedFiles | where {$_.FullPath -eq $currentFilePath} $currentFile.editor.InsertText($text) }
function New-FunctionTemplate{ param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [string] $verb, [parameter(Mandatory=$true, ValueFromPipeline=$true)] [string] $noun )
$f = @" function $Verb-$Noun { param ( [parameter(Mandatory=$true, ValueFromPipeline=$true) [string] $p1 ) begin {} process {} end {} } "@ Insert-Text -text $f } |
I guess the next task is to write a plug-in for Jame's Write-CommandBlogPost