shlomi-fish-homepage / t2 / humour / fortunes / proc-nyh-sigs-xml.pl

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use List::Util qw(min);

my $count = 1;
my $enc = 0;
my $id_str = "PLOC-IDENT";
my $title_str = "QUACKPROLOKOG==UNKNOWN-TITLE";

my @lines;
while (<>)
{
    if (m{\A *<fortune id=} .. m{\A *</raw>})
    {
        push @lines, $_;
    }
    else
    {
        if (@lines)
        {
            my $text = join("", @lines);
            $text =~ m{<!\[CDATA\[([^\n]*)$}ms;
            my $sig = $1;
            my @words = split(/\s+/, $sig);
            my @fmt_words =
                (
                    map { lc($_) }
                    grep { length($_) }
                    map
                    { my $s = $_; $s =~ s{\W}{}g; $s; }
                    @words
                );

            my $id = "nyh-sig--" . join("-" , @fmt_words[0 .. min(2,$#fmt_words)]);
            $text =~ s{(<fortune id=")[^"]+(">)}{$1$id$2};

            my $sig_esc = CGI::escapeHTML($sig);
            $text =~ s{(<title>)[^<]+(</title>)}{$1$sig_esc$2};
            print $text;
            @lines = ();
        }
        print $_;
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.