Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell : Generating RTF Help from XML help files

In a post on my former blog, /\/\o\/\/ PowerShelled: Read the raw Monad XML helpfiles , I showed how to parse the XML help file of PowerShell

 (as this was in a Beta version of PowerShell you need to change the extension $mshHome by $PSHOME)

 

# XmlHelp.PS!
# Reading Xml Helpfile Example
# /\/\o\/\/ 2006

ls -recurse $PSHOME *-Help.xml | foreach {
  write-host -fore "blue" $_.Name
  $Help = [xml](gc $_.fullName)
  $help.helpitems.command | foreach {
    write-host -fore "red" $_.details.name;$_.details.description.get_InnerText()
    $_.Examples.example | foreach {write-host -fore "White"$_.title.para;write-host -fore "White"$_.code;$_.results}
  }
}

that did rip out all examples showing them a bit more collorfull like this :

 

I go on from that here using a Rich Text box to output the help in RTF format, and to show it in a form,

that looks like this :

 If you past the contents of this box into word you have a 164 Pages PowerShell Command Reference,

From here you can adapt the script so that the exported documentation is formatted the way that YOU like, for example add the systax setting or identing font extc.

And here the script :

(*Edit* as I use the Forms library you need to have that loaded : [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") Thanks for the comment Jim V, as I load it in my profile I keep forgettting that)

 

# Get-RtbHelp.ps1
#
# Generates a Rich Text PowerShell Command help from the XML help files
# and Shows this in a Form
#
# /\/\o\/\/ 2006
# http://ThePowerShellGuy.com

Function get-RtbHelp {

    $FontBold = new-object System.Drawing.Font("Microsoft Sans Serif",10,[Drawing.FontStyle]'Bold' )
    $FontHeader = new-object System.Drawing.Font("Microsoft Sans Serif",14,[Drawing.FontStyle]'Bold' )
    $fontNormal = new-object System.Drawing.Font("Microsoft Sans Serif",10,[Drawing.FontStyle]'Regular')
    $fontItalic = new-object System.Drawing.Font("Microsoft Sans Serif",10,[Drawing.FontStyle]'Italic')
    $fontCode = new-object System.Drawing.Font("Lucida Console",8,[Drawing.FontStyle]'Bold'  )
    
    $form = new-object Windows.Forms.form  
    $form.Size = new-object System.Drawing.Size @(1000,800)  
    $form.text = "PowerShell Command overview"
    $RTB = new-object System.Windows.Forms.RichTextBox 
    $RTB.Dock = [System.Windows.Forms.DockStyle]::Fill
    $RTB.Font = $fontNormal
    $RTB.DetectUrls = $false
    $form.Controls.Add($RTB)
    
    $rtb.SelectionColor = 'Green'
    $RTB.selectionFont  = $fontHeader
    $rtb.appendText("PowerShell Command overview`n")
    $RTB.selectionFont  = $fontBold
    $RTB.appendText("Generated from XML Help files by Get-RtbHelp`n") 
    $RTB.appendText("/\/\o\/\/ 2006 `nhttp://ThePowerShellGuy.com`n") 

    ls -recurse $PSHOME *-Help.xml | foreach { 
    
    $rtb.SelectionColor = 'Green'
        $RTB.selectionFont  = $fontHeader
    $RTB.appendText("`n`nHelp File : $($_.Name)`n") 
    $rtb.SelectionColor = 'Black'
    $Help = [xml](get-content $_.fullName) 
    $help.helpitems.command |
    
    foreach { 
    
        $RTB.selectionFont  = $fontHeader
        $rtb.SelectionColor = 'Blue'
        $RTB.appendText("____________________ $($_.details.name)")
        $rtb.SelectionIndent = 10
        $rtb.SelectionColor = 'Black'
        $RTB.appendText("`n")
    
        $RTB.selectionFont  = $fontBold
        $RTB.appendText($_.details.description.get_InnerText())
        $RTB.appendText("`n`n")
        
        $RTB.selectionFont  = $fontItalic
        $RTB.appendText($_.description.get_innertext())
        $RTB.appendText("`n")
        
        $RTB.selectionFont  = $fontBold
        $RTB.appendText("`nParameters:`n`n")
    
        $_.syntax.syntaxItem |foreach {
            $_.parameter |  foreach  {
    
                $RTB.selectionFont  = $fontBold
                $rtb.SelectionIndent = 20
                $RTB.appendText("-$($_.name)")
                $RTB.appendText("`n")
                $RTB.selectionFont  = $fontNormal
                
                $rtb.SelectionIndent = 40
                $RTB.appendText($_.description.para)
                $RTB.appendText("`n")
                $rtb.SelectionIndent = 10
            }
        }
    
            $RTB.selectionFont  = $fontBold
        $RTB.appendText("`nExamples:`n`n")
    
        $_.Examples.example | foreach {
    
            $rtb.SelectionIndent = 40
            $RTB.selectionFont  = $fontCode
            $rtb.SelectionColor = 'Red'
            $RTB.appendText($_.introduction.get_innertext())
    
            $rtb.SelectionColor = 'Black'
            $RTB.appendText($_.code)
            $RTB.appendText("`n")
            $RTB.selectionFont  = $fontCode
            $RTB.appendText($_.results)
            $RTB.appendText("`n")
    
            $rtb.SelectionIndent = 60
            $RTB.selectionFont  = $fontNormal
            $RTB.appendText($_.Remarks.get_innertext())
            $RTB.appendText("`n`n")
            $rtb.SelectionIndent = 0
        } 
    } 
    }

    $Form.Add_Shown({$form.Activate()}) 
    [void]$form.showdialog() 

}

get-RtbHelp

 

Enjoy,

Greetings,/\/\o\/\/
Tags : PowerShell

Published Sunday, December 24, 2006 11:18 AM by admin
Filed under: , , ,

Comments

# re: PowerShell : Generating RTF Help from XML help files

Congratulations on your new blog. I like the layout. Happy holidays!

-Abhishek

Sunday, December 24, 2006 10:12 PM by Abhishek225

# re: PowerShell : Generating RTF Help from XML help files

Code formatting looks excellent.

Chalk up one more for Community Server.

Sunday, December 24, 2006 11:57 PM by Jim V

# re: PowerShell : Generating RTF Help from XML help files

The code looks good but youu forgot to add the assembly references.

Monday, December 25, 2006 12:15 AM by Jim V

# re: PowerShell : Generating RTF Help from XML help files

Thanks for the comments

I added the loading of the assembly as a remark.

Greetings /\/\o\/\/

Monday, December 25, 2006 1:36 AM by MoW

# re: PowerShell : Generating RTF Help from XML help files

Hey, great job with the new blog.

The code is not double spaced any more ;p

The formatting looks quite nice so that I want to actually save that RTF file.

Monday, December 25, 2006 12:02 PM by DBMwS

# re: PowerShell : Generating RTF Help from XML help files

Hiya DbmwS,

Glad you like it,

You can copy the text from the Form into Word

but If you want to save this the RichTextBox also has a SaveFile() Method,

You can add this line before the Form.ShowDialog

$rtb.SaveFile('c:\PowerShell\help.rtf','RichText')

Also you can add

$RTB.appendText("`f") before the CmdLet name, to have a new page for a new CmdLet in the output (you will not see this in the RTB box but it will be in the RTF file )

Greetings /\/\o\/\/

Tuesday, December 26, 2006 9:23 AM by MoW

# Welcome to The PowerShell Guy blog

Welcome to my New Blog "The PowerShell Guy" With ( I hope and think ) : A Better Name and address ( http://ThePowerShellGuy.com

Friday, December 29, 2006 8:58 PM by The PowerShell Guy
Anonymous comments are disabled