The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
#
#  Compile and/or show compiled version of WebDyne HTML scripts
#
package main;


#  Compiler pragma
#
sub BEGIN {
    #  Massage warnings and @INC path
    $^W=0;
    use File::Spec;
    use FindBin qw($RealBin $Script);
    require File::Spec->catfile($RealBin, 'perl5lib.pl');
    perl5lib->import(File::Spec->catdir($RealBin, File::Spec->updir()));
};
use strict qw(vars);
use vars   qw($VERSION);


#  Use the base module
#
use WebDyne::Base;


#  Other external modules
#
use WebDyne;
use WebDyne::Request::Fake;
use Getopt::Long;
use Pod::Usage;


#  Version Info, must be all one line for MakeMaker, CPAN.
#
$VERSION='1.018';


#  Run main
#
exit ${ &main(\@ARGV) || die errdump() };

#===================================================================================================


sub main {


    #  Get argv array ref
    #
    my $argv_ar=shift();


    #  Defaul options
    #
    my %option=(

	handler    => $ENV{'WebDyneHandler'} || 'WebDyne',
	error	   => 'text',
	header     => 0,
	warn	   => 1,

       );


    #  Get command line options
    #
    GetOptions(\%option, 'help|?', 'handler=s', 'header!', 'warn!', 'error=s', 'man', 'version') ||
	pod2usage(2);
    pod2usage(-verbose => 99, -sections => 'Synopsis|Options', -exitval => 1) if $option{'help'};
    pod2usage(-verbose => 2) if $option{'man'};
    $option{'version'} && do { 
      print "$Script version: $VERSION\n"; 
      print "WebDyne version: $WebDyne::VERSION\n";
      exit 0 
    };


    #  Get srce file, add to options
    #
    my $srce_fn=shift(@{$argv_ar}) ||
	pod2usage("$Script: no source file specified !");
    (-f $srce_fn) ||
	pod2usage("$Script: input file not found !");


    #  Get new request object
    #
    my $r=WebDyne::Request::Fake->new(

	filename	=>  $srce_fn,

       ) || return err();


    #  Get handler
    #
    my $handler=$option{'handler'};


    #  Load up whichever handler we are using
    #
    eval ("require $handler") ||
	return err("$Script: unable to load handler $handler, $@");


    #  Set text errors only
    #
    $WebDyne::Err::WEBDYNE_ERROR_TEXT=1 if ($option{'error'} eq 'text');
    
    
    #  Set header, warning output
    #
    $r->notes('noheader', !$option{'header'});
    $r->notes('nowarn', !$option{'warn'});


    #  Run it and display results, or any error generated
    #
    defined($handler->handler($r)) || return err();


    #  Manual cleanup
    #
    $r->DESTROY();


    #  Done, return success
    #
    \undef;

}


__END__

=head1 Name

wdrender - parse and render WebDyne pages

=head1 Synopsis

B<wdrender> B<[OPTIONS]> B<FILE>

=head1 Options

-h, --help
Show brief help message.

-v, --version
Show version information.


=over 5

=item --handler

Use a different WebDyne handler module. Currently the only other
handler module available is WebDyne::Chain.

=back

=over 5

=item --header

Show HTTP status headers (default). User --noheader to disable.

=back

=over 5

=item --error=text[|html]

Show text error messages. User --error=html to HTML error message.

=back


=head1 Description

The B<wdrender> command displays the HTML that would be generated from a
psp page using the WebDyne perl module.

By default B<wdrender> will use the internal WebDyne handler when building
the output, but can optionally use other WebDyne modules (such as
WebDyne::Chain) by using the B<--handler> option..

=head1 Notes

The B<wdrender> command will attempt to build the HTML as faithfully as
possible from the command line environment, but may not be able to
exactly duplicate the HTML generated under a real Web Server. As an
example if a psp page takes advantge of the Apache request handler when
generating HTML, the B<wdrender> commend will not be able to duplicate
that environment.

=head1 Examples

B<wdrender> B<widget.psp>

Display the HTML that would be generated by WebDyne from the widget.psp
file.

B<wdrender> B<--handler=WebDyne::Chain> B<widget.psp>

Display the HTML that would be generated by WebDyne from the widget.psp
file, but this time using the WebDyne::Chain handler. In this case the
example would fail, as WebDyne::Chain expects to get a list of modules
it will chain the page throught. Normally it would get that from the
Web Server configuration file, but the WebDyne::Chain module can take
the list of modules from the environment also. So a better example
would be:

B<WebDyneChain=WebDyne::Session> B<wdrender> B<--handler=WebDyne::Chain>
B<widget.psp>

=head1 Author

Written by Andrew Speer, andrew@webdyne.org

=head1 Copying

Copyright (C) 2008-2010 Andrew Speer. Free use of this software is
granted under the terms of the GNU General Public License (GPL)