Wednesday, March 21, 2012

PS Script to HTML for Blog


I needed something other than Blogger's QUOTE for script.

Try 1:
If (-not (Test-Path "$home\Documents1\WindowsPowerShell\Highlight-Syntax 2.0.ps1"))
{
    throw "You Need to get `'Highlight-Syntax 2.0.ps1`' from http://poshcode.org/1498`n`n"
}

$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("Out-Highlight",{
    if ($psise.CurrentFile.Editor.SelectedText) {
        $source  = $psise.CurrentFile.Editor.SelectedText
    } else {
        $source  =$psise.CurrentFile.Editor.Text
    }
    (& "$home\Documents\WindowsPowerShell\Highlight-Syntax 2.0.ps1" $source $true) |
          Out-Clipboard
},$null) | out-null


Now this worked, then I kept reading and found a Lee Holmes example that was much further along.

Try 2:
Get script from http://www.leeholmes.com/blog/2009/02/03/more-powershell-syntax-highlighting/ I had to fix some single quotes in the $tokenColours definition from copying off the site and the download wasn't clean either.

Around line 203 you find:

001
002
003
004
005
006
if (-not $psise.CurrentOpenedFile)
{
    Write-Error 'No script is available for copying.�
    return
}     
$text = $psise.CurrentOpenedFile.Editor.Text


From the try one modify this to:

001
002
003
004
005
006
007
008
009
010
if (-not $psise.CurrentFile)
{
    Write-Error 'No script is available for copying.�
    return
}
if ($psise.CurrentFile.Editor.SelectedText) {
    $text = $psise.CurrentFile.Editor.SelectedText
else {
    $text = $psise.CurrentFile.Editor.Text
}

...so I can pull a selection.

Now, I can copy with color to WordPad, but still not to MSWord, or the iframe that Blogger uses to compose in.  In Word I get the plain text and in Blogger I get nothing.

With:

001
[System.Windows.Clipboard]::GetDataObject().GetFormats()

The Script produces:

HTML Format
UnicodeText
System.String
Rich Text Format
and copy from Blogger produces:
Text
UnicodeText
System.String
HTML Format
Locale
OEMText
So I added the following.

001
002
$dataObject.SetText([string]$text, [Windows.TextDataFormat]::Text)
$dataObject.SetData([System.Windows.DataFormats]::OemText,[object]$text)

Still no go!

Local is a byte[4].  I assume it is EN_US or something similar to show localization.

I did a copy from each method then ran:

001
[System.Windows.Clipboard]::GetText([System.Windows.TextDataFormat]::Html)


There is some variation in building the "CF_HTML" header.  I think part of the problem may be formatting issues from making a copy of the script off the HTML, with converted quotes.

Reading this code and the associated blog, I understand how the HTML works much better now, but I also learned that PSCX Set-Clipboard has arguments to pupulate the HTML and RTF portions of the clipboard.  So that really solves my problem.  Unfortunately the solution is ignoring the problem.  But I don't see a need to reinvent the wheel.  The down side is that Set-Clipboard doesn't let you stack clipboard formats.  So I can specify RTF, Text, or HTML.  a bummer really.

Called Via an ISE Menu:

001
002
003
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("Copy Color",{
    (& "$home\Documents\WindowsPowerShell\Set-ClipboardScript.ps1")
},$null| out-null