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 URI::Escape qw(uri_escape_utf8);
11 use PublicInbox::MID qw/mid_clean/;
12 use base qw/Exporter/;
13 our @EXPORT_OK = qw/ascii_html/;
15 # for user-generated content (UGC) which may have excessively long lines
16 # and screw up rendering on some browsers. This is the only CSS style
18 use constant STYLE => '<style>pre{white-space:pre-wrap}</style>';
20 my $enc_ascii = find_encoding('us-ascii');
23 my ($class, $raw, $href) = @_;
25 # we never care about trailing whitespace
29 href => defined $href ? $href : $raw,
34 my ($class, $msgid, $no_compress) = @_;
35 $msgid = mid_clean($msgid);
36 $class->new($msgid, $msgid);
40 my ($class, $raw) = @_;
41 $raw = '' unless defined $raw;
42 $raw =~ tr/\t\n / /s; # squeeze spaces
43 $raw =~ tr/\r//d; # kill CR
57 $s =~ s/\r\n/\n/sg; # fixup bad line endings
58 $s =~ s/([<>&'"])/$xhtml_map{$1}/ge;
59 $enc_ascii->encode($s, Encode::HTMLCREF);
62 sub as_html { ascii_html($_[0]->{raw}) }
63 sub as_href { ascii_html(uri_escape_utf8($_[0]->{href})) }
75 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;