Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: perlcode to html with syntax highlighting

by neniro (Priest)
on May 07, 2007 at 05:15 UTC ( #613888=note: print w/replies, xml ) Need Help??

in reply to perlcode to html with syntax highlighting

Hello Holli, It's really cool! But PPI::HTML already exists. Here is a small example using it, that works a lot like your example:
#!/usr/bin/perl use strict; use warnings; use HTML::Template; use HTML::Entities; use PPI; use PPI::HTML; use Data::Dumper; use MIME::Base64; die "script needs at least one file to process!\n" unless @ARGV; my @content; my $template = HTML::Template->new(filehandle => *DATA); foreach my $source (@ARGV) { my ($type) = $source =~ /\.(.*)$/; my $part; open(FILE, '<', $source) or die "dieing while trying to open $source cause of: $!\n"; my $native = join '', (<FILE>); if ($type eq 'pl' or $type eq 'pm') { my $perldoc = PPI::Document->new( \$native ); my $highlight = PPI::HTML->new(); $part = '<pre>' . $highlight->html( $perldoc ) . '</ +pre>'; } elsif ($type eq 'png') { $part = '<div class="embimg"><img src="data:image/png;base64, +'; $part .= encode_base64($native) . '" alt=""/></div>'; } else { $part = '<pre>' . encode_entities($native) . '</pre>'; } close(FILE); push @content, { source => $source, content => $part, type => $type, }; } $template->param( content => \@content, ); print $template->output(); __DATA__ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ""> <html xmlns=""> <head> <meta name="generator" content="perl script:" /> <title>Documentation</title> <style type="text/css" media="screen"> /* <![CDATA[ */ h1 { color: #ff9; } a { color: #99f; } pre { border-width: 1px; border-style: solid; padding: 4px; background-color: #001; color: #ddd; line-height: 0.65em; } strong { color: #f99; } .default { color: #fff; background-color: #444; font-family: Arial, Helvectia; margin: 8px; } .embimg { border-width: 1px; border-style: solid; border-color: #000; background-color: #fff; margin-top: 4px; } .comment { color: #6600dd; } .keyword { color: #ff9900; } .pragma { text-style: italic; } .substitute { color: #eeee00; } .operator { color: #6666ff; } .single { color: #0c0; } .double { color: #3c3; } .symbol { color: #d66; } .structure {color: #ccc;} .word {color: #fff;} /* ]]> */ </style> </head> <body class="default"> <TMPL_LOOP NAME="content"> <h1><TMPL_VAR NAME="source"></h1> Type of file is <strong><TMPL_VAR NAME="type"></strong><br /> <TMPL_VAR NAME="content"><br /> <hr /> </TMPL_LOOP> </body> </html>

Replies are listed 'Best First'.
Re^2: perlcode to html with syntax highlighting
by holli (Abbot) on May 07, 2007 at 15:08 UTC
    I was aware of PPI::Html. As said the script is little more than a finger exercise. What I like about this solution is that you can tweak each and everything. You want double quoted strings that say foo in pink? Just say
    [Token::Quote::Double] *default = color:blue; "foo" = color:pink;

    holli, /regexed monk/

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://613888]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2022-12-01 16:30 GMT
Find Nodes?
    Voting Booth?