#!/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
# 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
# Fixed:
# ... B
# bar
# Fixed:
# =item - - a
# Fixed:
# \\` => \`
# Not fixed (RST does not support Bold-Italic):
# B>
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() {
# Remove comments
/^\#/ and next;
# quote -
s/^=item -/=item \001/;
if(/^ /) {
# ignore source blocks
} else {
# \\ => \
s/\\/\\\\/g;
}
print;
}
}
sub pre2 {
$/="\n\n";
while() {
# join lines in each paragraph
s/(\S)\n(\S)/$1 $2/g;
print;
}
}
sub pod2rst {
exec "pod2rst";
}
sub post {
while() {
# =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);