# encoding: ascii
# api: powershell
# title: Colorize.ps1
# description: This file was uploaded by a PowerGUI Script Editor Add-on.
# version: 0.1
# type: function
# author: Anonymous
# license: CC0
# function: Copy-Colored
# x-poshcode-id: 4262
# x-archived: 2013-06-29T06:39:58
# x-published: 2013-06-26T14:52:00
#
#
function Copy-Colored
{
<#
.Synopsis
Copies the currently selected text in the current file with colorization
.Description
Copies the currently selected text in the current file with colorization.
This allows for a user to paste colorized scripts into Word or Outlook
.Example
Copy-Colored
#>
param()
function Colorize
{
# colorize a script file or function
param([string]$Text, [int]$Start = -1, [int]$End = -1, [int]$FontSize = 12)
trap { break }
$rtb = New-Object Windows.Forms.RichTextBox
$rtb.Font = New-Object Drawing.Font "Consolas", $FontSize
$rtb.Text = $Text
# Now parse the text and report any errors...
$parse_errs = $null
$tokens = [system.management.automation.psparser]::Tokenize($rtb.Text,
[ref] $parse_errs)
if ($parse_errs) {
$parse_errs
return
}
$ColorPalette = New-ScriptPalette
# iterate over the tokens an set the colors appropriately...
foreach ($t in $tokens) {
$rtb.Select($t.start, $t.length)
$color = $ColorPalette[$t.Type.ToString()]
if ($color) {
$rtb.selectioncolor = [drawing.color]::FromArgb($color.A,
$color.R,
$color.G,
$color.B)
}
}
if ($start -eq -1 -and $end -eq -1) {
$rtb.select(0,$rtb.Text.Length)
} else {
$rtb.select($start, $end)
}
$rtb.Copy()
}
$text = Get-CurrentOpenedFileText
$selectedText = Select-CurrentText -NotInOutput -NotInCommandPane
if (-not $selectedText) {
$TextToColor = ($Text -replace '\r\n', "`n")
} else {
$TextToColor = ($selectedText -replace '\r\n', "`n")
}
Colorize $TextToColor
}
function Write-ColorizedHTML {
<#
.Synopsis
Writes Windows PowerShell as colorized HTML
.Description
Outputs a Windows PowerShell script as colorized HTML.
The script is wrapped in <PRE> tags with <SPAN> tags defining color regions.
.Example
Write-ColoredHTML {Get-Process}
#>
param(
# The Text to colorize
[Parameter(Mandatory=$true)]
[String]$Text,
# The starting within the string to colorize
[Int]$Start = -1,
# the end within the string to colorize
[Int]$End = -1)
trap { break }
#
# Now parse the text and report any errors...
#
$parse_errs = $null
$tokens = [Management.Automation.PsParser]::Tokenize($text,
[ref] $parse_errs)
if ($parse_errs) {
$parse_errs
return
}
$stringBuilder = New-Object Text.StringBuilder
$null = $stringBuilder.Append("<pre class='PowerShellColorizedScript'>")
# iterate over the tokens an set the colors appropriately...
$lastToken = $null
foreach ($t in $tokens)
{
if ($lastToken) {
$spaces = " " * ($t.Start - ($lastToken.Start + $lastToken.Length))
$null = $stringBuilder.Append($spaces)
}
if ($t.Type -eq "NewLine") {
$null = $stringBuilder.Append("
")
} else {
$chunk = $text.SubString($t.start, $t.length)
$colorChunk = "#$redChunk$greenChunk$blueChunk"
$null = $stringBuilder.Append("<span style='color:$colorChunk'>$chunk</span>")
}
$lastToken = $t
}
$null = $stringBuilder.Append("</pre>")
$stringBuilder.ToString()
}
function Copy-ColoredHTML
{
<#
.Synopsis
Copies the currently selected text in the current file as colorized HTML
.Description
Copies the currently selected text in the current file as colorized HTML
This allows for a user to paste colorized scripts into web pages or blogging
software
.Example
Copy-ColoredHTML
#>
param()
$currentText = Select-CurrentText -NotInCommandPane -NotInOutput
if (-not $currentText) {
# Try the current file
$currentFile = Get-CurrentOpenedFileText
$text = $currentFile
} else {
$text = $currentText
}
if (-not $text) { return }
$sb = [ScriptBlock]::Create($text)
$Error | Select-object -last 1 | ogv
$colorizedHTML = Write-ColorizedHTML -Text "$sb"
[Windows.Clipboard]::SetText($colorizedHTML )
return
$selectedRunspace = $psise.CurrentFile
$selectedEditor=$selectedRunspace.Editor
$FullText = $selectedEditor.Text -replace '\r\n', "`n"
if (-not $selectedEditor.SelectedText)
{
$colorizedText = Write-ColorizedHTML $selectedEditor.Text
}
else
{
$colorizedText = Write-ColorizedHTML $selectedEditor.SelectedText
}
}
Copy-Colored
Write-ColorizedHTML