Public/Convert-PCMarkdownToWord.ps1
|
function Convert-PCMarkdownToWord { <# .SYNOPSIS Renders a markdown file, or the files an outline names, into a themed Word document. .DESCRIPTION Walks the markdown through the PowerCraft.Word.Core kernel: headings, paragraphs, emphasis, lists, tables, code, quotes, links and images map onto the theme's named styles. Images on their own line become numbered Word captions. Table of contents and list of figures are real fields, placed at <!-- INSERT_TOC_HERE --> and <!-- INSERT_LOF_HERE --> markers when present, otherwise per the placement parameters. An optional title-page document is merged ahead of the body with [Token] substitution. Writes a build report beside the output when -ReportPath is given. With -Outline the document is made of several markdown files, in the order an outline.yaml gives them. Each file starts on a new page and its # is a Heading 1. A part of the outline is a page of its own with the part's title. Images are found beside the file that names them, a link from one file to another becomes a link inside the document, and figures are numbered through all files. Cross-platform. No Word, no COM, no Pandoc. .PARAMETER Path The markdown file. .PARAMETER Outline An outline.yaml: title, subtitle, author, tokens, and items. An item is a file, or a part with items of its own. Paths are relative to the outline. .PARAMETER OutputPath The .docx to write. Defaults to the name of the markdown file or of the outline, with a .docx extension. .PARAMETER Theme A .docx whose styles the renderer uses. Omit for the neutral default theme. See New-PCWordTheme. .PARAMETER TitlePage A .docx merged ahead of the body, with [Token] placeholders substituted from -Tokens. [Title] defaults to the first level-one heading and [Date] to today. .PARAMETER NoTitlePage With -Outline and no -TitlePage, the document starts with a page that says the outline's title, subtitle and author. This switch leaves that page out. .PARAMETER Tokens Hashtable of token values for the title page, for example @{ SUBTITLE = 'Draft for review' }. .PARAMETER Toc Where the table of contents goes when no marker is present: AfterTitle (default), BeforeContent, Marker, None. .PARAMETER ListOfFigures Where the list of figures goes when no marker is present: AfterToc (default), AfterTitle, BeforeContent, Marker, None. .PARAMETER HeadingOffset Added to heading levels. For one file the default is -1, which maps # to Title and ## to Heading 1, as the legacy pipeline did. For an outline the default is 0: the # of each file is a Heading 1. .PARAMETER Deterministic Fix document timestamps so repeated builds of the same input are structurally identical. .PARAMETER Force Overwrite an existing output file. .PARAMETER ReportPath Write the build report as JSON to this path. .EXAMPLE Convert-PCMarkdownToWord .\update.md -Theme .\Context\style-reference.docx -TitlePage .\Context\title-page.docx -Tokens @{ SUBTITLE = 'Status and milestones' } -Force .EXAMPLE Convert-PCMarkdownToWord -Outline .\book\outline.yaml -OutputPath .\book.docx -Force #> [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'File')] [OutputType([pscustomobject])] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'File')] [Alias('FullName', 'MarkdownFile')] [string]$Path, [Parameter(Mandatory, ParameterSetName = 'Outline')] [string]$Outline, [Parameter(Position = 1)] [string]$OutputPath, [string]$Theme, [string]$TitlePage, [switch]$NoTitlePage, [hashtable]$Tokens, [string]$Title, [ValidateSet('AfterTitle', 'BeforeContent', 'Marker', 'None')] [string]$Toc = 'AfterTitle', [ValidateSet('AfterToc', 'AfterTitle', 'BeforeContent', 'Marker', 'None')] [string]$ListOfFigures = 'AfterToc', [string]$TocHeading = 'Table of Contents', [string]$ListOfFiguresHeading = 'List of Figures', [Nullable[int]]$HeadingOffset, [switch]$KeepFirstHeading, [switch]$NoUpdateFieldsOnOpen, [switch]$Deterministic, [switch]$Force, [string]$ReportPath ) process { $request = [PowerCraft.Word.Core.DocumentBuildRequest]::new() if ($PSCmdlet.ParameterSetName -eq 'Outline') { $source = (Resolve-Path -LiteralPath $Outline -ErrorAction Stop).Path $request.OutlinePath = $source } else { $source = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path $request.MarkdownPath = $source } if ($OutputPath) { $request.OutputPath = $OutputPath } if ($Theme) { $request.ThemePath = (Resolve-Path -LiteralPath $Theme -ErrorAction Stop).Path } if ($TitlePage) { $request.TitlePagePath = (Resolve-Path -LiteralPath $TitlePage -ErrorAction Stop).Path } if ($Title) { $request.Title = $Title } if ($Tokens) { foreach ($key in $Tokens.Keys) { $request.Tokens[[string]$key] = [string]$Tokens[$key] } } $request.Toc = [PowerCraft.Word.Core.FieldPlacement]$Toc $request.ListOfFigures = [PowerCraft.Word.Core.FieldPlacement]$ListOfFigures $request.TocHeadingText = $TocHeading $request.LofHeadingText = $ListOfFiguresHeading if ($null -ne $HeadingOffset) { $request.HeadingOffset = $HeadingOffset } if ($KeepFirstHeading) { $request.RemoveFirstHeading = $false } if ($NoTitlePage) { $request.OutlineTitlePage = $false } $request.UpdateFieldsOnOpen = -not $NoUpdateFieldsOnOpen $request.Deterministic = [bool]$Deterministic $request.Overwrite = [bool]$Force $target = if ($OutputPath) { $OutputPath } else { [IO.Path]::ChangeExtension($source, '.docx') } if (-not $PSCmdlet.ShouldProcess($target, 'Render markdown to Word')) { return } try { $result = [PowerCraft.Word.Core.DocumentBuilder]::Build($request) } catch { # What the kernel throws ends the command, with the kernel's own exception and message. Left alone, # PowerShell ends only the statement, and the lines below would report on a document that was not made. $failure = $_.Exception.GetBaseException() $PSCmdlet.ThrowTerminatingError([System.Management.Automation.ErrorRecord]::new($failure, 'DocumentBuildFailed', [System.Management.Automation.ErrorCategory]::InvalidOperation, $source)) } $report = $result.Report foreach ($warning in $report.Warnings) { Write-Warning $warning } if ($ReportPath) { [IO.File]::WriteAllText((Join-Path (Get-Location) $ReportPath | ForEach-Object { if ([IO.Path]::IsPathRooted($ReportPath)) { $ReportPath } else { $_ } }), $report.ToJson()) } [pscustomobject]@{ OutputPath = $result.OutputPath Title = $report.Title Theme = $report.Theme Files = @($report.Files).Count Parts = $report.Parts Headings = $report.Headings Images = $report.Images Captions = $report.Captions Tables = $report.Tables Fields = @($report.Fields) TokensUnresolved = @($report.TokensUnresolved) StylesMissing = @($report.StylesMissing) ValidationErrors = $report.ValidationErrors Warnings = @($report.Warnings) ReportPath = $ReportPath } } } |