cmogstored 1.8.1 - use default system stack size
[cmogstored.git] / build-aux / txt2pre
blobe7b94f50c93cad196c447164f0a40bddabfc6d6a
1 #!/usr/bin/env perl
2 # Copyright (C) 2015-2020 all contributors <cmogstored-public@yhbt.net>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
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 $file = shift;
12 my $str;
13 if (defined $file) {
14 open my $fh, '<', $file or die "failed to open $file: $!\n";
15 local $/;
16 $str = <$fh>;
17 } else {
18 $str = eval { local $/; <> };
20 my %xhtml_map = (
21 '"' => '&#34;',
22 '&' => '&#38;',
23 "'" => '&#39;',
24 '<' => '&lt;',
25 '>' => '&gt;',
27 $str =~ s/([<>&'"])/$xhtml_map{$1}/ge;
28 $str = encode('us-ascii', $str, Encode::HTMLCREF);
29 my ($title) = ($str =~ /\A([^\n]+)\n[^a-zA-Z]*\n/s);
31 unless (defined $title) {
32 $title = $file;
33 $title =~ s,\A[^/]*/,,;
34 $title = "cmogstored - $title";
37 # temporarily swap &gt; for escape so our s!! to add href works.
38 # there's probably a way to do this with only a single s!! ...
39 $str =~ s!&gt;!\e!g;
40 $str =~ s!\b((nntps?|ftp|https?|imaps?)://[\w+\+\&\?\.\%\;/#=\!\@-]+)!<a
41 href="$1"\n>$1</a>!g;
43 $str =~ s!\e!&gt;!g; # swap escapes back to &gt;
45 print '<html><head>',
46 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
47 "<title>$title</title>",
48 "</head><body>\n<pre>", $str , '</pre></body></html>';