From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] view: wrap To: and Cc: headers in HTML display
Date: Mon, 23 Apr 2018 04:16:53 +0000 [thread overview]
Message-ID: <20180423041654.4152-3-e@80x24.org> (raw)
In-Reply-To: <20180423041654.4152-1-e@80x24.org>
It is common to have large amounts of addresses Cc:-ed in large
mailing lists like LKML. Make them more readable by wrapping
after addresses. Unfortunately, line breaks inserted by the
MUA get lost when using the public Email::MIME API.
Subject and body lines remain unwrapped, as it's the author's
fault to have such long lines :P
---
lib/PublicInbox/View.pm | 46 ++++++++++++++++++++++++++++++++++-------
t/view.t | 21 +++++++++++++++++++
2 files changed, 59 insertions(+), 8 deletions(-)
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index add3dfc..7339467 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -17,6 +17,7 @@ use PublicInbox::Reply;
require POSIX;
use Time::Local qw(timegm);
+use constant COLS => 72;
use constant INDENT => ' ';
use constant TCHILD => '` ';
sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
@@ -164,6 +165,24 @@ sub in_reply_to {
$refs->[-1];
}
+sub fold_addresses ($) {
+ return $_[0] if length($_[0]) <= COLS;
+ # try to fold on commas after non-word chars before $lim chars,
+ # Try to get the "," preceeded by ">" or ")", but avoid folding
+ # on the comma where somebody uses "Lastname, Firstname".
+ # We also try to keep the last and penultimate addresses in
+ # the list on the same line if possible, hence the extra \z
+ # Fall back to folding on spaces at $lim + 1 chars
+ my $lim = COLS - 8; # 8 = "\t" display width
+ my $too_long = $lim + 1;
+ $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
+ $_[0] = join("\n\t",
+ ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
+ .{1,$lim}(?:,|\z)|
+ .{1,$lim}|
+ .{$too_long,}?)(?:\s|\z)/xgo));
+}
+
sub _hdr_names_html ($$) {
my ($hdr, $field) = @_;
my $val = $hdr->header($field) or return '';
@@ -198,13 +217,6 @@ sub index_entry {
my @tocc;
my $mime = $smsg->{mime};
my $hdr = $mime->header_obj;
- foreach my $f (qw(To Cc)) {
- my $dst = _hdr_names_html($hdr, $f);
- if ($dst ne '') {
- obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
- push @tocc, "$f: $dst";
- }
- }
my $from = _hdr_names_html($hdr, 'From');
obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
$rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
@@ -212,7 +224,24 @@ sub index_entry {
my $mhref = $upfx . mid_escape($mid_raw) . '/';
$rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
$rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
- $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
+ my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
+ my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
+ my ($tlen, $clen) = (length($to), length($cc));
+ my $to_cc = '';
+ if (($tlen + $clen) > COLS) {
+ $to_cc .= ' To: '.$to."\n" if $tlen;
+ $to_cc .= ' Cc: '.$cc."\n" if $clen;
+ } else {
+ if ($tlen) {
+ $to_cc .= ' To: '.$to;
+ $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
+ } else {
+ $to_cc .= ' Cc: '.$cc if $clen;
+ }
+ $to_cc .= "\n";
+ }
+ obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
+ $rv .= $to_cc;
my $mapping = $ctx->{mapping};
if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
@@ -605,6 +634,7 @@ sub _msg_html_prepare {
}
foreach my $h (qw(To Cc)) {
defined($v = $hdr->header($h)) or next;
+ fold_addresses($v);
$v = ascii_html($v);
obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
$rv .= "$h: $v\n" if $v ne '';
diff --git a/t/view.t b/t/view.t
index 8ae4225..b829ecf 100644
--- a/t/view.t
+++ b/t/view.t
@@ -170,4 +170,25 @@ EOF
'regular ID not compressed');
}
+{
+ my $cols = PublicInbox::View::COLS();
+ my @addr;
+ until (length(join(', ', @addr)) > ($cols * 2)) {
+ push @addr, '"l, f" <a@a>';
+ my $n = int(rand(20)) + 1;
+ push @addr, ('x'x$n).'@x';
+ }
+ my $orig = join(', ', @addr);
+ my $res = PublicInbox::View::fold_addresses($orig.'');
+ isnt($res, $orig, 'folded result');
+ unlike($res, qr/l,\n\tf/s, '"last, first" no broken');
+ my @nospc = ($res, $orig);
+ s/\s+//g for @nospc;
+ is($nospc[0], $nospc[1], 'no addresses lost in translation');
+ my $tws = PublicInbox::View::fold_addresses($orig.' ');
+ # (Email::Simple drops leading whitespace, but not trailing)
+ $tws =~ s/ \z//;
+ is($tws, $res, 'not thrown off by trailing whitespace');
+}
+
done_testing();
--
EW
next prev parent reply other threads:[~2018-04-23 4:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-23 4:16 [PATCH 0/3] view: wrap long To:/Cc: lists Eric Wong
2018-04-23 4:16 ` [PATCH 1/3] view: untangle loop when showing message headers Eric Wong
2018-04-23 4:16 ` Eric Wong [this message]
2018-04-23 4:16 ` [PATCH 3/3] view: drop redundant References: display code Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180423041654.4152-3-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).