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