What is pandoc?
An application is used to convert between various document types. It allows for example to convert from markdown to .docx
and vice-versa.
A list of all formats can be seen on the home page of pandoc.
Why PowerShell wrapper?
I wanted to use Pandoc through Docker.
Why?
- to use the latest version possible (or use a specific one)
- to have not installed on the system
Using it though Docker has some limitations. Examples from Docker Hub use the current working folder.
docker run --rm \
--volume "$(pwd):/data" \
pandoc/core README.md -o outfile.pdf
Full source code
This file contains 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
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string] | |
$Path, | |
[Parameter(Mandatory = $true)] | |
[string] | |
$Output, | |
[Parameter()] | |
$PandocArgs, | |
[switch] | |
$Force | |
) | |
if (!(Test-Path $Path)) { | |
Write-Error -Message "Cannot find path '$($Path)' because it does not exist." | |
exit 1 | |
} | |
# normalizing output path | |
$isRooted = [System.IO.Path]::IsPathRooted($Output) | |
if (!$isRooted) { | |
$Output = Join-Path (Resolve-Path .) $Output | |
} | |
if ((!$Force.IsPresent) -and (Test-Path($Output))) { | |
Write-Error "The file '$($Output)' already exists." | |
exit 1 | |
} | |
function Enter-NewTempFolder { | |
$tempFolder = (New-Guid).Guid | |
Push-Location $env:temp | |
New-Item -Path $tempFolder -ItemType Directory | Out-Null | |
$fullTempPath = (Resolve-Path $tempFolder).Path | |
Write-Verbose "Entering: $($fullTempPath)" | |
Push-Location $fullTempPath | |
return $fullTempPath | |
} | |
function Exit-NewTempFolder { | |
param ( | |
[Parameter()] | |
[string] | |
$Path | |
) | |
if (Test-Path $Path) { | |
Pop-Location # temporary folder inside of $env:temp | |
Write-Verbose "Deleting: $($Path)" | |
Remove-Item -Path $Path -Recurse | |
Pop-Location # $env:temp | |
} | |
} | |
$tempFolder = Enter-NewTempFolder | |
try { | |
Write-Verbose "Copying: source to working directory" | |
Copy-Item -Path $Path -Destination $tempFolder | |
$fileName = Split-Path -Path $Path -Leaf | |
$outputFileName = Split-Path -Path $Output -Leaf | |
Write-Verbose "Running: pandoc on docker" | |
if ($PandocArgs.GetType() -eq [string]) { | |
Write-Verbose "String Vesion" | |
docker run ` | |
--rm ` | |
-v "$($tempFolder):/data" ` | |
pandoc/latex $PandocArgs.Split(' ') -o $outputFileName $fileName | |
} | |
else { | |
Write-Verbose "Defult Vesion" | |
docker run ` | |
--rm ` | |
-v "$($tempFolder):/data" ` | |
pandoc/latex @PandocArgs $fileName -o $outputFileName | |
} | |
Write-Verbose "Command ran: pandoc $fileName -o $outputFileName $PandocArgs" | |
if ($LASTEXITCODE -ne 0) { | |
Write-Error -Message "Docker: failed returning $($LASTEXITCODE) as exit code" | |
Exit-NewTempFolder -Path $tempFolder | |
exit 1 | |
} | |
Write-Verbose "Copying: output to designated destination" | |
Copy-Item -Path $outputFileName -Destination $Output | |
} | |
finally { | |
Exit-NewTempFolder -Path $tempFolder | |
} |