Skip to content

Instantly share code, notes, and snippets.

@kereis
Created May 12, 2021 12:35
Show Gist options
  • Save kereis/e514949f6fe221fa300ba140a77d5885 to your computer and use it in GitHub Desktop.
Save kereis/e514949f6fe221fa300ba140a77d5885 to your computer and use it in GitHub Desktop.
Replace Unix paths in Synctex (Latex) file with Windows paths
<#
.SYNOPSIS
Replace Unix paths in Synctex (Latex) file with Windows paths
.DESCRIPTION
Replace Unix paths in Synctex (Latex) file with Windows paths.
This script un-gzips the Synctex.gz file, replaces Unix paths set by Docker image arara-docker
with a Windows path of choice. The paths are defined by parameters.
.PARAMETER SynctexFile
Path to the Synctex file to be manipulated.
.PARAMETER LatexInstallLocation
Path to your LateX installation that will be used to replace the old Unix paths
.INPUTS
None. You cannot pipe objects to Fix-Syntex.ps1
.OUTPUTS
Re-writes existing Synctex file with replaced content.
.EXAMPLE
PS> Fix-Synctex.ps1 -SynctexFile C:\MyLatexSrc\build\document.synctex.gz -LatexInstallLocation "C:\Program Files\MyLatexInstallation"
#>
param (
[string]
$SynctexFile,
[string]
$LatexInstallLocation
)
$currentDir = Get-Location
Write-Host "My current working directory is " -ForegroundColor Green -NoNewline
Write-Host $currentDir -ForegroundColor Yellow
if (-not $SynctexFile) {
Write-Error -Message "Please specify -SynctexFile" -ErrorAction Stop
}
if (-not $LatexInstallLocation) {
Write-Error -Message "Please specify -LatexInstallLocation" -ErrorAction Stop
}
if (-not (Test-Path -LiteralPath $SynctexFile)) {
Write-Error -Message "SynctexFile not found" -ErrorAction Stop
}
if (-not (Test-Path -LiteralPath $LatexInstallLocation)) {
Write-Error -Message "LatexInstallLocation not found" -ErrorAction Stop
}
filter replace-slash {$_ -replace "\\", "/"}
Function Gunzip-File([ValidateScript({Test-Path $_})][string]$File){
$srcFile = Get-Item -Path $File
$newFileName = Join-Path -Path $srcFile.DirectoryName -ChildPath $srcFile.BaseName
try
{
$inputStream = New-Object System.IO.FileStream($srcFile.FullName, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read))
$outputStream = New-Object System.IO.FileStream($newFileName, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None))
$gzip = New-Object System.IO.Compression.GZipStream($inputStream, [System.IO.Compression.CompressionMode]::Decompress)
$gzip.CopyTo($outputStream)
}
catch
{
Write-Error -Message "$_" -ErrorAction Stop
}
finally
{
$gzip.Dispose()
$inputStream.Dispose()
$outputStream.Dispose()
}
}
Function Gzip-File([ValidateScript({Test-Path $_})][string]$File){
$srcFile = Get-Item -Path $File
$newFileName = "$($srcFile.FullName).gz"
try
{
$inputStream = New-Object System.IO.FileStream($srcFile.FullName, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read))
$outputStream = New-Object System.IO.FileStream($newFileName, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None))
$gzip = New-Object System.IO.Compression.GZipStream($outputStream, [System.IO.Compression.CompressionMode]::Compress)
$inputStream.CopyTo($gzip)
}
catch
{
Write-Error -Message "$_" -ErrorAction Stop
}
finally
{
$gzip.Dispose()
$inputStream.Dispose()
$outputStream.Dispose()
}
}
$decompressedFilePath = $SynctexFile.Substring(0, $SynctexFile.Length - 3)
Write-Host "Decompressed file path should be " -ForegroundColor Green -NoNewline
Write-Host $decompressedFilePath -ForegroundColor Yellow
Write-Host
Write-Host "Decompressing " -ForegroundColor Cyan -NoNewline
Write-Host $SynctexFile -ForegroundColor Yellow
Write-Host
Gunzip-File $SynctexFile
try {
$replacementPath = (((Get-Item -Path $decompressedFilePath).Directory.ToString() | replace-slash) + "/")
$replacementPathLatex = (((Get-Item -Path $LatexInstallLocation).ToString() | replace-slash) + "/")
$subfolderNames = @("kapitel", "vorlagen")
Write-Host "Trying to replace paths in Synctex file..." -ForegroundColor Green
foreach ($subfolder in $subfolderNames) {
Write-Host "> Replacing old Unix path " -ForegroundColor Cyan -NoNewline
Write-Host "/home/texbuilder/build/./$subfolder/" -ForegroundColor Red -NoNewline
Write-Host " with " -ForegroundColor Cyan -NoNewline
Write-Host "$replacementPath$subfolder/" -ForegroundColor Yellow
(Get-Content $decompressedFilePath) -replace "/home/texbuilder/build/./$subfolder/", "$replacementPath$subfolder/" | Set-Content -Encoding utf8 -Path $decompressedFilePath
}
Write-Host "> Replacing old Unix path " -ForegroundColor Cyan -NoNewline
Write-Host "/home/texbuilder/build/./" -ForegroundColor Red -NoNewline
Write-Host " with " -ForegroundColor Cyan -NoNewline
Write-Host $replacementPath -ForegroundColor Yellow
(Get-Content $decompressedFilePath) -replace '/home/texbuilder/build/./', $replacementPath | Set-Content -Encoding utf8 -Path $decompressedFilePath
Write-Host "> Replacing old Unix path " -ForegroundColor Cyan -NoNewline
Write-Host "/usr/share/texlive/texmf-dist/" -ForegroundColor Red -NoNewline
Write-Host " with " -ForegroundColor Cyan -NoNewline
Write-Host $replacementPathLatex -ForegroundColor Yellow
(Get-Content $decompressedFilePath) -replace '/usr/share/texlive/texmf-dist', $replacementPathLatex | Set-Content -Encoding utf8 -Path $decompressedFilePath
} catch {
Write-Error -Message "$_" -ErrorAction Stop
}
Write-Host
Write-Host "Compressing (GZip) " -ForegroundColor Cyan -NoNewline
Write-Host $decompressedFilePath -ForegroundColor Yellow
Gzip-File $decompressedFilePath
Write-Host "> Done!" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment