Posts Tagged ‘WordPress tip’
Mortal combat: Coloring Masters Cup 1/2 final
Who is the winner? Give the answer at last, may ask the intrigued reader. Well, the testing is almost over and soon comes the final. Two fightings were conducted, the honesty and independence is kept by using the same test script.
The first is between PowerGUI 2.4 (Copy as HTML) and IsePack of PowerShellPack:
https://powershelldevtools.wordpress.com/2011/05/06/test-code-2-2/
Next, this is a note that IsePack of PowerShellPack works well with multi-string here-strings:
https://powershelldevtools.wordpress.com/2011/05/06/test-code-3-isepack-vs-here-string/
What? It spoils multi-string here-strings too? This is obviously the result of sequential savings of the post. The WordPress’s bug most likely.
At last, the bout between twins, SparkPlug for PowerGUI and SparkPlug for ISE:
The twins are equally strong, but SparkPlug for PowerGUI was declared the winner on points due to the real mess-up in the ISE menu if you have previously installed and run IsePack.
All the post protected with the password 123 so as not to harm too young and too impressive powershellers, and to preserve the Google Environment from duplicated search spam too.
We have a leader! Currently the strongest fighter of all the desktop applications and the common candidate is SparkPlug of Script-Automating!
What’s about the final? I’d expect that the delegate of the WordPress web-services (grown up on the Internet users’ needs, please note) are trying and be fit to fight sooner or later. Really couln’t help waiting, the simplicity of copy-paste and sourcecode tag and the ability to highlight row against the embedded desktop heavyweight is tickling my nerves.
In response to the comment about using IsePack
A recommendation for using PowerShellPack’s IsePack Copy-ColoredAsHtml has been made in the comment to my post. I’m not a big fan of this huge package, but this is worth testing for publishing.
I use the same code copy-pasted from an ISE tab:
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
Below is the result of IsePack code preparation:
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 { 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
This looks great and very similar to the hand-made colored code from my post. But who has stolen my advanced function’s comment?! 🙂 All the text between <# and C:\> inclusively erased as a cow licked out.
WordPress PowerShell Code Coloring Test
I have already written about how it’s possibly to post Powershell code here. As a year turned, why don’t check the state of affairs again?
My example is very simple and doesn’t cover all aspects of code. On the other hand, great sheets of code are not what is easily comparable with eyes.
1. This way my example is eyed in ISE:
Bugs are rare, however, it’s necessary to list them:
1.1 String data used without quotes is colored as a function (line 10)
1.2 Methods are not colored. It’s a typical trick, however, since nobody may know what it will be after the run of code. After having run the code, it’s considered here that no reason to re-color already colored code. (lines 10 and 42)
1.3 Property ‘Value’ is not colored (line 42).
Anyway, the coloring left the reader in a mood that all is healthy here.
2. Using Copy as HTML in PowerGUI 2.4 and Chrome 11, after adding manually line breaks (why doesn’t it type
s?), spaces and deleting trailing spaces after backticks (not in this sample), the following is workable:
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-Hostscriptblock}.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
2.1 In the second code snippet we have numerous problems with names of functions, both where they are declared and where they are called.
2.2 Write-Host inside the second function
2.3 Such stuff like attributes in an advanced function.
To conclude this section, use the Copy as HTML option is a choice if manual editing doesn’t fatigue you.
3. WordPress provides a set of tags. Several parameters might do the life of a codeblogger simpler, especially 'highlight'.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 testHowever, the overall state is not appropriate, from my point of view. Yes, I know that the 'Frustration-Free' trademark is not WordPress's (as it is not Quest's too), but there is a room for improvement:
3.1 Variable names (lines 5, 6, 46, 48)
3.2 Function declarations and names (lines 11-13, 15, 21, 45-48)
3.3 A blob string (lines 7-9)
3.4 A specific to advanced functions comment-description (lines 23-35)
3.5 Types (lines 4-6, 10, 14)
3.6 Unquoted string (line 10)
I'll report these problems to Happiness engineers, maybe they share a bit of their happiness? 😉
For Those Who Wanted to Download Code Samples
Recently, I’ve been requested to put out a code sample. This blog is meant as dead for almost a year, but I visit its dashboard on a monthly basis just by a habit.
To fulfill the wish of the requester, I visited http://www.box.net since I almost forgot how to use a file box that is the only way I knew last year to share files on a WordPress blog. To my utter amusement, this blog counting four thousand visits and the most of them are counted after I wrote the last post, this blog has been read but not used as a download place.
The box statistics shows that files weren’t downloaded by anybody except me:
Actually, I’m surprised if people copy/paste code instead of using code samples. Meanwhile, the download is simple, even despite being tricked on the first use, and the steps you need perform as follows:
1. Navigate your mouse’s cursor to the rigth until you find the gray box:
2. Using the triangle arrows at the top and/or the bottom of the box, you may opt the sample you are interested in.
3. Click on the sample chosen. The message appears as shown below:
4. As I may suppose your choise is the Download button. However, the first try is not always successful:
At the same time, the standard Windows File Save dialog appears. Choose the path you want to save a sample and save the sample. If your download was successful, you’ll see the positively-grey message like this:
Otherwise, the message ‘Now downloading…’ is shown endlessly. If so, just close it by clicking into cross at the upper-right corner to close the message and do the same sequence of actions. The second time always finishes with the sample on the your hard drive.
All the steps described above are proceeded in the Firefox 4.0 Beta 12 instance that is set by default and no doubts that you may download samples instead of copying the code from pages.
How to put your Powershell code onto WordPress page
Supposing, you use the PowerGUI ScriptEditor. After writing a valuable fragment of code, you wanted to publish it in your WordPress blog. What would you do?
- copy and Paste as Plain Text into your post?
- copy and Paste from Word into your post?
Nope, the easiest way to do that is to
- Save your code As an HTML page
- open the page in any text or HTML editor of your choice
- copy all from <pre> tag to </pre> inclusively (don’t bother about Tools -> Options -> Text Editor -> Tab; both states work)
- activate ‘HTML’ tab in ‘Add New Post’ if you have ‘Visual’ one activated
- paste to Edit Post frame.
Now this code in your blog is ready to be copied and pasted in Editors of your followers; it works immediately without any editing.
How to publish your code from Powershell ISE? Copy and paste it into a new document of PowerGUI ScriptEditor and just follow recipe above…







