version 0.0007
Dancer2::Plugin::SyntaxHighlight::Perl - Generate pretty HTML from Perl code in a Dancer2 app
This module provides on-the-fly conversion of Perl to syntax-highlighted HTML. For convenience it adds the keywords highlight_perl and highlight_output to the Dancer2 DSL.
highlight_perl
highlight_output
plugins: 'SyntaxHighlight::Perl': line_numbers: 1
get '/perl_tutorial' => sub { return template 'perl_tutorial', { example_code => highlight_perl('/path/to/file.pl'), example_output => highlight_output('/path/to/file.txt'), }; };
<div style="white-space: pre-wrap"> [% example_code %] </div>
Or:
<div> <pre>[%example_code %]</pre> </div>
Takes as input the full pathname of a file, or a filehandle, or a reference to a scalar. Expects what it is given to contain Perl code.
Outputs Perl code as HTML with syntax highlighting, in the form of <span></span> tags, with the appropriate class names, around the elements of the Perl code after it has been parsed by PPI.
<span></span>
PPI
If line_numbers is set to true in the Dancer2 config, the output will have line numbers.
line_numbers
For more details on the format of the ouput, see PPI::HTML, or examine the files in the examples/ directory in this distribution.
PPI::HTML
examples/
You will need to provide the CSS for the styling, see examples/ for examples.
Important: This module removes the <BR> tags from the end of the generated HTML lines, so you must enclose the HTML in either <pre></pre> tags or an element with style="white-space: pre-wrap">.
<BR>
<pre></pre>
style="white-space: pre-wrap"
You can override this transformation by setting skip_postprocessing to true in the Dancer2 config.
skip_postprocessing
Often when showing Perl code you will want to show also the output of the code, This function adds very simple highlighting to the saved output of Perl code.
Takes as input the full pathname of a file, or a filehandle, or a reference to a scalar.
Outputs the content with the first line wrapped in a <span></span> tag with the special class prompt, and all other with the class word.
prompt
word
This generated HTML also must be enclosed in either <pre></pre> tags or an element with style="white-space: pre-wrap" (or set skip_postprocessing to true in the Dancer2 config).
PPI, PPI::HTML
Nick Tonkin <tonkin@cpan.org>
This software is copyright (c) 2017 by Nick Tonkin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Dancer2::Plugin::SyntaxHighlight::Perl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::SyntaxHighlight::Perl
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::SyntaxHighlight::Perl
For more information on module installation, please visit the detailed CPAN module installation guide.