ljr/livejournal/htdocs/translate/diff.bml

157 lines
4.2 KiB
Plaintext
Raw Permalink Normal View History

2019-02-05 21:49:12 +00:00
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
del {
text-decoration: none;
color: #ff0000;
background-color: #ffd0d0;
}
ins {
text-decoration: none;
color: #002000;
background-color: #20ff20;
}
</style>
</head>
<body>
<?_code
{
use strict;
use vars qw(%GET);
use File::Temp qw/tempfile/;
BML::set_content_type("text/html; charset=utf-8");
my $lang = $GET{'lang'};
my $l = LJ::Lang::get_lang($lang);
return "<b>Invalid language</b>" unless $l;
return "<b>Invalid item</b>" unless $GET{'it'} =~ /^(\d+):(\d+)$/;
my ($dmid, $itid) = ($1, $2);
my @lnids = $l->{'lnid'};
{
my $il = $l;
while ($il && $il->{'parentlnid'}) {
push @lnids, $il->{'parentlnid'};
$il = LJ::Lang::get_lang_id($il->{'parentlnid'});
}
}
my $lnids = join(",", @lnids);
my $dbr = LJ::get_db_reader();
my ($sth, $ret);
my $sth = $dbr->prepare("select * from ml_text where dmid=$dmid and itid=$itid and lnid in ($lnids) ORDER BY txtid");
$sth->execute;
my @tlist;
while (my $t = $sth->fetchrow_hashref) {
next if @tlist && $t->{'text'} eq $tlist[-1]->{'text'};
push @tlist, $t;
}
my $changes = scalar @tlist - 1;
return "<b>No changes</b>" unless $changes;
my $view_change = $GET{'change'} || $changes;
return "bogus change" if $view_change < 1 || $view_change > $changes;
for (1..$changes) {
if ($_ eq $view_change) {
$ret .= "<b>[Change $_]</b>\n";
} else {
$ret .= "<a href='diff.bml?lang=$lang&it=$GET{'it'}&change=$_'>[Change $_]</a>\n";
}
}
$ret .= "<hr>";
my $was = $tlist[$view_change-1]->{'text'};
my $then = $tlist[$view_change]->{'text'};
my ($was_alt, $then_alt) = ($was, $then);
foreach (\$was_alt, \$then_alt) {
$$_ =~ s/\n/*NEWLINE*/g;
$$_ =~ s/\s+/\1\n/g;
$$_ .= "\n";
}
my ($was_file, $then_file, $fh);
my $tries = 0;
($fh, $was_file) = tempfile();
print $fh $was_alt; close $fh;
($fh, $then_file) = tempfile();
print $fh $then_alt; close $fh;
my @words = split(/\n/, $was_alt);
my $diff = `diff -u $was_file $then_file`;
my $pos = 0;
my $mode;
my $setmode = sub {
my $newmode = shift;
return " " if $newmode eq $mode;
my $ret;
$ret .= "</$mode>" if $mode;
$ret .= " ";
$ret .= "<$newmode>" if $newmode;
$mode = $newmode;
return $ret;
};
foreach my $dl (split(/\n/, $diff)) {
next if $dl =~ /^(\+\+\+|\-\-\-)/;
if ($dl =~ /^\@\@ \-(\d+),(\d+)/) {
my $newpos = $1;
$ret .= $setmode->("");
for (my $i=$pos+1; $i<$newpos; $i++) {
my $word = LJ::ehtml($words[$i-1]);
$word =~ s/\*NEWLINE\*/<br>\n/g;
$ret .= "$word ";
$pos++;
}
next;
}
if ($dl =~ /^ /) {
$pos++;
my $word = LJ::ehtml($words[$pos-1]);
$word =~ s/\*NEWLINE\*/<br>\n/g;
$ret .= $setmode->("") . $word;
}
if ($dl =~ /^\-/) {
$pos++;
my $word = LJ::ehtml($words[$pos-1]);
$word =~ s/\*NEWLINE\*/<br>\n/g;
$ret .= $setmode->("del") . $word;
}
if ($dl =~ /^\+(.*)/) {
my $word = LJ::ehtml($1);
$word =~ s/\*NEWLINE\*/<br>\n/g;
$ret .= $setmode->("ins") . $word;
}
}
$ret .= $setmode->("");
while ($pos < @words) {
my $word = LJ::ehtml($words[$pos]);
$word =~ s/\*NEWLINE\*/<br>\n/g;
$ret .= "$word ";
$pos++;
}
$was = LJ::eall($was);
$was =~ s/\n( *)/"<br \/>" . "&nbsp;"x length($1)/eg;
$then = LJ::eall($then);
$then =~ s/\n( *)/"<br \/>" . "&nbsp;"x length($1)/eg;
$ret .= "<p><table width='100%' border='1'><tr valign='top'><td width='50%'><b>Before:</b><br>$was</td>";
$ret .= "<td width='50%'><b>After:</b><br>$then</td></tr></table>";
unlink($was_file, $then_file);
return $ret;
}
_code?>
</body>
</html>