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