]> Sergey Matveev's repositories - public-inbox.git/blob - Documentation/txt2pre
various documentation updates
[public-inbox.git] / Documentation / txt2pre
1 #!/usr/bin/env perl
2 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
3 # License: AGPLv3 or later (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 CGI qw/escapeHTML/;
11 use Encode qw/encode/;
12 my $str = eval { local $/; <> };
13 $str = escapeHTML($str);
14 $str = encode('us-ascii', $str, Encode::HTMLCREF);
15 my ($title) = ($str =~ /\A([^\n]+)/);
16
17 # temporarily swap &gt; for escape so our s!! to add href works.
18 # there's probably a way to do this with only a single s!! ...
19 $str =~ s!&gt;!\e!g;
20 $str =~ s!\b(https?://[\w+\+\&\?\.\%\;/-]+)!<a\nhref="$1"\n>$1</a>!g;
21 $str =~ s!\e!&gt;!g; # swap escapes back to &gt;
22
23 print '<html><head>',
24   '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
25   "<title>$title</title>",
26   "</head><body>\n<pre>",  $str , '</pre></body></html>';