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