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