Blogger and Code Highlighting

Just a few quick notes on achieving nice code highlighting for sharing.


My preferred method is to use Visual Studio Code to achieve highlighting. 

If you have a language extension installed, then VS Code will copy with rich HTML formatting that looks sharp and consistent. But doesn't quite match the original. 

Here's a line of SQL:


SELECT CONVERT(DATETIME2(3), GETDATE())
;


And here's how it looks in my VS Code




I do wish that the layered bracket highlighting would transfer over, but honestly that's a very small difference, and I give VS Code high marks for quality of export.

Not long ago PowerShell code highlighting or SQL code highlighting was not this easy to achieve; since the most readily available editors didn't provide it natively.


My secondary method.

Code highlighting is from http://hilite.me/   Style: Monokai 

For PowerShell code I made a manual correction, to adjust the hard to read formatting applied to double quotes, semi-colons, etc.
Replacing:      <span style="color: #960050; background-color: #1e0010">
With:              <span style="color: #e6db74;">

This corrects the hard to read formatting applied to semi-colons, double quotes, etc.
The manual steps keep this from being ideal but, depending on the task, it is sufficient.

The real value that a tool like this can offer is the range of styles that you can switch between, where VS Code is dependent on theme you're running in.

It's worth holding on to for "just in case" occasions, like posting from a tablet.



A bit of caution regarding Blogger's Format HTML button.

Sometimes it's nice to jump into Blogger's HTML mode and do manual adjustments. But I found one side effect to be cautious of.

After using the Format HTML button, the formatting from VS Code was all out of alignment.
It's not a big problem, and simple to solve if you can repaste. But it's worth noting that it can be a trap for anyone inclined to come back and tweak an old post.

This is a code snippet directly from VS Code:

Function Get-FilesFromDialog()
{  
 Param(
    [Parameter(Mandatory=$False)][String[]]$myInitialDirectory = $env:USERPROFILE,
    [Parameter(Mandatory=$False)][String[]]$myTitle = "Open File",
    [Parameter(Mandatory=$False, )][String[]]$myFilterString = ""

 )

 [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $myInitialDirectory
 $OpenFileDialog.filter = "$myFilterString" + “All files (*.*)| *.*”
 $OpenFileDialog.Multiselect = $true;
 $OpenFileDialog.Title = $myTitle;
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filenames
}


And here is the same snippet after using the Format HTML button:

Function Get-FilesFromDialog()
{  
 Param(
    [Parameter(Mandatory=$False)][String[]]$myInitialDirectory = $env:USERPROFILE,
    [Parameter(Mandatory=$False)][String[]]$myTitle = "Open File",
    [Parameter(Mandatory=$False, )][String[]]$myFilterString = ""

 )

 [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $myInitialDirectory
 $OpenFileDialog.filter = "$myFilterString" + “All files (*.*)| *.*”
 $OpenFileDialog.Multiselect = $true;
 $OpenFileDialog.Title = $myTitle;
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filenames
}





Comments

Popular posts from this blog

Default Browser - set url association with PowerShell

LINQ within a foreach()

Rules of Automation