Wednesday, February 13, 2008

Code Snippet plugin for Windows Live Writer

Update: It actually tested this layout on my test blog, but formatting is definitely not the same: On msgoodies, only the example without alternating and without container is pretty. I will look into this and do another update.

Thank you Leo Vildosola! You just made me a very happy blogger :)

I found his plugin for Windows Live Writer and it works great with PowerShell scripts (as well as Blogger). Inserting well-formatted, color-coded PowerShell code is now as easy as selecting 'Insert Code Snippet', pasting the code and pressing ok (and can even be easier, see the end of this entry). There are only two drawbacks -

  1. It is not copy-friendly (see below), as there are no line breaks. But my current method does not have that either
  2. The formatting is called MSH and not PowerShell, but I think I can live with that ;)

If you choose a layout with either line numbers or alternate formatting, it becomes copy-friendly.

Some examples

With line numbers and a container (not copy-friendly)

   1: # Set-SMSCacheSize


   2: param([int]$newSizeInMB=2000)


   3:  


   4: $sms=new-object -com UIResource.UIResourceMgr


   5: $ci=$sms.GetCacheInfo()


   6: if ($ci.TotalSize -ne $newSizeInMB) {


   7:     $ci.TotalSize=$newSizeInMB


   8:     "Size set to $newSizeInMB"


   9: }


  10: else {


  11:     "Size $newSizeInMB already correct"


  12: }






Alternating lines, no container, no line numbers (copy-friendly)





# Set-SMSCacheSize


param([int]$newSizeInMB=2000)


 


$sms=new-object -com UIResource.UIResourceMgr


$ci=$sms.GetCacheInfo()


if ($ci.TotalSize -ne $newSizeInMB) {


    $ci.TotalSize=$newSizeInMB


    "Size set to $newSizeInMB"


}


else {


    "Size $newSizeInMB already correct"


}







No line numbers, no container (not copy-friendly)




# Set-SMSCacheSize
param([int]$newSizeInMB=2000)

$sms=new-object -com UIResource.UIResourceMgr
$ci=$sms.GetCacheInfo()
if ($ci.TotalSize -ne $newSizeInMB) {
$ci.TotalSize=$newSizeInMB
"Size set to $newSizeInMB"
}
else {
"Size $newSizeInMB already correct"
}






No line numbers, no container, alternating lines (copy-friendly)





# Set-SMSCacheSize


param([int]$newSizeInMB=2000)


 


$sms=new-object -com UIResource.UIResourceMgr


$ci=$sms.GetCacheInfo()


if ($ci.TotalSize -ne $newSizeInMB) {


    $ci.TotalSize=$newSizeInMB


    "Size set to $newSizeInMB"


}


else {


    "Size $newSizeInMB already correct"


}







No line numbers, container, alternating lines (copy-friendly)





# Set-SMSCacheSize


param([int]$newSizeInMB=2000)


 


$sms=new-object -com UIResource.UIResourceMgr


$ci=$sms.GetCacheInfo()


if ($ci.TotalSize -ne $newSizeInMB) {


    $ci.TotalSize=$newSizeInMB


    "Size set to $newSizeInMB"


}


else {


    "Size $newSizeInMB already correct"


}






In the future, I'll probably stick with the no container, alternating lines style, although I would be glad to get rid of the alternation. I do not like how the container behaves when the browser is resized - YMMV.



BTW: It seems like the copy-friendliness is depending upon whether the generated HTML contains a single <pre> or multiple ones. Alternating lines and line-numbers uses a <pre> per line; without, a single <pre> is used.



Silent Mode



The plugin has another smart feature. You can enable silent mode. With silent mode, your clipboard is inserted with the lastest settings, just by clicking the 'Insert Code Snippet'. If you want to disable silent mode and do something else, simply ctrl-click the link. This is a great productivity feature.