PoshCode Logo PowerShell Code Repository

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

ConvertTo-DekiContent is an improvement on New-HtmlHelp, with a specific focus on output suitable for DekiWiki. I commented out the html and body tags, etc. because the HTML markup is destined for the DekiWiki. It’s an improvement over the original because we cleaned up the parameter and example code, and broke apart the syntax, so it’s easier to read.

NOTE: This has been tested as a v2 Module, but should work in v1 (_fingers crossed_)

  1. ## ConvertTo-DekiContent (aka Convert Help to Html)
  2. ####################################################################################################
  3. ## Converts the -Full help output to HTML markup for insertion into web pages.
  4. ####################################################################################################
  5. ## Usage:
  6. ##
  7. ## foreach($cmd in (gcm -type cmdlet | ? { $_.PsSnapin -like "Microsoft.PowerShell*" })) {
  8. ##
  9. ##    Get-Help $cmd.Name -full | ConvertTo-DekiContent Cmdlet_Help |
  10. ##    %{ Set-DekiContent "Cmdlet_Help/$($cmd.PSSnapin)/$($cmd.Name)" $_ }
  11. ## }
  12. ##
  13. ####################################################################################################
  14. ## History:
  15. ## v2.0 - Refactoring of markup and code by Joel "Jaykul" Bennett to avoid line-wrapping, and 'pre'
  16. ##        blocks in the code and to format the parameters and examples more like the originals.
  17. ## v1.0 - Original version by http://blogs.vmware.com/vipowershell/2007/09/new-htmlhelp.html
  18. ####################################################################################################
  19.  
  20. #Import System.Web in order to use HtmlEncode functionality
  21. [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
  22.  
  23. ## Get-HtmlHelp - A Helper function for generating help:
  24. ## Usage:  Get-HtmlHelp Get-*
  25. function Get-HtmlHelp {
  26.    param([string[]]$commands, [string]$baseUrl)
  27.    $commands | Get-Command -type Cmdlet -EA "SilentlyContinue" | get-help -Full | ConvertTo-DekiContent $baseUrl
  28. }
  29.  
  30. function ConvertTo-DekiContent {
  31. param($baseUrl)
  32. PROCESS {
  33.    if($_ -and ($_.PSObject.TypeNames -contains "MamlCommandHelpInfo#FullView")) {
  34.       $help = $_
  35.      
  36.       # Name isn't needed, since this is going as the body, but ...
  37.       # $data = "<html><head><title>$(encode($help.Name))</title></head><body>";
  38.       # $data += "<h1>$(encode($help.Name))</h1>"
  39.    
  40.       # Synopsis
  41.       $data += "<h2>Synopsis</h2>$($help.Synopsis | Out-HtmlPara)"
  42.      
  43.       # Syntax
  44.       $data += "<h2>Syntax</h2>$($help.Syntax | Out-HtmlPara)"
  45.    
  46.       # Related Commands
  47.       $data += "<h2>Related Commands</h2>"
  48.       foreach ($relatedLink in $help.relatedLinks.navigationLink) {
  49.          if($relatedLink.linkText -ne $null -and $relatedLink.linkText.StartsWith("about") -eq $false) {
  50.             $uri = ""
  51.             if( $relatedLink.uri -ne "" ) {
  52.                $uri = $relatedLink.uri
  53.             } else{
  54.                $uri = "$baseUrl/$((get-command $relatedLink.linkText -EA "SilentlyContinue").PSSnapin.Name)/$($relatedLink.linkText)"
  55.             }
  56.             $data += "<a href='$(encode($uri)).html'>$(encode($relatedLink.linkText))</a><br>"
  57.          }
  58.       }
  59.    
  60.       # Detailed Description
  61.       $data += "<h2>Detailed Description</h2>$(encode(&{$help.Description | out-string -width 200000}))"
  62.    
  63.       # Parameters
  64.       $data += "<h2>Parameters</h2>"
  65.       $help.parameters.parameter | %{
  66.          $param = $_
  67.          $data += "<h4>-$(encode($param.Name)) [&lt;$(encode($param.type.name))&gt;]</h4>"
  68.          $data += $param.Description | Out-HtmlPara
  69.          $data += "<table>"
  70.          $data += "<tr><th>Required? &nbsp;</th><td> $(encode($param.Required))</td></tr>"
  71.          $data += "<tr><th>Position? &nbsp;</th><td> $(encode($param.Position))</td></tr>"
  72.          $data += "<tr><th>Default value? &nbsp;</th><td> $(encode($param.defaultValue))</td></tr>"
  73.          $data += "<tr><th>Accept pipeline input? &nbsp;</th><td> $(encode($param.pipelineInput))</td></tr>"
  74.          $data += "<tr><th>Accept wildcard characters? &nbsp;</th><td> $(encode($param.globbing))</td></tr></table>"
  75.       }
  76.    
  77.       if($help.inputTypes) {
  78.          # Input Type
  79.          $data += "<h3>Input Type</h3>$($help.inputTypes | Out-HtmlPara)"
  80.       }
  81.       if($help.returnValues) {
  82.          # Return Type
  83.          $data += "<h3>Return Type</h3>$($help.returnValues | Out-HtmlPara)"
  84.       }
  85.       # Notes
  86.       $data += "<h2>Notes</h2>$($help.alertSet | Out-HtmlPara)"
  87.    
  88.       # Examples
  89.       $data += "<h2>Examples</h2>"
  90.      
  91.       $help.Examples.example | %{
  92.          $example = $_
  93.          $data += "<h4>$(encode($example.title.trim(' -')))</h4>"
  94.          $data += "<code><strong>PS&gt;</strong>&nbsp;$(encode($example.code))</code>"
  95.          $data += "<p>$($example.remarks | out-string -width ([int]::MaxValue) | Out-HtmlPara)</p>"
  96.  
  97.       }
  98.       # $data += "</body>"
  99.  
  100.       write-output $data
  101.    } else {
  102.       Write-Error "Can only process -Full view help output"
  103.    }
  104. }}
  105.  
  106.  
  107.  
  108. function encode($str) {
  109.    begin{ if($str){ $str.split("`n") | encode  } }
  110.    process{ if($_){ [System.Web.HttpUtility]::HtmlEncode($_).Trim() } }
  111. }
  112.  
  113. function trim($str) {
  114.    begin{ if($str){ $str.Trim() } }
  115.    process{ if($_){ $_.Trim() } }
  116. }
  117.  
  118. function split($Separator="`n",$inputObject) {
  119.    begin{ if($inputObject){ $inputObject | split $Separator } }
  120.    process{ if($_){ [regex]::Split($_,$Separator) | ? {$_.Length} } }
  121. }
  122.  
  123. function join($Separator=$ofs,$inputObject) {
  124.    begin{ if($inputObject){ [string]::Join($Separator,$inputObject) } else { $array =@() }}
  125.    process{ if($_){ $array += $_ } }
  126.    end{ if($array.Length) { [string]::Join($Separator,$array) } }
  127. }
  128.  
  129. function Out-HtmlPara {
  130.    process{if($_){"<p>$($_ | out-string -width ([int]::MaxValue) | split "\s*`n" | encode | trim | join "</p>`n<p>")</p>"}}
  131. }

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