]> Sergey Matveev's repositories - public-inbox.git/blob - Documentation/txt2pre
244dc50c9cb9bd0aba4ed7b5866c9895e06b9b85
[public-inbox.git] / Documentation / txt2pre
1 #!/usr/bin/env perl
2 # Copyright (C) 2014-2021 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 PublicInbox::Linkify;
11 use PublicInbox::Hval qw(ascii_html);
12 my %xurls;
13 for (qw[lei(1)
14         lei-add-external(1)
15         lei-config(1)
16         lei-daemon-kill(1)
17         lei-daemon-pid(1)
18         lei-forget-external(1)
19         lei-import(1)
20         lei-init(1)
21         lei-ls-external(1)
22         lei-overview(7)
23         lei-q(1)
24         public-inbox.cgi(1)
25         public-inbox-compact(1)
26         public-inbox-config(5)
27         public-inbox-convert(1)
28         public-inbox-daemon(8)
29         public-inbox-edit(1)
30         public-inbox-glossary(7)
31         public-inbox-httpd(1)
32         public-inbox-imapd(1)
33         public-inbox-index(1)
34         public-inbox-init(1)
35         public-inbox-learn(1)
36         public-inbox-mda(1)
37         public-inbox-nntpd(1)
38         public-inbox-overview(7)
39         public-inbox-purge(1)
40         public-inbox-v1-format(5)
41         public-inbox-v2-format(5)
42         public-inbox-watch(1)
43         public-inbox-xcpdb(1)
44 ]) {
45         my ($n) = (/([\w\-\.]+)/);
46         $xurls{$_} = "$n.html";
47         $xurls{$n} = "$n.html";
48 }
49
50 for (qw[copydatabase(1) xapian-compact(1)]) {
51         my ($n) = (/([\w\-\.]+)/);
52         $xurls{$_} = ".$n.1.html"
53 }
54
55 for (qw[make(1) flock(2) setrlimit(2) vfork(2) tmpfs(5)]) {
56         my ($n, $s) = (/([\w\-]+)\((\d)\)/);
57         $xurls{$_} = "http://www.man7.org/linux/man-pages/man$s/$n.$s.html";
58 }
59
60 for (qw[git(1)
61         git-am(1)
62         git-apply(1)
63         git-config(1)
64         git-credential(1)
65         git-daemon(1)
66         git-fast-import(1)
67         git-fetch(1)
68         git-filter-branch(1)
69         git-format-patch(1)
70         git-gc(1)
71         git-http-backend(1)
72         git-imap-send(1)
73         git-init(1)
74         git-send-email(1)
75         gitrepository-layout(5)
76 ]) {
77         my ($n) = (/([\w\-\.]+)/);
78         $xurls{$_} = "https://kernel.org/pub/software/scm/git/docs/$n.html"
79 }
80
81 for (qw[
82         sd_listen_fds(3)
83         systemd(1)
84         systemd.unit(5)
85         systemd.socket(5)
86 ]) {
87         my ($n) = (/([\w\-\.]+)/);
88         $xurls{$_} = "https://www.freedesktop.org/software/systemd/man/$n.html";
89 }
90
91 $xurls{'netrc(5)'} = 'https://manpages.debian.org/stable/ftp/netrc.5.en.html';
92 $xurls{'mbsync(1)'} =
93         'https://manpages.debian.org/stable/isync/mbsync.1.en.html';
94 $xurls{'offlineimap(1)'} =
95         'https://manpages.debian.org/stable/offlineimap/offlineimap.1.en.html';
96 $xurls{'spamc(1)'} =
97         'https://spamassassin.apache.org/full/3.4.x/doc/spamc.html';
98 $xurls{'grok-pull'} =
99         'https://git.kernel.org/pub/scm/utils/grokmirror/grokmirror.git' .
100         '/tree/man/grok-pull.1.rst';
101 $xurls{'git-filter-repo(1)'} = 'https://github.com/newren/git-filter-repo'.
102                         '/blob/master/Documentation/git-filter-repo.txt';
103 $xurls{'ssoma(1)'} = 'https://ssoma.public-inbox.org/ssoma.txt';
104 $xurls{'cgitrc(5)'} = 'https://git.zx2c4.com/cgit/tree/cgitrc.5.txt';
105 $xurls{'prove(1)'} = 'https://perldoc.perl.org/prove.html';
106 $xurls{'mbox(5)'} = 'https://manpages.debian.org/stable/mutt/mbox.5.en.html';
107
108 my $str = do { local $/; <STDIN> };
109 my ($title) = ($str =~ /\A([^\n]+)/);
110 if ($str =~ /^NAME\n\s+([^\n]+)/sm) {
111         # don't link to ourselves
112         $title = $1;
113         if ($title =~ /([\w\.\-]+)/) {
114                 delete $xurls{$1};
115         }
116 }
117 $title = ascii_html($title);
118 my $l = PublicInbox::Linkify->new;
119 $str = $l->linkify_1($str);
120 $str = ascii_html($str);
121
122 # longest matches, first
123 my @keys = sort { length($b) <=> length($a) } keys %xurls;
124 my $xkeys = join('|', map { quotemeta } @keys);
125 $str =~ s,(?<![>\w_])($xkeys)(?!(?:[\w<\-]|\.html)),
126         qq(<a\nhref=").$xurls{$1}.qq(">$1).($2//'').'</a>',sge;
127
128 $str = $l->linkify_2($str);
129
130 print '<html><head>',
131   qq(<meta\nhttp-equiv="Content-Type"\ncontent="text/html; charset=utf-8"\n/>),
132   "<title>$title</title>",
133   "</head><body><pre>",  $str , '</pre></body></html>';
134 STDOUT->flush;
135
136 # keep mtime on website consistent so clients can cache
137 if (-f STDIN && -f STDOUT) {
138         my @st = stat(STDIN);
139         utime($st[8], $st[9], \*STDOUT);
140 }