| 1 |
#!/usr/local/bin/perl |
| 2 |
|
| 3 |
# AUTHOR: Don Owens <[email protected]> |
| 4 |
# OWNER: 2010 Don Owens <[email protected]> |
| 5 |
# LICENSE: Perl_5 |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
use Carp; |
| 10 |
use Getopt::Long qw(:config no_ignore_case bundling); |
| 11 |
|
| 12 |
use Pod::POM::View::Restructured; |
| 13 |
|
| 14 |
our $VERSION = '1.000004'; # VERSION |
| 15 |
|
| 16 |
# main |
| 17 |
{ |
| 18 |
# local($SIG{__DIE__}) = sub { &Carp::confess }; |
| 19 |
my $self = bless { }, __PACKAGE__; |
| 20 |
|
| 21 |
my $opts = $self->get_options([ "help|h", |
| 22 |
"in_file|infile|in-file|i=s", |
| 23 |
"out_file|outfile|out-file|o=s", |
| 24 |
"title=s", |
| 25 |
], { }); |
| 26 |
$self->check_options($opts, [ ]); # die's on bad options |
| 27 |
$self->{opts} = $opts; |
| 28 |
|
| 29 |
my ($in_fh, $out_fh) = $self->open_files; |
| 30 |
|
| 31 |
my $conv = Pod::POM::View::Restructured->new; |
| 32 |
my $output = $conv->convert_file($in_fh, $opts->{title}, $out_fh); |
| 33 |
|
| 34 |
unless (defined($output)) { |
| 35 |
print STDERR "\n\nFailed to convert!\n\n"; |
| 36 |
exit 1; |
| 37 |
} |
| 38 |
|
| 39 |
exit 0; |
| 40 |
} |
| 41 |
|
| 42 |
exit 0; |
| 43 |
|
| 44 |
############################################################################### |
| 45 |
# Subroutines |
| 46 |
|
| 47 |
sub open_files { |
| 48 |
my ($self) = @_; |
| 49 |
my $opts = $self->{opts}; |
| 50 |
|
| 51 |
my $in_fh; |
| 52 |
my $out_fh; |
| 53 |
|
| 54 |
if (defined($opts->{in_file})) { |
| 55 |
open($in_fh, '<', $opts->{in_file}) |
| 56 |
or die "couldn't open input file $opts->{in_file}: $!"; |
| 57 |
} |
| 58 |
else { |
| 59 |
$in_fh = \*STDIN; |
| 60 |
} |
| 61 |
|
| 62 |
if (defined($opts->{out_file})) { |
| 63 |
open($out_fh, '>', $opts->{out_file}) |
| 64 |
or die "couldn't open output file $opts->{out_file}: $!"; |
| 65 |
} |
| 66 |
else { |
| 67 |
$out_fh = \*STDOUT; |
| 68 |
} |
| 69 |
|
| 70 |
return ($in_fh, $out_fh); |
| 71 |
} |
| 72 |
|
| 73 |
########## begin option processing ########## |
| 74 |
sub print_usage { |
| 75 |
print STDERR qq{\nUsage: @{[ ($0 =~ m{\A.*/([^/]+)\Z})[0] || $0 ]} options |
| 76 |
|
| 77 |
Options: |
| 78 |
--infile # filename for the pod to convert (input taken from |
| 79 |
# stdin by default) |
| 80 |
--outfile # filename for the resulting reStructuredText file |
| 81 |
# (output sent to stdout by default) |
| 82 |
--title # title for the document (by default an attempt will |
| 83 |
# be made to extract the title, assuming that the first |
| 84 |
# section is a head1 called NAME) |
| 85 |
[-h | --help] # this help msg |
| 86 |
\n}; |
| 87 |
} |
| 88 |
|
| 89 |
sub check_options { |
| 90 |
my ($self, $opts, $required) = @_; |
| 91 |
|
| 92 |
if (not $opts or $opts->{help}) { |
| 93 |
$self->print_usage; |
| 94 |
exit 1; |
| 95 |
} |
| 96 |
|
| 97 |
my $opt_ok = 1; |
| 98 |
$required = [ ] unless $required; |
| 99 |
foreach my $key (@$required) { |
| 100 |
if (defined($opts->{$key})) { |
| 101 |
if (my $v = $opts->{$key}) { |
| 102 |
if (my $ref = ref($v)) { |
| 103 |
if ($ref eq 'ARRAY' ) { |
| 104 |
unless (@$v) { |
| 105 |
$opt_ok = 0; |
| 106 |
warn "missing required option '$key' |
| 107 |
"; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
else { |
| 114 |
$opt_ok = 0; |
| 115 |
warn "missing required option '$key'\n"; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
unless ($opt_ok) { |
| 120 |
$self->print_usage; |
| 121 |
exit 1; |
| 122 |
} |
| 123 |
|
| 124 |
return $opt_ok; |
| 125 |
} |
| 126 |
|
| 127 |
sub get_options { |
| 128 |
my ($self, $spec, $defaults) = @_; |
| 129 |
my %opts = $defaults ? %$defaults : (); |
| 130 |
$spec = [ ] unless $spec; |
| 131 |
|
| 132 |
my $process_opt = sub { |
| 133 |
my ($key, $val) = @_; |
| 134 |
|
| 135 |
if (scalar(@_) > 2) { |
| 136 |
$opts{$key}{$val} = $_[2]; |
| 137 |
} |
| 138 |
else { |
| 139 |
if ( exists($opts{$key}) and (my $v = $opts{$key}) ) { |
| 140 |
if (my $ref = ref($v)) { |
| 141 |
if ($ref eq 'ARRAY' ) { |
| 142 |
push @{ $opts{$key} }, $val; |
| 143 |
return 1; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$opts{$key} = $val; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
my $opt_rv = Getopt::Long::GetOptions(map { ($_ => $process_opt) } @$spec); |
| 153 |
|
| 154 |
return $opt_rv ? \%opts : undef; |
| 155 |
} |
| 156 |
########## end option processing ########## |
| 157 |
|
| 158 |
__END__ |
| 159 |
|
| 160 |
=pod |
| 161 |
|
| 162 |
=head1 NAME |
| 163 |
|
| 164 |
pod2rst - convert .pod files to .rst files |
| 165 |
|
| 166 |
=head1 SYNOPSIS |
| 167 |
|
| 168 |
=for pod2rst next-code-block: bash |
| 169 |
|
| 170 |
pod2rst --infile=<name> --outfile=<name> --title=<name> |
| 171 |
|
| 172 |
=head1 DESCRIPTION |
| 173 |
|
| 174 |
Converts files containing POD to reStructuredText format for input to Sphinx. |
| 175 |
|
| 176 |
=head1 ARGUMENTS |
| 177 |
|
| 178 |
C<pod2rst> takes the following arguments: |
| 179 |
|
| 180 |
=over 4 |
| 181 |
|
| 182 |
=item infile |
| 183 |
|
| 184 |
--infile=<name> |
| 185 |
|
| 186 |
Specifies the pod file to convert. Input is taken from stdin by default. |
| 187 |
|
| 188 |
=item outfile |
| 189 |
|
| 190 |
--outfile=<name> |
| 191 |
|
| 192 |
Specifies the reStructuredText file to create. Output goes to stdout by default. |
| 193 |
|
| 194 |
=item title |
| 195 |
|
| 196 |
--title=<title> |
| 197 |
|
| 198 |
Specifies the section title for the file. |
| 199 |
|
| 200 |
=back |
| 201 |
|
| 202 |
=head1 SEE ALSO |
| 203 |
|
| 204 |
L<Pod::POM::View::Restructured>, L<pod2html> |
| 205 |
|
| 206 |
Sphinx: L<https://www.sphinx-doc.org/> |
| 207 |
|
| 208 |
=head1 AUTHOR |
| 209 |
|
| 210 |
Don Owens <[email protected]> |
| 211 |
|
| 212 |
=head1 COPYRIGHT |
| 213 |
|
| 214 |
This software is copyright (c) 2010 by Don Owens <[email protected]>. |
| 215 |
|
| 216 |
This software is available under the same terms as the perl 5 programming language system itself. |
| 217 |
|
| 218 |
=head1 VERSION |
| 219 |
|
| 220 |
1.000004 |
| 221 |
|
| 222 |
=cut |