Devel-StackTrace-AsHTML
view release on metacpan or search on metacpan
lib/Devel/StackTrace/AsHTML.pm view on Meta::CPAN
package Devel::StackTrace::AsHTML;
use strict;
use 5.008_001;
our $VERSION = '0.15';
use Data::Dumper;
use Devel::StackTrace;
use Scalar::Util;
no warnings 'qw';
my %enc = qw( & & > > < < " " ' ' );
# NOTE: because we don't know which encoding $str is in, or even if
# $str is a wide character (decoded strings), we just leave the low
# bits, including latin-1 range and encode everything higher as HTML
# entities. I know this is NOT always correct, but should mostly work
# in case $str is encoded in utf-8 bytes or wide chars. This is a
# necessary workaround since we're rendering someone else's code which
# we can't enforce string encodings.
sub encode_html {
my $str = shift;
$str =~ s/([^\x00-\x21\x23-\x25\x28-\x3b\x3d\x3f-\xff])/$enc{$1} || '&#' . ord($1) . ';' /ge;
utf8::downgrade($str);
$str;
}
sub Devel::StackTrace::as_html {
__PACKAGE__->render(@_);
}
sub render {
my $class = shift;
my $trace = shift;
my %opt = @_;
my $msg = encode_html($trace->frame(0)->as_string(1));
my $out = qq{<!doctype html><head><title>Error: ${msg}</title>};
$opt{style} ||= \<<STYLE;
a.toggle { color: #444 }
body { margin: 0; padding: 0; background: #fff; color: #000; }
h1 { margin: 0 0 .5em; padding: .25em .5em .1em 1.5em; border-bottom: thick solid #002; background: #444; color: #eee; font-size: x-large; }
pre.message { margin: .5em 1em; }
li.frame { font-size: small; margin-top: 3em }
li.frame:nth-child(1) { margin-top: 0 }
pre.context { border: 1px solid #aaa; padding: 0.2em 0; background: #fff; color: #444; font-size: medium; }
pre .match { color: #000;background-color: #f99; font-weight: bold }
pre.vardump { margin:0 }
pre code strong { color: #000; background: #f88; }
table.lexicals, table.arguments { border-collapse: collapse }
table.lexicals td, table.arguments td { border: 1px solid #000; margin: 0; padding: .3em }
table.lexicals tr:nth-child(2n) { background: #DDDDFF }
table.arguments tr:nth-child(2n) { background: #DDFFDD }
.lexicals, .arguments { display: none }
.variable, .value { font-family: monospace; white-space: pre }
td.variable { vertical-align: top }
STYLE
if (ref $opt{style}) {
$out .= qq(<style type="text/css">${$opt{style}}</style>);
} else {
$out .= qq(<link rel="stylesheet" type="text/css" href=") . encode_html($opt{style}) . q(" />);
}
$out .= <<HEAD;
<script language="JavaScript" type="text/javascript">
function toggleThing(ref, type, hideMsg, showMsg) {
var css = document.getElementById(type+'-'+ref).style;
css.display = css.display == 'block' ? 'none' : 'block';
var hyperlink = document.getElementById('toggle-'+ref);
hyperlink.textContent = css.display == 'block' ? hideMsg : showMsg;
( run in 2.134 seconds using v1.01-cache-2.11-cpan-e1769b4cff6 )