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)
4 # represents a header value in various forms
5 package PublicInbox::Hval;
8 use fields qw(raw href);
9 use Encode qw(find_encoding);
10 use URI::Escape qw(uri_escape_utf8);
12 my $enc_ascii = find_encoding('us-ascii');
15 my ($class, $raw, $href) = @_;
16 my $self = fields::new($class);
18 # we never care about leading/trailing whitespace
22 $self->{href} = defined $href ? $href : $raw;
27 my ($class, $msgid) = @_;
28 $msgid =~ s/\A\s*<?//;
29 $msgid =~ s/>?\s*\z//;
31 if (length($msgid) <= 40) {
35 my $hex = Digest::SHA::sha1_hex($msgid);
36 $class->new($msgid, $hex);
41 my ($class, $raw) = @_;
42 $raw = '' unless defined $raw;
43 $raw =~ tr/\t\n / /s; # squeeze spaces
44 $raw =~ tr/\r//d; # kill CR
58 $s =~ s/\r\n/\n/sg; # fixup bad line endings
59 $s =~ s/([<>&'"])/$xhtml_map{$1}/ge;
60 $enc_ascii->encode($s, Encode::HTMLCREF);
63 sub as_html { ascii_html($_[0]->{raw}) }
64 sub as_href { ascii_html(uri_escape_utf8($_[0]->{href})) }