Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 236 lines (170 sloc) 6.48 KB
#!/usr/bin/env perl
use strict;
use warnings;
our $VERSION = '0.03';
use Getopt::Long;
use Pod::2::DocBook 0.03;
use Pod::Usage;
use List::MoreUtils 'none';
use CPAN::Version;
exit main();
sub main {
my $doctype = 'section';
my $spaces = 2;
my ($title, $double_quotes, $help, $no_header, $base_id, $id_version, $parse_title, $skip, $min_version);
'doctype=s' => \$doctype,
'base-id=s' => \$base_id,
'title=s' => \$title,
'spaces=i' => \$spaces,
'fix-double-quotes' => \$double_quotes,
'no-header' => \$no_header,
'help' => \$help,
'id-version=i' => \$id_version,
'parse-title' => \$parse_title,
'skip=s' => \$skip,
'min-version=s' => \$min_version,
) or pod2usage();
pod2usage if $help;
pod2usage if none { $doctype eq $_ } qw(article chapter refentry section);
pod2usage unless $spaces =~ /^\d+$/xms;
die 'version "'.$min_version.'" required, this is just "'.$VERSION.'"'."\n"
if ($min_version and CPAN::Version->vlt($VERSION, $min_version));
# TODO refactor this if so that there are no the same patterns in each of
# the branch
if (@ARGV == 0) {
$title = q{} unless defined $title;
my $parser = Pod::2::DocBook->new(
title => $title,
doctype => $doctype,
base_id => $base_id,
fix_double_quotes => $double_quotes,
header => !$no_header,
spaces => $spaces,
id_version => $id_version,
skip => $skip,
else {
my ($infile, $outfile) = @ARGV;
# default doctype element id is the name of the input file
$base_id ||= $infile;
$title = parse_title($infile)
if $parse_title;
($title) = $infile =~ m{([^/]+)\z}xms unless defined $title;
my $parser = Pod::2::DocBook->new(
title => $title,
doctype => $doctype,
base_id => $base_id,
fix_double_quotes => $double_quotes,
header => !$no_header,
spaces => $spaces,
id_version => $id_version,
skip => $skip,
if (defined $outfile) {
$parser->parse_from_file($infile, $outfile);
else {
return 0;
sub parse_title {
my $filename = shift;
open(my $fh, '<', $filename) or die 'failed to open "'.$filename.'"';
my $in_name = 0;
my $line;
while ($line = <$fh>) {
# skip empty lines
next if $line =~ m/^\s*$/;
# we have the title, it's the first text line inside "=head1 NAME"
if ($in_name);
# found "=head1 NAME"
$in_name = 1
if $line =~ m/^=head1 \s+ NAME/xmsi;
# remove formating codes from title and xml characters
$line =~ s/[IBCLEFSXZ]<(.*)>/$1/xmsg;
$line =~ s/&/&amp;/;
$line =~ s/</&lt;/;
$line =~ s/>/&gt;/;
return $line
if $line !~ m/^[^-]+-\s+(.+)$/;
return $1;
=head1 NAME
pod2docbook - Convert POD data to DocBook SGML
pod2docbook [B<--help>]
[B<--title>=I<title>] S<[B<--spaces>=I<# spaces per indent level>]>
[B<--fix-double-quotes>] [B<--no-header>]
S<[I<infile> [I<outfile>]]>
B<pod2docbook> converts files from pod format (see L<perlpod>) to
DocBook 4.2 SGML (see L<>). The program itself
is merely a driver for the Pod::2::DocBook class; if you're interested in
details of pod-to-SGML translation see L<Pod::2::DocBook>.
=item [B<--help>]
Print usage and exit.
=item [B<--doctype>=B<article>|B<chapter>|B<section>|B<refentry>]
Specifies the document type for the output file; the default is
=item [B<--title>=I<title>]
Specifies the document title. The default is I<infile>, if it is
supplied, or empty string otherwise.
=item [B<--spaces>=I<# spaces per indent level>]
Specifies the number of spaces per indent level in the SGML output;
the default is 2.
=item [B<--fix-double-quotes>]
Replace pairs of double quotes in regular paragraphs with <quote> and
</quote> (see L<Pod::2::DocBook> for details).
=item [B<--no-header>]
Omit the DOCTYPE line from the output.
=item I<infile>
The name of the file from which to read pod source; if not supplied,
STDIN is used for input.
=item I<outfile>
The name of the file to which to write SGML; if not supplied, STDOUT
is used for output.
=item [B<--base-id>=I<idstring>]
The root element of the B<--doctype> will have the I<idstring> set for attribute I<id>.
The default is an input file name "cleaned up" to conform with XML restriction for
characteds used in id strings. (SEE L<>)
=head1 SEE ALSO
L<pod2docbook>, L<perlpod>, L<Pod::DocBook>,
SVN repo - L<>,
F<doc/> + F<examples/pod2docbook-docbook/> for Pod::2::DocBook
DocBook documentation
DocBook related links: L<>,
=head1 AUTHOR
Alligator Descartes <> wrote a module called
Pod::2::DocBook, which was later maintained by Jan Iven
<>. That module was based on the original L<pod2html>
by Tom Christiansen <>.
Nandu Shah <> wrote Pod::DocBook, which is
unrelated to the previous module (even though they both perform the
same function). (L<>)
Jozef Kutej <> renamed the module to Pod::2::DocBook
because Nandus version was buried in the CPAN archive as an
Copyright 2004, Nandu Shah <>
Copyright 2008, Jozef Kutej <>
This library is free software; you may redistribute it and/or modify
it under the same terms as Perl itself