From e74f670e40c8fd8800fdf2f0ad72cd2137ab3bd0 Mon Sep 17 00:00:00 2001 From: foefl Date: Wed, 12 Aug 2026 15:11:02 +0200 Subject: [PATCH] =?UTF-8?q?generisches=20Skript=20f=C3=BCr=20die=20Konvert?= =?UTF-8?q?ierung=20von=20Markdown=20zu=20DOCX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convert_markdown_to_word.ps1 | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 convert_markdown_to_word.ps1 diff --git a/convert_markdown_to_word.ps1 b/convert_markdown_to_word.ps1 new file mode 100644 index 0000000..ece1688 --- /dev/null +++ b/convert_markdown_to_word.ps1 @@ -0,0 +1,74 @@ +param( + [Parameter(Mandatory = $false, Position = 0)] + [string]$RelativePath = ".", + + [Parameter(Mandatory = $false)] + [switch]$Recurse +) + +$scriptDir = $PSScriptRoot + +$refDoc = Join-Path $scriptDir 'referenz.docx' +$luaFilter = Join-Path $scriptDir 'filter.lua' + +$targetDir = Join-Path $scriptDir $RelativePath + +if (-not (Test-Path $targetDir)) { + Write-Error "Der Zielordner '$targetDir' existiert nicht." + exit 1 +} + +$mdFiles = Get-ChildItem -Path $targetDir -Filter '*.md' -Recurse:$Recurse + +if ($mdFiles.Count -eq 0) { + Write-Host "Keine .md-Dateien in '$targetDir' gefunden." -ForegroundColor Yellow + exit 0 +} + +Write-Host "Verarbeite Ordner: $targetDir (Rekursiv: $Recurse)" -ForegroundColor Yellow + +# --- 1. DATEIEN AUFLISTEN --- +Write-Host "`nFolgende $($mdFiles.Count) Datei(en) werden verarbeitet:" -ForegroundColor Yellow +foreach ($file in $mdFiles) { + # Zeigt den relativen Pfad ab dem Zielordner für bessere Lesbarkeit + $relativePath = Resolve-Path -Path $file.FullName -RelativeBasePath $targetDir + Write-Host " - $relativePath" -ForegroundColor Gray +} +Write-Host "" + +# --- 2. NUTZERABFRAGE --- +$response = Read-Host "Möchtest du diese Dateien konvertieren? Bereits vorhandene .docx-Dateien werden überschrieben (J/N)" + +# Prüft, ob der Nutzer mit 'j' oder 'ja' geantwortet hat (case-insensitive) +if ($response -notmatch '^[jJ](a)?$') { + Write-Host "Vorgang vom Benutzer abgebrochen." -ForegroundColor Red + exit 0 +} + +Write-Host "`nStarte Konvertierung..." -ForegroundColor Green + +$pandocArgs = @() + +if (Test-Path $refDoc) { + $pandocArgs += "--reference-doc=$refDoc" +} else { + Write-Host "Hinweis: 'referenz.docx' wurde im Skriptordner nicht gefunden und wird übersprungen." -ForegroundColor DarkGray +} + +if (Test-Path $luaFilter) { + $pandocArgs += "--lua-filter=$luaFilter" +} else { + Write-Host "Hinweis: 'nohr.lua' wurde im Skriptordner nicht gefunden und wird übersprungen." -ForegroundColor DarkGray +} + +# --- 3. KONVERTIERUNGSDURCHLAUF --- +foreach ($file in $mdFiles) { + Write-Host "Konvertiere $($file.Name) ($($file.DirectoryName))..." -ForegroundColor Cyan + + $destDocx = Join-Path $file.DirectoryName "$($file.BaseName).docx" + + # Pandoc-Befehl ausführen (Splatting über das Array $pandocArgs) + pandoc $file.FullName -o $destDocx @pandocArgs +} + +Write-Host "Fertig! Alle Markdown-Dateien wurden konvertiert." -ForegroundColor Green \ No newline at end of file