Generate syntax-hightlighted html for perl using ppi
use PPI; use PPI::HTML; # Load your Perl file my $Document = PPI::Document->load( 'script.pl' ); # Create a reusable syntax highlighter my $Highlight = PPI::HTML->new( line_numbers => 1 ); # Spit out the HTML print $Highlight->html( $Document );
\s-1PPI::HTML\s0 converts Perl documents into syntax highlighted \s-1HTML\s0 pages.
\s-1PPI::HTML\s0 is the successor to the now-redundant PPI::Format::HTML.
While early on it was thought that the same formatting code might be able to be used for a variety of different types of things (\s-1ANSI\s0 and \s-1HTML\s0 for example) later developments with the here-doc code and the need for independantly written serializers meant that this idea had to be discarded.
In addition, the old module only made use of the Tokenizer, and had a pretty shit \s-1API\s0 to boot.
The new module is much cleaner. Simply create an object with the options you want, pass PPI::Document objects to the \*(C`html\*(C' method, and you get strings of \s-1HTML\s0 that you can do whatever you want with.
The \*(C`new\*(C' constructor takes a simple set of key/value pairs to define the formatting options for the \s-1HTML\s0.
Is the \*(C`page\*(C' option is enabled, the generator will wrap the generated \s-1HTML\s0 fragment in a basic but complete page.
At the present time, the only option available. If set to true, line numbers are added to the output.
For cases where you don't want to use an external stylesheet, you can provide \*(C`colors\*(C' as a hash reference where the keys are \s-1CSS\s0 classes (generally matching the token name) and the values are colours. This allows basic colouring without the need for a whole stylesheet.
The \*(C`css\*(C' option lets you provide a custom CSS::Tiny object containing any \s-1CSS\s0 you want to apply to the page (if you are using page mode). If both the \*(C`colors\*(C' and \*(C`css\*(C' options are used, the colour \s-1CSS\s0 entries will overwrite anything contained in the CSS::Tiny object. The object will also be cloned if it to be modified, to prevent destroying any \s-1CSS\s0 objects passed in.
Returns a new \s-1PPI::HTML\s0 object
The \*(C`css\*(C' accessor returns the CSS::Tiny object originally provided to the constructor. The main method for the class, the \*(C`html\*(C' method takes a single PPI::Document object, or anything that can be turned into a PPI::Document via its \*(C`new\*(C' method, and returns a string of \s-1HTML\s0 formatted based on the arguments given to the \*(C`PPI::HTML\*(C' constructor.
Returns a string, or \*(C`undef\*(C' on error.
Bugs should always be submitted via the \s-1CPAN\s0 bug tracker
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PPI-HTML>
For other issues, contact the maintainer
Adam Kennedy <[email protected]>
Funding provided by The Perl Foundation
<http://ali.as/>, \s-1PPI\s0
Copyright 2005 - 2009 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the \s-1LICENSE\s0 file included with this module.