Apache2-PPI-HTML
view release on metacpan or search on metacpan
lib/Apache2/PPI/HTML.pm view on Meta::CPAN
package Apache2::PPI::HTML;
=pod
=head1 NAME
Apache2::PPI::HTML - Apache 2 interface to PPI::HTML
=head1 DESCRIPTION
L<PPI::HTML> is a HTML syntax highlighter for Perl source code. Because
it is based on L<PPI> it can correctly parse just about anything you can
possibly throw at it, and then flexibly generate a HTML version based on
any arbitrary colour scheme you wish, or use a standalone CSS file for
it's style information.
This initial version is primarily intended to serve as a highlighting
service to a AJAX-based syntax highlighter.
The handler recieves a chunk of Perl source code from the browser in
the CGI C<'code'> parameter, and a second C<'ajax'> param determines if
the highlighter is running in AJAX mode.
In AJAX mode, the fragment of formatted HTML is handed back straight up,
without any surrounding body or headers or the other parts of the HTML
wrapper.
With AJAX mode off, it returns a complete HTML document.
With this initial version, you may have some difficulties getting CSS
actually working.
We recommend you wait for the next release, which will support a range
of pre-packaged colour schemes being created for L<PPI::HTML>.
=cut
use 5.006;
use strict;
use warnings;
use PPI;
use PPI::HTML;
use CGI;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw( OK SERVER_ERROR );
use vars qw{$VERSION};
BEGIN {
$VERSION = '0.02';
}
sub handler {
# The request object
my $r = shift;
# Long-winded way of getting the user's data...
my $cgi = CGI->new( $r );
my $data = $cgi->param('code');
# Turn the user's data into HTML
my $document = PPI::Document->new( \$data )
or return Apache2::Const::OK;
my $highlight;
if ( $cgi->param('ajax') ) {
$highlight = PPI::HTML->new();
} else {
$highlight = PPI::HTML->new( page => 1 );
}
my $html = $highlight->html( $document )
or return Apache2::Const::SERVER_ERROR;
( run in 0.645 second using v1.01-cache-2.11-cpan-39bf76dae61 )