summaryrefslogtreecommitdiffstats
path: root/www/digest/mailify.pl
blob: 4e0c7d60fd93648d44856d3e5469d490b93c4847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl

my $end++;

my @out;
my $url;

my $urlnum=1;

sub showlinks {
    my ($date)=@_;
    if(scalar(keys %store)) {
        print "\n";
        
        for(sort {$store{$a} <=> $store{$b} } keys %store) {
            my $url=$_;
            
            $url =~ s/^\"(.*)\"/$1/g;
            printf("[%d] = %s\n", $store{$_}, $url);
        }
        undef %store;
        $urlnum=1; # reset to 1
    }
}

sub showitem {
    my @text=@_;

    if(@text) {
        my $c=3;
        my $width=72;
        print " * ";

        my $thelot = join(" ", @text);
        
        for (split(/[ \t\n]/, $thelot)) {
            my $word = $_;

            $word =~ s/[ \t]//g;

            my $len = length($word);
            if(!$len) {
                next; # skip blanks
            }

            if($len + $c + 1> $width) {
                print "\n   ";
                $c = 3;
            }
            elsif($c != 3) {
                print " ";
                $c++;
            }
            print $word;
            $c += $len;
        }
    }
    print "\n"; # end of item

 #   my @words=split(

}

sub date2secs {
    my ($date)=@_;
    my $secs = `date -d "$date" +"%s"`;
    return 0+$secs;
}

while(<STDIN>) {
    my $line = $_;

    if($_ =~ /^Date: (.*)/) {
        my $date=$1;
        my $secs = date2secs($date);
        my $tendaysago = time()-(14*24*60*60);
        showitem(@out);
        undef @out;
        chomp;

        showlinks();

        if($secs < $tendaysago) {
            last;
        }

        print "\n----------------------------------------------------------------------\n$_";
        next;
    }
    elsif($line =~ /^ *(---)(.*)/) {

        showitem(@out);

        @out="";
        $line = $2;
    }

    if($line =~ s/\[URL\](.*)\[URL\]//) {
        $url=$1;

        if(!$store{$url}) {
            $footnote = "[$urlnum]";
            $store{$url} = $urlnum;
            $urlnum++;
        }
        else {
            $footnote = "[".$store{$url}."]";
        }
 #       print STDERR "Set $footnote for $url\n";
    }
    if($line =~ s/\[TEXT\](.*)\[TEXT\]/$1$footnote/) {
 #       print STDERR "Output $footnote (full TEXT)\n";
        undef $text;
    }
    elsif(!$text && ($line =~ s/\[TEXT\](.*)/$1/)) {
 #       print STDERR "Detected start of TEXT\n";
        $text = $1;
    }
    elsif($text && ($line =~ s/(.*)\[TEXT\]/$1$footnote/)) {
 #       print STDERR "Output $footnote (end-TEXT)\n";
        undef $text;
    }

    push @out, $line;
}

if(@out) {
    showitem(@out);
}