The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#! /usr/local/bin/perl -ws

use strict;
use warnings;
use Carp;

our $toc;   # was the -toc flag specified on the command-line?

use Perl6::Perldoc::Parser;
use Perl6::Perldoc::To::Xhtml;

my $source_file = $ARGV[0] || \*STDIN;

sub enhanced_text {
    my ($parent, $text) = @_;

    for ($text) {
        s{&}{&}gxms;
        s{<}{&lt;}gxms;
        s{>}{&gt;}gxms;

        last if $parent->is_verbatim;

        s{``}{&ldquo;}gxms;
        s{''}{&rdquo;}gxms;
        s{`}{&lsquo;}gxms;
        s{(?<=\S)'}{&rsquo;}gxms;
        s{'(?=\S)}{&lsquo;}gxms;
        s{(?<=\S)"}{&rdquo;}gxms;
        s{"(?=\S)}{&ldquo;}gxms;
    }

    return $text;
}

# Add in this CSS so that tables look nicer...
my $CSS = <<'END_CSS';
    th, td  { padding-right: 2em; }

    th p    { margin: 3pt;
              text-align: left;
              border-bottom: thin solid;
            }

    td p    { margin: 3pt; }
END_CSS

# Handle -toc requests by prepending a =TOC section with a P<toc:...> link
if ($toc) {
    if (!ref $source_file) {
        my $filename = $source_file;
        undef $source_file;
        open $source_file, '<', $filename
            or croak "Couldn't open file '$filename': $!";
    }
    my $source = "=TOC\nP<toc:head1 head2 head3>\n\n"
               . do { local $/; <$source_file> };
    $source_file = \$source;
}

print Perl6::Perldoc::Parser->parse( $source_file, {all_pod=>'auto'} )
                            ->report_errors()
                            ->to_xhtml({ text_to_entities => \&enhanced_text,
                                         full_doc => { style=>$CSS },
                                      });

__END__

=head1 NAME

perldoc2xhtml_enhanced - Convert Perldoc markup to XHTML

=head1 VERSION

This documentation refers to perldoc2xhtml_enhanced version 0.0.1
This version is still a prototype.

=head1 USAGE

    perldoc2xhtml_enhanced [-toc] [filename]

=head1 REQUIRED ARGUMENTS

None.

=head1 OPTIONS

May be given a single argument specifying the name of the file to convert.
If filename not given, input is read from STDIN.

May also be given a C<-toc> flag to autgenerate a table-of-contents at
the start of the document.

=head1 DESCRIPTION

This utility converts documentation in the Perl 6 "Perldoc" notation to
XHTML. It is enhanced in that it recognizes constructs like:

    ``Open the pod-bay door, Hal!''

    I'm sorry Dave, I'm afraid I can't do that. 

    It's called `Daisy'.

    "Daisy, Daisy, give me your answer, do..."

in non-verbatim text and "educates" the quote marks. That is, it
converts the quote marks to:

    &ldquo;Open the pod-bay door, Hal!&rdquo;

    I&rsquo;m sorry Dave, I&rsquo;m afraid I can&rsquo;t do that. 

    It&rsquo;s called &lsquo;Daisy&rsquo;.

    &ldquo;Daisy, Daisy, give me your answer, do...&rdquo;


The XHTML is always written to STDOUT.


=head1 DIAGNOSTICS

See the documentation for Perl6::Perldoc::Parser


=head1 CONFIGURATION AND ENVIRONMENT

Requires no configuration files or environment variables.


=head1 DEPENDENCIES

Perl6::Perldoc::Parser


=head1 BUGS

None reported.
Bug reports and other feedback are most welcome.


=head1 AUTHOR

Damian Conway C<< DCONWAY@cpan.org >>


=head1 COPYRIGHT

Copyright (c) 2007, Damian Conway C<< <DCONWAY@cpan.org> >>. All rights reserved.

This module is free software. It may be used, redistributed
and/or modified under the terms of the Perl Artistic License
(see http://www.perl.com/perl/misc/Artistic.html)


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.