]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
hval: set font-size:100% for all elements
[public-inbox.git] / lib / PublicInbox / Hval.pm
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 PublicInbox::MID qw/mid_clean mid_escape/;
11 use base qw/Exporter/;
12 our @EXPORT_OK = qw/ascii_html obfuscate_addrs to_filename/;
13
14 # User-generated content (UGC) may have excessively long lines
15 # and screw up rendering on some browsers, so we use pre-wrap.
16 #
17 # We also force everything to the same scaled font-size because GUI
18 # browsers (tested both Firefox and surf (webkit)) uses a larger font
19 # for the Search <form> element than the rest of the page.  Font size
20 # uniformity is important to people who rely on gigantic fonts.
21 use constant STYLE =>
22         '<style>pre{white-space:pre-wrap}*{font-size:100%}</style>';
23
24 my $enc_ascii = find_encoding('us-ascii');
25
26 sub new {
27         my ($class, $raw, $href) = @_;
28
29         # we never care about trailing whitespace
30         $raw =~ s/\s*\z//;
31         bless {
32                 raw => $raw,
33                 href => defined $href ? $href : $raw,
34         }, $class;
35 }
36
37 sub new_msgid {
38         my ($class, $msgid) = @_;
39         $class->new($msgid, mid_escape($msgid));
40 }
41
42 sub new_oneline {
43         my ($class, $raw) = @_;
44         $raw = '' unless defined $raw;
45         $raw =~ tr/\t\n / /s; # squeeze spaces
46         $raw =~ tr/\r//d; # kill CR
47         $class->new($raw);
48 }
49
50 my %xhtml_map = (
51         '"' => '&#34;',
52         '&' => '&#38;',
53         "'" => '&#39;',
54         '<' => '&lt;',
55         '>' => '&gt;',
56 );
57
58 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
59 # some of these overrides are standard C escapes so they're
60 # easy-to-understand when rendered.
61 $xhtml_map{"\x00"} = '\\0'; # NUL
62 $xhtml_map{"\x07"} = '\\a'; # bell
63 $xhtml_map{"\x08"} = '\\b'; # backspace
64 $xhtml_map{"\x09"} = "\t"; # obvious to show as-is
65 $xhtml_map{"\x0a"} = "\n"; # obvious to show as-is
66 $xhtml_map{"\x0b"} = '\\v'; # vertical tab
67 $xhtml_map{"\x0c"} = '\\f'; # form feed
68 $xhtml_map{"\x0d"} = '\\r'; # carriage ret (not preceding \n)
69 $xhtml_map{"\x1b"} = '^['; # ASCII escape (mutt seems to escape this way)
70 $xhtml_map{"\x7f"} = '\\x7f'; # DEL
71
72 sub ascii_html {
73         my ($s) = @_;
74         $s =~ s/\r\n/\n/sg; # fixup bad line endings
75         $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
76         $enc_ascii->encode($s, Encode::HTMLCREF);
77 }
78
79 sub as_html { ascii_html($_[0]->{raw}) }
80
81 sub raw {
82         if (defined $_[1]) {
83                 $_[0]->{raw} = $_[1];
84         } else {
85                 $_[0]->{raw};
86         }
87 }
88
89 sub prurl {
90         my ($env, $u) = @_;
91         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
92 }
93
94 # for misguided people who believe in this stuff, give them a
95 # substitution for '.'
96 # &#8228; &#183; and &#890; were also candidates:
97 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
98 # However, &#8226; was chosen to make copy+paste errors more obvious
99 sub obfuscate_addrs ($$;$) {
100         my $ibx = $_[0];
101         my $repl = $_[2] || '&#8226;';
102         my $re = $ibx->{-no_obfuscate_re}; # regex of domains
103         my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
104         $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
105                 my ($addr, $user, $domain) = ($1, $2, $3);
106                 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
107                         $addr;
108                 } else {
109                         $domain =~ s!([^\.]+)\.!$1$repl!;
110                         $user . '@' . $domain
111                 }
112                 /sge;
113 }
114
115 # like format_sanitized_subject in git.git pretty.c with '%f' format string
116 sub to_filename ($) {
117         my ($s, undef) = split(/\n/, $_[0]);
118         $s =~ s/[^A-Za-z0-9_\.]+/-/g;
119         $s =~ tr/././s;
120         $s =~ s/[\.\-]+\z//;
121         $s =~ s/\A[\.\-]+//;
122         $s
123 }
124
125 1;