Invoke-Pandoc
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment