1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # represents a header value in various forms. Used for HTML generation
5 # in our web interface(s)
6 package PublicInbox::Hval;
9 use Encode qw(find_encoding);
10 use PublicInbox::MID qw/mid_clean mid_escape/;
11 use base qw/Exporter/;
12 our @EXPORT_OK = qw/ascii_html obfuscate_addrs/;
14 # for user-generated content (UGC) which may have excessively long lines
15 # and screw up rendering on some browsers. This is the only CSS style
17 use constant STYLE => '<style>pre{white-space:pre-wrap}</style>';
19 my $enc_ascii = find_encoding('us-ascii');
22 my ($class, $raw, $href) = @_;
24 # we never care about trailing whitespace
28 href => defined $href ? $href : $raw,
33 my ($class, $msgid) = @_;
34 $class->new($msgid, mid_escape($msgid));
38 my ($class, $raw) = @_;
39 $raw = '' unless defined $raw;
40 $raw =~ tr/\t\n / /s; # squeeze spaces
41 $raw =~ tr/\r//d; # kill CR
53 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
54 # some of these overrides are standard C escapes so they're
55 # easy-to-understand when rendered.
56 $xhtml_map{"\x00"} = '\\0'; # NUL
57 $xhtml_map{"\x07"} = '\\a'; # bell
58 $xhtml_map{"\x08"} = '\\b'; # backspace
59 $xhtml_map{"\x09"} = "\t"; # obvious to show as-is
60 $xhtml_map{"\x0a"} = "\n"; # obvious to show as-is
61 $xhtml_map{"\x0b"} = '\\v'; # vertical tab
62 $xhtml_map{"\x0c"} = '\\f'; # form feed
63 $xhtml_map{"\x0d"} = '\\r'; # carriage ret (not preceding \n)
64 $xhtml_map{"\x1b"} = '^['; # ASCII escape (mutt seems to escape this way)
65 $xhtml_map{"\x7f"} = '\\x7f'; # DEL
69 $s =~ s/\r\n/\n/sg; # fixup bad line endings
70 $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
71 $enc_ascii->encode($s, Encode::HTMLCREF);
74 sub as_html { ascii_html($_[0]->{raw}) }
86 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
89 # for misguided people who believe in this stuff, give them a
90 # substitution for '.'
91 # ․ · and ͺ were also candidates:
92 # https://public-inbox.org/meta/20170615015250.GA6484@starla/
93 # However, • was chosen to make copy+paste errors more obvious
94 sub obfuscate_addrs ($) { $_[0] =~ s/(\S+@[^\.]+)\./$1•/g }