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