Created
May 9, 2017 12:09
-
-
Save Stephanevg/f6409e7ab679872d69059943e5316c84 to your computer and use it in GitHub Desktop.
Converts a PsScribo Document to Markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function ConvertDocumentTo-Markdown { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[System.Object] $Document, | |
$Path = 'C:\Temp\export.md' | |
) | |
Function ConvertTableTo-Markdown { | |
<# | |
.Synopsis | |
Converts a PowerShell object to a Markdown table. | |
.Description | |
The ConvertTo-Markdown function converts a Powershell Object to a Markdown formatted table | |
.EXAMPLE | |
Get-Process | Where-Object {$_.mainWindowTitle} | Select-Object ID, Name, Path, Company | ConvertTo-Markdown | |
This command gets all the processes that have a main window title, and it displays them in a Markdown table format with the process ID, Name, Path and Company. | |
.EXAMPLE | |
ConvertTo-Markdown (Get-Date) | |
This command converts a date object to Markdown table format | |
.EXAMPLE | |
Get-Alias | Select Name, DisplayName | ConvertTo-Markdown | |
This command displays the name and displayname of all the aliases for the current session in Markdown table format | |
#> | |
[CmdletBinding()] | |
[OutputType([string])] | |
Param ( | |
[Parameter( | |
Mandatory = $true, | |
Position = 0, | |
ValueFromPipeline = $true | |
)] | |
[PSObject[]]$InputObject | |
) | |
Begin { | |
$items = @() | |
$columns = @{} | |
} | |
Process { | |
ForEach($item in $InputObject) { | |
$items += $item | |
$item.PSObject.Properties | %{ | |
if($_.Value -ne $null){ | |
if(-not $columns.ContainsKey($_.Name) -or $columns[$_.Name] -lt $_.Value.ToString().Length) { | |
$columns[$_.Name] = $_.Value.ToString().Length | |
} | |
} | |
} | |
} | |
} | |
End { | |
ForEach($key in $($columns.Keys)) { | |
$columns[$key] = [Math]::Max($columns[$key], $key.Length) | |
} | |
$header = @() | |
ForEach($key in $columns.Keys) { | |
$header += ('{0,-' + $columns[$key] + '}') -f $key | |
} | |
$header -join ' | ' | |
$separator = @() | |
ForEach($key in $columns.Keys) { | |
$separator += '-' * $columns[$key] | |
} | |
$separator -join ' | ' | |
ForEach($item in $items) { | |
$values = @() | |
ForEach($key in $columns.Keys) { | |
$values += ('{0,-' + $columns[$key] + '}') -f $item.($key) | |
} | |
$values -join ' | ' | |
} | |
} | |
} | |
foreach ($Section in $Document.Sections){ | |
write-verbose "working on: $($section.id)" | |
write-verbose "working on: $($section.Type)" | |
if ($Section.Type -eq 'PScribo.Paragraph'){ | |
switch ($Section.Style){ | |
"Header1" {$MarkdownContent += "# $($Section.id) `r`n";Break} | |
"Header2" {$MarkdownContent += "## $($Section.id) `r`n";Break} | |
"Header3" {$MarkdownContent += "### $($Section.id) `r`n";Break} | |
"Header4" {$MarkdownContent += "#### $($Section.id) `r`n";Break} | |
"Header5" {$MarkdownContent += "##### $($Section.id) `r`n";Break} | |
"Header6" {$MarkdownContent += "###### $($Section.id) `r`n";Break} | |
Default {$MarkdownContent += "$($Section.id) `r`n" ;Break} | |
} | |
}elseif($Section.Type -eq 'PScribo.Table'){ | |
write-verbose 'Starting Table Conversion' | |
$MarkdownContent += (ConvertTableTo-Markdown -InputObject $Section.rows) + "`r `n" | |
#$MarkdownContent += "`r`n" | |
}elseif($Section.Type -eq 'PScribo.Section'){ | |
ConvertToMarkDown -Document $Section | |
} | |
} | |
return $MarkdownContent | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment