]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
www: ensure XML validity for some odd ASCII chars
[public-inbox.git] / lib / PublicInbox / Hval.pm
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)
3 #
4 # represents a header value in various forms.  Used for HTML generation
5 # in our web interface(s)
6 package PublicInbox::Hval;
7 use strict;
8 use warnings;
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/;
14
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
17 # feature we use.
18 use constant STYLE => '<style>pre{white-space:pre-wrap}</style>';
19
20 my $enc_ascii = find_encoding('us-ascii');
21
22 sub new {
23         my ($class, $raw, $href) = @_;
24
25         # we never care about trailing whitespace
26         $raw =~ s/\s*\z//;
27         bless {
28                 raw => $raw,
29                 href => defined $href ? $href : $raw,
30         }, $class;
31 }
32
33 sub new_msgid {
34         my ($class, $msgid) = @_;
35         $msgid = mid_clean($msgid);
36         $class->new($msgid, $msgid);
37 }
38
39 sub new_oneline {
40         my ($class, $raw) = @_;
41         $raw = '' unless defined $raw;
42         $raw =~ tr/\t\n / /s; # squeeze spaces
43         $raw =~ tr/\r//d; # kill CR
44         $class->new($raw);
45 }
46
47 my %xhtml_map = (
48         '"' => '&#34;',
49         '&' => '&#38;',
50         "'" => '&#39;',
51         '<' => '&lt;',
52         '>' => '&gt;',
53 );
54
55 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
56 # some of these overrides are standard C escapes so they're
57 # easy-to-understand when rendered.
58 $xhtml_map{"\x00"} = '\\0'; # NUL
59 $xhtml_map{"\x07"} = '\\a'; # bell
60 $xhtml_map{"\x08"} = '\\b'; # backspace
61 $xhtml_map{"\x09"} = "\t"; # obvious to show as-is
62 $xhtml_map{"\x0a"} = "\n"; # obvious to show as-is
63 $xhtml_map{"\x0b"} = '\\v'; # vertical tab
64 $xhtml_map{"\x0c"} = '\\f'; # form feed
65 $xhtml_map{"\x0d"} = '\\r'; # carriage ret (not preceding \n)
66 $xhtml_map{"\x1b"} = '^['; # ASCII escape (mutt seems to escape this way)
67 $xhtml_map{"\x7f"} = '\\x7f'; # DEL
68
69 sub ascii_html {
70         my ($s) = @_;
71         $s =~ s/\r\n/\n/sg; # fixup bad line endings
72         $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
73         $enc_ascii->encode($s, Encode::HTMLCREF);
74 }
75
76 sub as_html { ascii_html($_[0]->{raw}) }
77 sub as_href { ascii_html(uri_escape_utf8($_[0]->{href})) }
78
79 sub raw {
80         if (defined $_[1]) {
81                 $_[0]->{raw} = $_[1];
82         } else {
83                 $_[0]->{raw};
84         }
85 }
86
87 sub prurl {
88         my ($env, $u) = @_;
89         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
90 }
91
92 1;