49 lines
1.7 KiB
PowerShell
49 lines
1.7 KiB
PowerShell
# Repariert doppel-kodierte UTF-8-Zeichen (Mojibake) in einer Datei.
|
|
# ASCII-only: Alle Sonderzeichen werden programmatisch berechnet,
|
|
# damit das Skript selbst von Encoding-Problemen unberuehrt bleibt.
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$Path
|
|
)
|
|
|
|
# Erzeugt die Mojibake-Variante eines Strings:
|
|
# UTF-8-Bytes werden als Windows-1252 interpretiert (exakt der Fehler).
|
|
function ConvertTo-Mojibake([string]$Text) {
|
|
$bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
|
|
return [System.Text.Encoding]::GetEncoding('windows-1252').GetString($bytes)
|
|
}
|
|
|
|
# Zu korrigierende Zeichen (als Unicode-Codepoints, ASCII-only-Skript)
|
|
$correctChars = @(
|
|
[char]0x00FC # ue (klein)
|
|
[char]0x00F6 # oe (klein)
|
|
[char]0x00E4 # ae (klein)
|
|
[char]0x00C4 # Ae (gross)
|
|
[char]0x00D6 # Oe (gross)
|
|
[char]0x00DC # Ue (gross)
|
|
[char]0x00DF # scharfes S
|
|
[char]0x2013 # en dash
|
|
[char]0x201E # Anfuehrung unten
|
|
[char]0x201C # Anfuehrung oben
|
|
[char]0x2018 # einfaches Anfuehrungszeichen
|
|
[char]0x2019 # einfaches Anfuehrungszeichen (schliessend)
|
|
[char]0x2026 # Ellipse
|
|
[char]0x00B7 # Mittelpunkt
|
|
[char]0x2014 # em dash
|
|
)
|
|
|
|
$content = [System.IO.File]::ReadAllText($Path, [System.Text.Encoding]::UTF8)
|
|
|
|
$replacementCount = 0
|
|
foreach ($correct in $correctChars) {
|
|
$mojibake = ConvertTo-Mojibake ([string]$correct)
|
|
if ($content.Contains($mojibake)) {
|
|
$count = ([regex]::Matches($content, [regex]::Escape($mojibake))).Count
|
|
$content = $content.Replace($mojibake, $correct)
|
|
$replacementCount += $count
|
|
}
|
|
}
|
|
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
|
[System.IO.File]::WriteAllText($Path, $content, $utf8NoBom)
|
|
Write-Output "Korrigiert: $replacementCount Stellen in $Path" |