]> Sergey Matveev's repositories - public-inbox.git/blob - Documentation/txt2pre
Merge branch 'unsubscribe'
[public-inbox.git] / Documentation / txt2pre
1 #!/usr/bin/env perl
2 # Copyright (C) 2014-2016 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Stupid script to make HTML from preformatted, utf-8 text versions,
6 # only generating links for http(s).  Markdown does too much
7 # and requires indentation to output preformatted text.
8 use strict;
9 use warnings;
10 use Encode qw/encode/;
11 my $str = eval { local $/; <> };
12 my %xhtml_map = (
13         '"' => '&#34;',
14         '&' => '&#38;',
15         "'" => '&#39;',
16         '<' => '&lt;',
17         '>' => '&gt;',
18 );
19 $str =~ s/([<>&'"])/$xhtml_map{$1}/ge;
20 $str = encode('us-ascii', $str, Encode::HTMLCREF);
21 my ($title) = ($str =~ /\A([^\n]+)/);
22
23 # temporarily swap &gt; for escape so our s!! to add href works.
24 # there's probably a way to do this with only a single s!! ...
25 $str =~ s!&gt;!\e!g;
26 $str =~ s!\b((nntp|ftp|https?)://[\w+\+\&\?\.\%\;/#-]+)!<a
27 href="$1"\n>$1</a>!g;
28
29 $str =~ s!\e!&gt;!g; # swap escapes back to &gt;
30
31 print '<html><head>',
32   '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
33   "<title>$title</title>",
34   "</head><body>\n<pre>",  $str , '</pre></body></html>';