Skip to content

Instantly share code, notes, and snippets.

@squadette
Created October 9, 2015 09:17
Show Gist options
  • Save squadette/d478ad009eae98a50c3b to your computer and use it in GitHub Desktop.
Save squadette/d478ad009eae98a50c3b to your computer and use it in GitHub Desktop.
sub fancy_decode {
my $post_title = shift;
$post_title = decode("utf-8", $post_title);
# Devel::Peek::Dump($post_title);
$post_title = encode("iso-8859-1", $post_title, Encode::FB_PERLQQ);
$post_title =~ s/\320\\x\{([0-9a-f]+)\}/!!!320-$1!!!/g;
$post_title =~ s/\321\\x\{([0-9a-f]+)\}/!!!321-$1!!!/g;
$post_title = decode("utf-8", $post_title) || "Untitled";
$post_title =~ s/!!!([0-9a-f-]+)!!!/_tmpconvert($1)/ge;
$post_title =~ s/\x{fffd}\\x\{20ac\}\\x\{0153\}/``/g;
$post_title =~ s/\x{fffd}\\x\{20ac\}\x{fffd}/''/g;
$post_title =~ s/\x{fffd}\\x\{201e\}\x{fffd}/\x{a9}/g;
$post_title =~ s/\x{fffd}\\x\{20ac\}\\x\{201[cd]\}/—/g;
$post_title =~ s/\x{fffd}\\x\{20ac\}\\x\{2122\}/'/g;
$post_title =~ s/\x{fffd}\\x\{0178\}/\x{00DF}/g;
return $post_title;
}
sub _tmpconvert {
my $numbers = shift;
if ($numbers eq "320-2018") {
return "Б";
} elsif ($numbers eq "321-2018") {
return "ё";
}
$numbers =~ s/^\d+-//;
my %cnv = (
"201a" => "т",
"201d" => "Д",
"0152" => "ь",
"201e" => "ф",
"20ac" => "р",
"02dc" => "И",
"0192" => "у",
"2026" => "х",
"2039" => "ы",
"02c6" => "ш",
"2020" => "ц",
"2021" => "ч",
"0178" => "П",
"017d" => "ю",
"017e" => "О",
"2019" => "В",
"201c" => "Г",
"0153" => "М",
"203a" => "Л",
"0161" => "К",
"2018" => "Б",
"2030" => "щ",
"2014" => "З",
"2022" => "Е",
"0160" => "ъ",
"2013" => "Ж",
);
return $cnv{$numbers} || $numbers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment