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