PoshCode Logo PowerShell Code Repository

Out-Html (modification of post by Vegard Hamar view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1612"></script>download | new post

Out-HTML – converts cmdlets help to HTML format
Minor modification of Vegard Hamar’s OUT-HTML to support modules instead of pssnapin’s. Originally based on Out-wiki by Dimitry Sotnikov (http://dmitrysotnikov.wordpress.com/2008/08/18/out-wiki-convert-powershell-help-to-wiki-format/)

Modify the invocation line at the bottom of the script if you want to document fewer command, subsets or snapins
Open default.htm to view in frameset or index.htm for index page with links.

  1. ################################################################################
  2. # Out-HTML - converts module functions or cmdlet help to HTML format
  3. # Minor modification of Vegard Hamar's OUT-HTML to support modules instead of pssnapin's
  4. # Based on Out-wiki by Dimitry Sotnikov (http://dmitrysotnikov.wordpress.com/2008/08/18/out-wiki-convert-powershell-help-to-wiki-format/)
  5. #
  6. # Modify the invocation line at the bottom of the script if you want to document
  7. # fewer command, subsets or snapins
  8. # Open default.htm to view in frameset or index.htm for index page with links.
  9. ################################################################################
  10. # Created By: Vegard Hamar
  11. ################################################################################
  12.  
  13. param($outputDir = "./help")
  14.  
  15. function FixString {
  16.         param($in = "")
  17.         if ($in -eq $null) {
  18.                 $in = ""
  19.         }
  20.         return $in.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;")
  21. }
  22.  
  23. function Out-HTML {
  24.         param($commands = $null, $outputDir = "./help")
  25.  
  26.         $commandsHelp = $commands | sort-object modulename, name | get-help -full
  27.  
  28.         #create an output directory
  29.         if ( -not (Test-Path $outputDir)) {
  30.                 md $outputDir | Out-Null
  31.         }
  32.  
  33.         #Generate frame page
  34.         $indexFileName = $outputDir + "/index.htm"
  35.        
  36.         #Generate frameset
  37. @'
  38. <html>
  39.         <head>
  40.                 <title>PowerShell Help</title>
  41.         </head>
  42.         <frameset cols="250,*">
  43.                 <frame src="./index.htm" />
  44.                 <frame src="" name="display"/>
  45.         </frameset>
  46. </html>
  47. '@ | Out-File "$outputDir/default.htm"
  48.  
  49.         #Generate index
  50. @'
  51. <html>
  52.         <head>
  53.                 <title>PowerShell Help</title>
  54.         </head>
  55.         <body>
  56. '@  | out-file $indexFileName
  57.  
  58.         $SnapIn = ""
  59.         foreach ($c in $commandsHelp) {
  60.                 if ($SnapIn -ne $c.modulename) {
  61.                         "<a href='#" + $c.modulename + "'>* " + $c.modulename.Replace(".", " ") + "</a></br>"   | out-file $indexFileName -Append
  62.                         $SnapIn = $c.modulename
  63.                 }
  64.         }
  65.  
  66.         $SnapIn = ""
  67.         foreach ($c in $commandsHelp) {
  68.                 if ($SnapIn -ne $c.modulename) {
  69.                         "<h3><a name='$($c.modulename)'>" +$c.modulename.Replace(".", " ") + "</a></h3>" | Out-File $indexFileName -Append
  70.                         $SnapIn = $c.modulename
  71.                 }
  72.                 "<a href='" + $c.name + ".htm' target='display'>* $($c.Name)</a></br>"   | out-file $indexFileName -Append
  73.         }
  74.  
  75.         #Generate all single help files
  76.         $outputText = $null
  77.         foreach ($c in $commandsHelp) {
  78.                 $fileName = ( $outputDir + "/" + $c.Name + ".htm" )
  79.  
  80. @"
  81. <html>
  82.         <head>
  83.                 <title>$($c.Name)</title>
  84.         </head>
  85.         <body>
  86.                 <h1>$($c.Name)</h1>
  87.                 <div>$($c.synopsis)</div>
  88.  
  89.                 <h2> Syntax </h2>
  90.                 <code>$(FixString($c.syntax | out-string  -width 2000).Trim())</code>  
  91.  
  92.                 <h2> Detailed Description </h2>
  93.                 <div>$(FixString($c.Description  | out-string  -width 2000))</div>
  94.  
  95.                 <h2> Related Commands </h2>
  96.                 <div>
  97. "@ | out-file $fileName
  98.                 foreach ($relatedLink in $c.relatedLinks.navigationLink) {
  99.                         if($relatedLink.linkText -ne $null -and $relatedLink.linkText.StartsWith("about") -eq $false){
  100.                                 "                       * <a href='$($relatedLink.linkText).htm'>$($relatedLink.linkText)</a><br/>" | out-file $fileName -Append        
  101.                         }
  102.                 }
  103.          
  104. @"
  105.                 </div>
  106.                 <h2> Parameters </h2>
  107.                 <table border='1'>
  108.                         <tr>
  109.                                 <th>Name</th>
  110.                                 <th>Description</th>
  111.                                 <th>Required?</th>
  112.                                 <th>Pipeline Input</th>
  113.                                 <th>Default Value</th>
  114.                         </tr>
  115. "@   | out-file $fileName -Append
  116.  
  117.                 $paramNum = 0
  118.                 foreach ($param in $c.parameters.parameter ) {
  119. @"
  120.                         <tr valign='top'>
  121.                                 <td>$($param.Name)&nbsp;</td>
  122.                                 <td>$(FixString(($param.Description  | out-string  -width 2000).Trim()))&nbsp;</td>
  123.                                 <td>$(FixString($param.Required))&nbsp;</td>
  124.                                 <td>$(FixString($param.PipelineInput))&nbsp;</td>
  125.                                 <td>$(FixString($param.DefaultValue))&nbsp;</td>
  126.                         </tr>
  127. "@  | out-file $fileName -Append
  128.                 }
  129.                 "               </table>}"  | out-file $fileName -Append
  130.    
  131.                 # Input Type
  132.                 if (($c.inputTypes | Out-String ).Trim().Length -gt 0) {
  133. @"
  134.                 <h2> Input Type </h2>
  135.                 <div>$(FixString($c.inputTypes  | out-string  -width 2000).Trim())</div>
  136. "@  | out-file $fileName -Append
  137.                 }
  138.    
  139.                 # Return Type
  140.                 if (($c.returnValues | Out-String ).Trim().Length -gt 0) {
  141. @"
  142.                 <h2> Return Values </h2>
  143.                 <div>$(FixString($c.returnValues  | out-string  -width 2000).Trim())</div>
  144. "@  | out-file $fileName -Append
  145.                 }
  146.          
  147.                 # Notes
  148.                 if (($c.alertSet | Out-String).Trim().Length -gt 0) {
  149. @"
  150.                 <h2> Notes </h2>
  151.                         "<div>$(FixString($c.alertSet  | out-string -Width 2000).Trim())</div>
  152. "@  | out-file $fileName -Append
  153.                 }
  154.  
  155.                 # Examples
  156.                 if (($c.examples | Out-String).Trim().Length -gt 0) {
  157.                         "               <h2> Examples </h2>"  | out-file $fileName -Append      
  158.                         foreach ($example in $c.examples.example) {
  159. @"
  160.                 <h3> $(FixString($example.title.Trim(('-',' '))))</h3>
  161.                                 <pre>$(FixString($example.code | out-string ).Trim())</pre>
  162.                                 <div>$(FixString($example.remarks | out-string -Width 2000).Trim())</div>
  163. "@  | out-file $fileName -Append
  164.                         }
  165.                 }
  166. @"
  167.         </body>
  168. </html>
  169. "@ | out-file $fileName -Append
  170.         }
  171. @"
  172.         </body>
  173. </html>
  174. "@ | out-file $indexFileName -Append
  175. }
  176.  
  177. Out-HTML ( get-command | where {$_.modulename -ne $null}) $outputDir

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me