Released as 20240522 ('Tbilisi')
[parallel.git] / src / pod2rst-fix
blobab40f5103e57e5fd1ad75395c884cdcb13a2e3ed
1 #!/usr/bin/perl
3 # Copyright (C) 2007-2022 Ole Tange, http://ole.tange.dk and Free
4 # Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, see <https://www.gnu.org/licenses/>
18 # or write to the Free Software Foundation, Inc., 51 Franklin St,
19 # Fifth Floor, Boston, MA 02110-1301 USA
21 # SPDX-FileCopyrightText: 2021-2022 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
22 # SPDX-License-Identifier: GPL-3.0-or-later
24 # This fixes problems in pod2rst conversion
26 # Conversion errors:
28 # B<foo `bar` baz>
30 # Fixed:
31 # ... B<foo>
32 # bar
34 # Fixed:
35 # =item - - a
37 # Fixed:
38 # \\` => \`
40 # Not fixed (RST does not support Bold-Italic):
41 # B<cat | xargs -d "\n" -n1 I<command>>
43 sub pipefunc {
44 # Emulate a shell pipe but between Perl functions
45 # stdin | func1 | func2 | ... | funcN
46 # pipefunc(*func1, *func2, ..., *funcN);
47 my $func = pop;
49 my $pid = open(my $kid_to_read, "-|");
50 defined($pid) || die "can't fork: $!";
51 if ($pid) {
52 open STDIN, "<&", $kid_to_read or die;
53 &$func();
54 } else { # child
55 close $kid_to_read;
56 if($_[1]) {
57 # More than one function remaining: Recurse
58 pipefunc(@_);
59 } else {
60 # Only one function remaining: Run it
61 $func = pop;
62 &$func();
64 exit 0;
68 sub pre1 {
69 while(<STDIN>) {
70 # Remove comments
71 /^\#/ and next;
72 # quote -
73 s/^=item -/=item \001/;
75 if(/^ /) {
76 # ignore source blocks
77 } else {
78 # \\ => \
79 s/\\/\\\\/g;
81 print;
85 sub pre2 {
86 $/="\n\n";
87 while(<STDIN>) {
88 # join lines in each paragraph
89 s/(\S)\n(\S)/$1 $2/g;
90 print;
94 sub pod2rst {
95 exec "pod2rst";
98 sub post {
99 while(<STDIN>) {
100 # =item in =item
101 s/- \\[*]/- /;
102 # B<*.log>
103 s/\\\\[*]/\\*/g;
104 # - -
105 s/^-(\s+)\001/-$1\\-/g;
106 # \\` => \`
107 s/\\\\`/\\`/g;
108 print;
112 # stdin | pre1() | pre2() | pod2rst() | post()
113 pipefunc(*pre1,*pre2,*pod2rst,*post);