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