]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
consistently whitespace wrap on <pre> sections
[public-inbox.git] / lib / PublicInbox / View.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::View;
4 use strict;
5 use warnings;
6 use PublicInbox::Hval;
7 use URI::Escape qw/uri_escape_utf8/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11
12 # TODO: make these constants tunable
13 use constant MAX_INLINE_QUOTED => 5;
14 use constant MAX_TRUNC_LEN => 72;
15 use constant PRE_WRAP => '<pre style="white-space:pre-wrap">';
16
17 *ascii_html = *PublicInbox::Hval::ascii_html;
18
19 my $enc_utf8 = find_encoding('UTF-8');
20 my $enc_mime = find_encoding('MIME-Header');
21
22 # public functions:
23 sub msg_html {
24         my ($class, $mime, $full_pfx, $footer) = @_;
25         if (defined $footer) {
26                 $footer = "\n" . $footer;
27         } else {
28                 $footer = '';
29         }
30         headers_to_html_header($mime, $full_pfx) .
31                 multipart_text_as_html($mime, $full_pfx) .
32                 '</pre><hr />' . PRE_WRAP .
33                 html_footer($mime) . $footer .
34                 '</pre></body></html>';
35 }
36
37 sub feed_entry {
38         my ($class, $mime, $full_pfx) = @_;
39
40         PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
41 }
42
43
44 # only private functions below.
45
46 sub enc_for {
47         my ($ct) = @_;
48         defined $ct or return $enc_utf8;
49         my $ct_parsed = parse_content_type($ct);
50         if ($ct_parsed) {
51                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
52                         my $enc = find_encoding($charset);
53                         return $enc if $enc;
54                 }
55         }
56         $enc_utf8;
57 }
58
59 sub multipart_text_as_html {
60         my ($mime, $full_pfx) = @_;
61         my $rv = "";
62         my $part_nr = 0;
63         my $enc_msg = enc_for($mime->header("Content-Type"));
64
65         # scan through all parts, looking for displayable text
66         $mime->walk_parts(sub {
67                 my ($part) = @_;
68                 return if $part->subparts; # walk_parts already recurses
69                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
70
71                 if ($part_nr > 0) {
72                         my $fn = $part->filename;
73                         defined($fn) or $fn = "part #" . ($part_nr + 1);
74                         $rv .= add_filename_line($enc->decode($fn));
75                 }
76
77                 if (defined $full_pfx) {
78                         $rv .= add_text_body_short($enc, $part, $part_nr,
79                                                 $full_pfx);
80                 } else {
81                         $rv .= add_text_body_full($enc, $part, $part_nr);
82                 }
83                 $rv .= "\n" unless $rv =~ /\n\z/s;
84                 ++$part_nr;
85         });
86         $rv;
87 }
88
89 sub add_filename_line {
90         my ($fn) = @_;
91         my $len = 72;
92         my $pad = "-";
93
94         $len -= length($fn);
95         $pad x= ($len/2) if ($len > 0);
96         "$pad " . ascii_html($fn) . " $pad\n";
97 }
98
99 sub add_text_body_short {
100         my ($enc, $part, $part_nr, $full_pfx) = @_;
101         my $n = 0;
102         my $s = ascii_html($enc->decode($part->body));
103         # TODO: fold the "so-and-so wrote:" attribute line here, too:
104         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
105                 my $cur = $1;
106                 my @lines = split(/\n/, $cur);
107                 if (@lines > MAX_INLINE_QUOTED) {
108                         # show a short snippet of quoted text
109                         $cur = join(' ', @lines);
110                         $cur =~ s/^&gt;\s*//;
111
112                         my @sum = split(/\s+/, $cur);
113                         $cur = '';
114                         do {
115                                 my $tmp = shift(@sum);
116                                 my $len = length($tmp) + length($cur);
117                                 if ($len > MAX_TRUNC_LEN) {
118                                         @sum = ();
119                                 } else {
120                                         $cur .= $tmp . ' ';
121                                 }
122                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
123                         $cur =~ s/ \z/ .../;
124                         "&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
125                                 "\">$cur<\/a>&gt;\n";
126                 } else {
127                         $cur;
128                 }
129         !emg;
130         $s;
131 }
132
133 sub add_text_body_full {
134         my ($enc, $part, $part_nr) = @_;
135         my $n = 0;
136         my $s = ascii_html($enc->decode($part->body));
137         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
138                 my $cur = $1;
139                 my @lines = split(/\n/, $cur);
140                 if (@lines > MAX_INLINE_QUOTED) {
141                         "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
142                 } else {
143                         $cur;
144                 }
145         !emg;
146         $s;
147 }
148
149 sub headers_to_html_header {
150         my ($mime, $full_pfx) = @_;
151
152         my $rv = "";
153         my @title;
154         foreach my $h (qw(From To Cc Subject Date)) {
155                 my $v = $mime->header($h);
156                 defined($v) && length($v) or next;
157                 $v = PublicInbox::Hval->new_oneline($v);
158                 $rv .= "$h: " . $v->as_html . "\n";
159
160                 if ($h eq 'From') {
161                         my @from = Email::Address->parse($v->raw);
162                         $v = $from[0]->name;
163                         unless (defined($v) && length($v)) {
164                                 $v = '<' . $from[0]->address . '>';
165                         }
166                         $title[1] = ascii_html($v);
167                 } elsif ($h eq 'Subject') {
168                         $title[0] = $v->as_html;
169                 }
170         }
171
172         my $header_obj = $mime->header_obj;
173         my $mid = $header_obj->header_raw('Message-ID');
174         if (defined $mid) {
175                 $mid = PublicInbox::Hval->new_msgid($mid);
176                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
177                 my $href = $mid->as_href;
178                 $href = "../m/$href" unless $full_pfx;
179                 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
180         }
181
182         my $irp = $header_obj->header_raw('In-Reply-To');
183         if (defined $irp) {
184                 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
185                 my $html = $v->as_html;
186                 my $href = $v->as_href;
187                 $rv .= "In-Reply-To: &lt;";
188                 $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
189         }
190
191         my $refs = $header_obj->header_raw('References');
192         if ($refs) {
193                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
194                 my @refs = ($refs =~ /<([^>]+)>/g);
195                 if (@refs) {
196                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
197                 }
198         }
199
200         $rv .= "\n";
201
202         ("<html><head><title>".  join(' - ', @title) .
203          '</title></head><body>' . PRE_WRAP . $rv);
204 }
205
206 sub html_footer {
207         my ($mime) = @_;
208         my %cc; # everyone else
209         my $to; # this is the From address
210
211         foreach my $h (qw(From To Cc)) {
212                 my $v = $mime->header($h);
213                 defined($v) && length($v) or next;
214                 my @addrs = Email::Address->parse($v);
215                 foreach my $recip (@addrs) {
216                         my $address = $recip->address;
217                         my $dst = lc($address);
218                         $cc{$dst} ||= $address;
219                         $to ||= $dst;
220                 }
221         }
222         Email::Address->purge_cache;
223
224         my $subj = $mime->header('Subject') || '';
225         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
226         my $irp = uri_escape_utf8(
227                         $mime->header_obj->header_raw('Message-ID') || '');
228         delete $cc{$to};
229         $to = uri_escape_utf8($to);
230         $subj = uri_escape_utf8($subj);
231
232         my $cc = uri_escape_utf8(join(',', values %cc));
233         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
234
235         '<a href="' . ascii_html($href) . '">reply</a>';
236 }
237
238 sub linkify_refs {
239         join(' ', map {
240                 my $v = PublicInbox::Hval->new_msgid($_);
241                 my $html = $v->as_html;
242                 my $href = $v->as_href;
243                 "&lt;<a href=\"$href.html\">$html</a>&gt;";
244         } @_);
245 }
246
247 1;