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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="perl script: project_to_html.pl" />
<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>