PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

Posts Tagged ‘WordPress support

WordPress and PowerShell Code. The Second Try

with 4 comments

Last week I complained about how it is not easy to post Powershell code here. The letter of my dissatisfaction has been sent to and the answer was received from the WordPress support.

As a first step of our efforts to improve the code coloration was the question about what is wrong and what I’ve wanted. Of course, I assured the support specialist that I’ll provide so many examples of code that they will be able to polish publishing if they want to. That time the code sample appeared as on the following figure:

This post is the next step. I copy-pasted the WordPress code from my previous post on this topic to LibreOffice Writer 3.3, marked the most of significant blocks of code with several colors and put it back to WordPress page as an HTML.

Below is what I’d expect I might have had in my blog when I use the ‘sourcecode’ tag:

cls
#region WordPress posting code test
#this is a test of Powershell code coloring
[string]$stringVar1 = “string 1”;
[string]$private:stringVar2 = ‘string 2’;
[string]$script:stringVar3=
@’
string data
‘@
[scriptblock]$global:sb = {{Write-Host scriptblock}.Invoke();};
function
write1{Write-Host $stringVar1;}
function private:write2
{param([string]$str2 = )Write-Host $str2;}
function script:write3
#this is a function
{
Write-Host $script:stringVar3;
}
function global:Print-SB
{
<#
.SYNOPSIS
This is a code coloring test.

.DESCRIPTION
This test function represents an advanced Powershell function syntax.

.PARAMETER Param
Demonstrates how a scriptblock can be passed as a reference.

.EXAMPLE
PS C:\> Print-SB ([ref]$sb)
#>
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[ref]$Param
)
Begin{}
Process{$Param.Value.Invoke()}
End{}
}
write1
private:write2 $private:stringVar2;
script:write3
Print-SB ([ref]$global:sb)
#endregion WordPress posting code test

I divided all the sample code into several meaningful groups. Although the exact color schema is a subject of possible further discussion, I used some, taken from both Powershell ISE and PowerGUI as an average. Here are groups instructions of the sample were split into.

1) the comments (green) group includes one-string comment beginning with the # sign and a multi-string comment applicable for advanced functions and consisting of all between <# and #> character sequences (sequences are included too).

2) commandlets and commands (some blue-gray color that has been used in the original WordPress’s coloration), and their aliases like Out-Null, Get-ChildItem, dir. There is a fixed list of them. Custom aliases and commandlets are not supposed to be colored due to the fact that WordPress doesn’t run any PS code and it’s completely unaware of new constructs.

3) data types and attributes (light-blue or green-blue) group constitutes of all the code in square parentheses but inner round parentheses.

4) the string data (brown) group is comprised of one-string declarations limited with single quotes or double quotes and a multi-string construction beginning with @’ and finishing at ‘@.

5) variable names (magenta) includes all unquoted string beginning with the $ sign and containing letters, numbers, underlines, question mark and colon. The dot is an end as well as the space.

6) specific words used within function declarations (bright blue). This groups the word function, and the function-related words param, begin, process, end inside curly brackets following the word function.

This color also can be used for statements like foreach, for, switch, if and so on.

7) function names (light lilac) of two types of occurrence: within the function declaration (that is, following the word function) and anywhere in the code. Function names usually contain letters, numbers and colons.

I hope that there is no need to repeat how lucky would be all WordPress Powershell bloggers if the WordPress developing team does the requested.

Written by Alexander Petrovskiy

April 30, 2011 at 5:25 pm

Posted in Powershell, WordPress

Tagged with