| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
our $VERSION = '0.03'; |
| 7 |
|
| 8 |
use Getopt::Long; |
| 9 |
use Pod::2::DocBook 0.03; |
| 10 |
use Pod::Usage; |
| 11 |
use List::MoreUtils 'none'; |
| 12 |
use CPAN::Version; |
| 13 |
|
| 14 |
exit main(); |
| 15 |
|
| 16 |
sub main { |
| 17 |
my $doctype = 'section'; |
| 18 |
my $spaces = 2; |
| 19 |
my ($title, $double_quotes, $help, $no_header, $base_id, $id_version, $parse_title, $skip, $min_version); |
| 20 |
|
| 21 |
GetOptions( |
| 22 |
'doctype=s' => \$doctype, |
| 23 |
'base-id=s' => \$base_id, |
| 24 |
'title=s' => \$title, |
| 25 |
'spaces=i' => \$spaces, |
| 26 |
'fix-double-quotes' => \$double_quotes, |
| 27 |
'no-header' => \$no_header, |
| 28 |
'help' => \$help, |
| 29 |
'id-version=i' => \$id_version, |
| 30 |
'parse-title' => \$parse_title, |
| 31 |
'skip=s' => \$skip, |
| 32 |
'min-version=s' => \$min_version, |
| 33 |
) or pod2usage(); |
| 34 |
|
| 35 |
pod2usage if $help; |
| 36 |
pod2usage if none { $doctype eq $_ } qw(article chapter refentry section); |
| 37 |
pod2usage unless $spaces =~ /^\d+$/xms; |
| 38 |
|
| 39 |
die 'version "'.$min_version.'" required, this is just "'.$VERSION.'"'."\n" |
| 40 |
if ($min_version and CPAN::Version->vlt($VERSION, $min_version)); |
| 41 |
|
| 42 |
# TODO refactor this if so that there are no the same patterns in each of |
| 43 |
# the branch |
| 44 |
if (@ARGV == 0) { |
| 45 |
$title = q{} unless defined $title; |
| 46 |
my $parser = Pod::2::DocBook->new( |
| 47 |
title => $title, |
| 48 |
doctype => $doctype, |
| 49 |
base_id => $base_id, |
| 50 |
fix_double_quotes => $double_quotes, |
| 51 |
header => !$no_header, |
| 52 |
spaces => $spaces, |
| 53 |
id_version => $id_version, |
| 54 |
skip => $skip, |
| 55 |
); |
| 56 |
$parser->parse_from_filehandle(\*STDIN); |
| 57 |
} |
| 58 |
else { |
| 59 |
my ($infile, $outfile) = @ARGV; |
| 60 |
|
| 61 |
# default doctype element id is the name of the input file |
| 62 |
$base_id ||= $infile; |
| 63 |
|
| 64 |
$title = parse_title($infile) |
| 65 |
if $parse_title; |
| 66 |
($title) = $infile =~ m{([^/]+)\z}xms unless defined $title; |
| 67 |
|
| 68 |
my $parser = Pod::2::DocBook->new( |
| 69 |
title => $title, |
| 70 |
doctype => $doctype, |
| 71 |
base_id => $base_id, |
| 72 |
fix_double_quotes => $double_quotes, |
| 73 |
header => !$no_header, |
| 74 |
spaces => $spaces, |
| 75 |
id_version => $id_version, |
| 76 |
skip => $skip, |
| 77 |
); |
| 78 |
|
| 79 |
if (defined $outfile) { |
| 80 |
$parser->parse_from_file($infile, $outfile); |
| 81 |
} |
| 82 |
|
| 83 |
else { |
| 84 |
$parser->parse_from_file($infile); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
sub parse_title { |
| 92 |
my $filename = shift; |
| 93 |
|
| 94 |
open(my $fh, '<', $filename) or die 'failed to open "'.$filename.'"'; |
| 95 |
|
| 96 |
my $in_name = 0; |
| 97 |
my $line; |
| 98 |
while ($line = <$fh>) { |
| 99 |
# skip empty lines |
| 100 |
next if $line =~ m/^\s*$/; |
| 101 |
|
| 102 |
# we have the title, it's the first text line inside "=head1 NAME" |
| 103 |
last |
| 104 |
if ($in_name); |
| 105 |
|
| 106 |
# found "=head1 NAME" |
| 107 |
$in_name = 1 |
| 108 |
if $line =~ m/^=head1 \s+ NAME/xmsi; |
| 109 |
} |
| 110 |
|
| 111 |
close($filename); |
| 112 |
|
| 113 |
chomp($line); |
| 114 |
|
| 115 |
# remove formating codes from title and xml characters |
| 116 |
$line =~ s/[IBCLEFSXZ]<(.*)>/$1/xmsg; |
| 117 |
$line =~ s/&/&/; |
| 118 |
$line =~ s/</</; |
| 119 |
$line =~ s/>/>/; |
| 120 |
|
| 121 |
return $line |
| 122 |
if $line !~ m/^[^-]+-\s+(.+)$/; |
| 123 |
|
| 124 |
return $1; |
| 125 |
} |
| 126 |
|
| 127 |
__END__ |
| 128 |
|
| 129 |
=head1 NAME |
| 130 |
|
| 131 |
pod2docbook - Convert POD data to DocBook SGML |
| 132 |
|
| 133 |
=head1 SYNOPSIS |
| 134 |
|
| 135 |
pod2docbook [B<--help>] |
| 136 |
[B<--doctype>=B<article>|B<chapter>|B<section>|B<refentry>] |
| 137 |
[B<--title>=I<title>] S<[B<--spaces>=I<# spaces per indent level>]> |
| 138 |
[B<--fix-double-quotes>] [B<--no-header>] |
| 139 |
[B<--base-id>=I<idstring>] |
| 140 |
S<[I<infile> [I<outfile>]]> |
| 141 |
|
| 142 |
=head1 DESCRIPTION |
| 143 |
|
| 144 |
B<pod2docbook> converts files from pod format (see L<perlpod>) to |
| 145 |
DocBook 4.2 SGML (see L<http://www.docbook.org/>). The program itself |
| 146 |
is merely a driver for the Pod::2::DocBook class; if you're interested in |
| 147 |
details of pod-to-SGML translation see L<Pod::2::DocBook>. |
| 148 |
|
| 149 |
=head1 OPTIONS AND ARGUMENTS |
| 150 |
|
| 151 |
=over |
| 152 |
|
| 153 |
=item [B<--help>] |
| 154 |
|
| 155 |
Print usage and exit. |
| 156 |
|
| 157 |
=item [B<--doctype>=B<article>|B<chapter>|B<section>|B<refentry>] |
| 158 |
|
| 159 |
Specifies the document type for the output file; the default is |
| 160 |
B<section>. |
| 161 |
|
| 162 |
=item [B<--title>=I<title>] |
| 163 |
|
| 164 |
Specifies the document title. The default is I<infile>, if it is |
| 165 |
supplied, or empty string otherwise. |
| 166 |
|
| 167 |
=item [B<--spaces>=I<# spaces per indent level>] |
| 168 |
|
| 169 |
Specifies the number of spaces per indent level in the SGML output; |
| 170 |
the default is 2. |
| 171 |
|
| 172 |
=item [B<--fix-double-quotes>] |
| 173 |
|
| 174 |
Replace pairs of double quotes in regular paragraphs with <quote> and |
| 175 |
</quote> (see L<Pod::2::DocBook> for details). |
| 176 |
|
| 177 |
=item [B<--no-header>] |
| 178 |
|
| 179 |
Omit the DOCTYPE line from the output. |
| 180 |
|
| 181 |
=item I<infile> |
| 182 |
|
| 183 |
The name of the file from which to read pod source; if not supplied, |
| 184 |
STDIN is used for input. |
| 185 |
|
| 186 |
=item I<outfile> |
| 187 |
|
| 188 |
The name of the file to which to write SGML; if not supplied, STDOUT |
| 189 |
is used for output. |
| 190 |
|
| 191 |
=item [B<--base-id>=I<idstring>] |
| 192 |
|
| 193 |
The root element of the B<--doctype> will have the I<idstring> set for attribute I<id>. |
| 194 |
The default is an input file name "cleaned up" to conform with XML restriction for |
| 195 |
characteds used in id strings. (SEE L<http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name>) |
| 196 |
|
| 197 |
=back |
| 198 |
|
| 199 |
=head1 SEE ALSO |
| 200 |
|
| 201 |
L<pod2docbook>, L<perlpod>, L<Pod::DocBook>, |
| 202 |
SVN repo - L<https://cle.sk/repos/pub/cpan/Pod-2-DocBook/>, |
| 203 |
L<http://www.ohloh.net/projects/pod-2-docbook>, |
| 204 |
F<doc/> + F<examples/pod2docbook-docbook/> for Pod::2::DocBook |
| 205 |
DocBook documentation |
| 206 |
|
| 207 |
DocBook related links: L<http://www.docbook.org/>, |
| 208 |
L<http://www.sagehill.net/docbookxsl/>, |
| 209 |
L<http://developers.cogentrts.com/cogent/prepdoc/pd-axfrequentlyuseddocbooktags.html> |
| 210 |
|
| 211 |
=head1 AUTHOR |
| 212 |
|
| 213 |
Alligator Descartes <[email protected]> wrote a module called |
| 214 |
Pod::2::DocBook, which was later maintained by Jan Iven |
| 215 |
<[email protected]>. That module was based on the original L<pod2html> |
| 216 |
by Tom Christiansen <[email protected]>. |
| 217 |
|
| 218 |
Nandu Shah <[email protected]> wrote Pod::DocBook, which is |
| 219 |
unrelated to the previous module (even though they both perform the |
| 220 |
same function). (L<http://search.cpan.org/~nandu/Pod-DocBook-1.2/>) |
| 221 |
|
| 222 |
Jozef Kutej <[email protected]> renamed the module to Pod::2::DocBook |
| 223 |
because Nandus version was buried in the CPAN archive as an |
| 224 |
"UNAUTHORIZED RELEASE". |
| 225 |
|
| 226 |
=head1 COPYRIGHT |
| 227 |
|
| 228 |
Copyright 2004, Nandu Shah <[email protected]> |
| 229 |
|
| 230 |
Copyright 2008, Jozef Kutej <[email protected]> |
| 231 |
|
| 232 |
This library is free software; you may redistribute it and/or modify |
| 233 |
it under the same terms as Perl itself |
| 234 |
|
| 235 |
=cut |