Szerkesztő:GumiBot/code61

A(z) 61. kódú hibát javító kódrészletSzerkesztés

sub fix_61 {	# Reference with punctuation
    my ($error_code, $title_str, $text_str) = @_;

    my $excluded = search_tags($text_str);
    my $references = search_tags($text_str, [
            ['<ref>|<ref[^>]*[^/]>', '</ref>'],
            ['<ref [^>]+/\s*>', ''],
        ], $excluded);

    my @refs;
    while ($references) {
        (my $ref, $references) = $references->first;
        push(@refs, {
            min     => $ref->min,
            max     => $ref->max,
        });
    }
    push(@refs, { min => length($text_str) } );     # guard

    my @parsed;
    my $pos = 0;
    foreach my $r (@refs) {
        my $min = $r->{min};
        if ($pos < $min) {
            my $spacelen = 0;
            substr($text_str, $pos, $min-$pos) =~
                m/^((?: *[.,:;!?]+)?)(.*?)((?:[.,:;!?]+ *)?)$/s;
            my ($punct1, $txt, $punct2) = ($1, $2, $3);

            if ($punct1 =~ /^( +)/) {
                $spacelen = length($1);
                substr($punct1,0,$spacelen) = '';
                $pos += $spacelen;
                $spacelen = 0;
            }
            foreach (split(//,$punct1)) {
                push(@parsed, {
                    type    => 'punct',
                    'pos'   => $pos++,
                    'len'   => 1,
                });
            }

            push(@parsed, {
                type    => 'text',
                'pos'   => $pos,
                'len'   => length($txt),
            });
            $pos += length($txt);

            if ($punct2 =~ /( +)$/) {
                $spacelen = length($1);
                substr($punct2,-$spacelen) = '';
            }
            foreach (split(//,$punct2)) {
                push(@parsed, {
                    type    => 'punct',
                    'pos'   => $pos++,
                    'len'   => 1,
                });
            }
            $pos += $spacelen;
        }
        last unless exists $r->{max};
        push(@parsed, {
            type    => 'ref',
            'pos'   => $min,
            'len'   => $r->{max}-$pos+1,
        });
        $pos = $r->{max}+1;
    }

    my $count = 0;
    while (1) {
        my $changed = 0;
        for (my $i=$#parsed; $i>0; $i--) {
            next unless $parsed[$i-1]->{type} eq 'ref' and
                        $parsed[$i]->{type} eq 'punct';
            splice(@parsed, $i-1, 2, ($parsed[$i], $parsed[$i-1]));
            $changed++;
            $count++;
        }
        last unless $changed;
    }
    while (1) {
        my $changed = 0;
        for (my $i=$#parsed; $i>1; $i--) {
            next unless $parsed[$i-2]->{type} eq 'punct' and
                        $parsed[$i-1]->{type} eq 'punct' and
                        $parsed[$i]->{type} eq 'ref';
            splice(@parsed, $i-2, 2, ($parsed[$i-1]));
            $changed++;
        }
        last unless $changed;
    }
    for (my $i=$0; $i<$#parsed; $i++) {
        next unless $parsed[$i]->{type} eq 'text' and
                    ($parsed[$i+1]->{type} eq 'punct' or
                        $parsed[$i+1]->{type} eq 'ref');
        my $txt = substr($text_str, $parsed[$i]->{pos},  $parsed[$i]->{len});
        next unless $txt =~ /(\s+)$/s;
        $parsed[$i]->{len} -= length($1);
    }

    my $new_str;
    foreach (@parsed) {
        $new_str .= substr($text_str, $_->{pos},  $_->{len});
    }
    $text_str = $new_str;

    $text_str =~ s/\.('{2,5}\.)/\1/g;       # a common idiom

    $count or return ('Nincs <ref> utani irasjel');
    my $summary_str = $latin2->decode(
        "Bot: $count helyen az írásjel átmozgatva a <ref> elé. (Hibakód: $error_code)"
    );
    return ($summary_str, $count, $text_str);
}