| 1 | package Apache2::PPI::HTML; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Apache2::PPI::HTML - Apache 2 interface to PPI::HTML | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | L<PPI::HTML> is a HTML syntax highlighter for Perl source code. Because | ||
| 12 | it is based on L<PPI> it can correctly parse just about anything you can | ||
| 13 | possibly throw at it, and then flexibly generate a HTML version based on | ||
| 14 | any arbitrary colour scheme you wish, or use a standalone CSS file for | ||
| 15 | it's style information. | ||
| 16 | |||
| 17 | This initial version is primarily intended to serve as a highlighting | ||
| 18 | service to a AJAX-based syntax highlighter. | ||
| 19 | |||
| 20 | The handler recieves a chunk of Perl source code from the browser in | ||
| 21 | the CGI C<'code'> parameter, and a second C<'ajax'> param determines if | ||
| 22 | the highlighter is running in AJAX mode. | ||
| 23 | |||
| 24 | In AJAX mode, the fragment of formatted HTML is handed back straight up, | ||
| 25 | without any surrounding body or headers or the other parts of the HTML | ||
| 26 | wrapper. | ||
| 27 | |||
| 28 | With AJAX mode off, it returns a complete HTML document. | ||
| 29 | |||
| 30 | With this initial version, you may have some difficulties getting CSS | ||
| 31 | actually working. | ||
| 32 | |||
| 33 | We recommend you wait for the next release, which will support a range | ||
| 34 | of pre-packaged colour schemes being created for L<PPI::HTML>. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | use 5.006; | ||
| 39 | use strict; | ||
| 40 | use warnings; | ||
| 41 | use PPI; | ||
| 42 | use PPI::HTML; | ||
| 43 | use CGI; | ||
| 44 | use Apache2::RequestRec (); | ||
| 45 | use Apache2::RequestIO (); | ||
| 46 | use Apache2::Const -compile => qw( OK SERVER_ERROR ); | ||
| 47 | |||
| 48 | use vars qw{$VERSION}; | ||
| 49 | BEGIN { | ||
| 50 | $VERSION = '0.02'; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub handler { | ||
| 54 | # The request object | ||
| 55 | my $r = shift; | ||
| 56 | |||
| 57 | # Long-winded way of getting the user's data... | ||
| 58 | my $cgi = CGI->new( $r ); | ||
| 59 | my $data = $cgi->param('code'); | ||
| 60 | |||
| 61 | # Turn the user's data into HTML | ||
| 62 | my $document = PPI::Document->new( \$data ) | ||
| 63 | or return Apache2::Const::OK; | ||
| 64 | |||
| 65 | my $highlight; | ||
| 66 | if ( $cgi->param('ajax') ) { | ||
| 67 | $highlight = PPI::HTML->new(); | ||
| 68 | } else { | ||
| 69 | $highlight = PPI::HTML->new( page => 1 ); | ||
| 70 | } | ||
| 71 | |||
| 72 | my $html = $highlight->html( $document ) | ||
| 73 | or return Apache2::Const::SERVER_ERROR; | ||
| 74 | |||
| 75 | # Return to the user | ||
| 76 | $r->content_type('text/html'); | ||
| 77 | $r->print( $html ); | ||
| 78 | return Apache2::Const::OK; | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | =pod | ||
| 84 | |||
| 85 | =head1 TO DO | ||
| 86 | |||
| 87 | - Add support for L<PPI::HTML> 0.06 colour schemes. | ||
| 88 | |||
| 89 | - Add support for acting as a content filter. | ||
| 90 | |||
| 91 | If you like the idea of this module, and would like to volunteer | ||
| 92 | to help maintain or improve it, your assistance you be greatly | ||
| 93 | welcomed. | ||
| 94 | |||
| 95 | Inquiries via the L<PPI> dicussion mailing list, located on the | ||
| 96 | SourceForge website. L<http://sourceforge.net/projects/parseperl>. | ||
| 97 | |||
| 98 | =head1 SUPPORT | ||
| 99 | |||
| 100 | Bugs should be reported via the CPAN bug tracker at | ||
| 101 | |||
| 102 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Apache2-PPI-HTML> | ||
| 103 | |||
| 104 | For other issues, contact the author. | ||
| 105 | |||
| 106 | =head1 AUTHOR | ||
| 107 | |||
| 108 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 109 | |||
| 110 | Written by Pete Sergeant (in about 3 minutes) :) | ||
| 111 | |||
| 112 | =head1 COPYRIGHT | ||
| 113 | |||
| 114 | Copyright 2005 - 2008 Pete Sergeant, Adam Kennedy. | ||
| 115 | |||
| 116 | This program is free software; you can redistribute | ||
| 117 | it and/or modify it under the same terms as Perl itself. | ||
| 118 | |||
| 119 | The full text of the license can be found in the | ||
| 120 | LICENSE file included with this module. | ||
| 121 | |||
| 122 | =cut |