## ConvertTo-DekiContent (aka Convert Help to Html) #################################################################################################### ## Converts the -Full help output to HTML markup for insertion into web pages. #################################################################################################### ## Usage: ## ## foreach($cmd in (gcm -type cmdlet | ? { $_.PsSnapin -like "Microsoft.PowerShell*" })) { ## ## Get-Help $cmd.Name -full | ConvertTo-DekiContent Cmdlet_Help | ## %{ Set-DekiContent "Cmdlet_Help/$($cmd.PSSnapin)/$($cmd.Name)" $_ } ## } ## #################################################################################################### ## History: ## v2.0 - Refactoring of markup and code by Joel "Jaykul" Bennett to avoid line-wrapping, and 'pre' ## blocks in the code and to format the parameters and examples more like the originals. ## v1.0 - Original version by http://blogs.vmware.com/vipowershell/2007/09/new-htmlhelp.html #################################################################################################### #Import System.Web in order to use HtmlEncode functionality [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null ## Get-HtmlHelp - A Helper function for generating help: ## Usage: Get-HtmlHelp Get-* function Get-HtmlHelp { param([string[]]$commands, [string]$baseUrl) $commands | Get-Command -type Cmdlet -EA "SilentlyContinue" | get-help -Full | ConvertTo-DekiContent $baseUrl } function ConvertTo-DekiContent { param($baseUrl) PROCESS { if($_ -and ($_.PSObject.TypeNames -contains "MamlCommandHelpInfo#FullView")) { $help = $_ # Name isn't needed, since this is going as the body, but ... # $data = "$(encode($help.Name))"; # $data += "

$(encode($help.Name))

" # Synopsis $data += "

Synopsis

$($help.Synopsis | Out-HtmlPara)" # Syntax $data += "

Syntax

$($help.Syntax | Out-HtmlPara)" # Related Commands $data += "

Related Commands

" foreach ($relatedLink in $help.relatedLinks.navigationLink) { if($relatedLink.linkText -ne $null -and $relatedLink.linkText.StartsWith("about") -eq $false) { $uri = "" if( $relatedLink.uri -ne "" ) { $uri = $relatedLink.uri } else{ $uri = "$baseUrl/$((get-command $relatedLink.linkText -EA "SilentlyContinue").PSSnapin.Name)/$($relatedLink.linkText)" } $data += "$(encode($relatedLink.linkText))
" } } # Detailed Description $data += "

Detailed Description

$(encode(&{$help.Description | out-string -width 200000}))" # Parameters $data += "

Parameters

" $help.parameters.parameter | %{ $param = $_ $data += "

-$(encode($param.Name)) [<$(encode($param.type.name))>]

" $data += $param.Description | Out-HtmlPara $data += "" $data += "" $data += "" $data += "" $data += "" $data += "
Required?   $(encode($param.Required))
Position?   $(encode($param.Position))
Default value?   $(encode($param.defaultValue))
Accept pipeline input?   $(encode($param.pipelineInput))
Accept wildcard characters?   $(encode($param.globbing))
" } if($help.inputTypes) { # Input Type $data += "

Input Type

$($help.inputTypes | Out-HtmlPara)" } if($help.returnValues) { # Return Type $data += "

Return Type

$($help.returnValues | Out-HtmlPara)" } # Notes $data += "

Notes

$($help.alertSet | Out-HtmlPara)" # Examples $data += "

Examples

" $help.Examples.example | %{ $example = $_ $data += "

$(encode($example.title.trim(' -')))

" $data += "PS> $(encode($example.code))" $data += "

$($example.remarks | out-string -width ([int]::MaxValue) | Out-HtmlPara)

" } # $data += "" write-output $data } else { Write-Error "Can only process -Full view help output" } }} function encode($str) { begin{ if($str){ $str.split("`n") | encode } } process{ if($_){ [System.Web.HttpUtility]::HtmlEncode($_).Trim() } } } function trim($str) { begin{ if($str){ $str.Trim() } } process{ if($_){ $_.Trim() } } } function split($Separator="`n",$inputObject) { begin{ if($inputObject){ $inputObject | split $Separator } } process{ if($_){ [regex]::Split($_,$Separator) | ? {$_.Length} } } } function join($Separator=$ofs,$inputObject) { begin{ if($inputObject){ [string]::Join($Separator,$inputObject) } else { $array =@() }} process{ if($_){ $array += $_ } } end{ if($array.Length) { [string]::Join($Separator,$array) } } } function Out-HtmlPara { process{if($_){"

$($_ | out-string -width ([int]::MaxValue) | split "\s*`n" | encode | trim | join "

`n

")

"}} }