cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob e7b94f50c93cad196c447164f0a40bddabfc6d6a 1399 bytes (raw)
$ git show HEAD:build-aux/txt2pre	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
#!/usr/bin/env perl
# Copyright (C) 2015-2020 all contributors <cmogstored-public@yhbt.net>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
#
# Stupid script to make HTML from preformatted, utf-8 text versions,
# only generating links for http(s).  Markdown does too much
# and requires indentation to output preformatted text.
use strict;
use warnings;
use Encode qw/encode/;
my $file = shift;
my $str;
if (defined $file) {
	open my $fh, '<', $file or die "failed to open $file: $!\n";
	local $/;
	$str = <$fh>;
} else {
	$str = eval { local $/; <> };
}
my %xhtml_map = (
	'"' => '&#34;',
	'&' => '&#38;',
	"'" => '&#39;',
	'<' => '&lt;',
	'>' => '&gt;',
);
$str =~ s/([<>&'"])/$xhtml_map{$1}/ge;
$str = encode('us-ascii', $str, Encode::HTMLCREF);
my ($title) = ($str =~ /\A([^\n]+)\n[^a-zA-Z]*\n/s);

unless (defined $title) {
	$title = $file;
	$title =~ s,\A[^/]*/,,;
	$title = "cmogstored - $title";
}

# temporarily swap &gt; for escape so our s!! to add href works.
# there's probably a way to do this with only a single s!! ...
$str =~ s!&gt;!\e!g;
$str =~ s!\b((nntps?|ftp|https?|imaps?)://[\w+\+\&\?\.\%\;/#=\!\@-]+)!<a
href="$1"\n>$1</a>!g;

$str =~ s!\e!&gt;!g; # swap escapes back to &gt;

print '<html><head>',
  '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
  "<title>$title</title>",
  "</head><body>\n<pre>",  $str , '</pre></body></html>';

git clone https://yhbt.net/cmogstored.git