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