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