/
AsHTML.pm6
195 lines (142 loc) · 5.49 KB
/
AsHTML.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use v6;
use MONKEY-TYPING;
unit class Backtrace::AsHTML;
my constant $P6HOME = $*VM.prefix ~ '/..';
augment class Backtrace {
method as-html(*%opt) returns Str {
return render(self, %opt);
}
};
my sub render(Backtrace $bt, %opt) returns Str {
my $traces = $bt.reverse;
my $msg = encode-html($traces[0].Str.chomp);
my $out = sprintf '<!doctype html><head><title>Error: %s</title>', $msg;
%opt<style> ||= q:heredoc/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
$out ~= sprintf '<style type="text/css">%s</style>', %opt<style>;
$out ~= sprintf q:heredoc/HEAD/, $msg;
<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;
}
function toggleArguments(ref) {
toggleThing(ref, 'arguments', 'Hide function arguments', 'Show function arguments');
}
function toggleLexicals(ref) {
toggleThing(ref, 'lexicals', 'Hide lexical variables', 'Show lexical variables');
}
</script>
</head>
<body>
<h1>Error trace</h1><pre class="message">%s</pre><ol>
HEAD
my $i = 0;
for @$traces -> Backtrace::Frame $frame {
$i++;
my Backtrace::Frame $next-frame = $traces[$i]; # peek next
$out ~= join(
'',
'<li class="frame">',
($next-frame && $next-frame.subname) ?? encode-html("in " ~ $next-frame.subname) !! '',
' at ',
$frame.file ?? encode-html($frame.file) !! '',
' line ',
$frame.line,
'<pre class="context"><code>',
build-context($frame) || '',
'</code></pre>',
'</li>',
);
}
$out ~= '</ol>';
$out ~= '</body></html>';
return $out;
}
my sub build-context(Backtrace::Frame $frame) returns Str {
my $file = $frame.file;
my $linenum = $frame.line;
unless $file.IO.f {
# maybe runtime file
$file = "$P6HOME/$file";
}
my Str $code;
if $file.IO.f {
my $start = $linenum - 3;
my $end = $linenum + 3;
$start = $start < 1 ?? 1 !! $start;
my $fh = try { open $file, :bin } or die "cannot open $file: $!";
my $cur-line = 0;
my @lines = $fh.lines;
for @lines -> $line {
++$cur-line;
last if $cur-line > $end;
next if $cur-line < $start;
(my $l = $line) ~~ s:global/\t/ /;
my @tag = $cur-line == $linenum ?? ['<strong class="match">', '</strong>']
!! ['', ''];
$code ~= sprintf "%s%5d: %s%s\n", @tag[0], $cur-line, encode-html($l), @tag[1];
}
CATCH {
default {
# probably read bin file...
$code = '';
}
}
$fh.close;
}
return $code;
}
my sub encode-html(Str $str) {
return $str.trans(
[ '&', '<', '>', '"', q{'} ] =>
[ '&', '<', '>', '"', ''' ]
);
}
=begin pod
=head1 NAME
Backtrace::AsHTML - Displays back trace in HTML
=head1 SYNOPSIS
use Backtrace::AsHTML;
my $trace = Backtrace.new;
my $html = $trace.as-html;
=head1 DESCRIPTION
Backtrace::AsHTML adds C<as-html> method to L<Backtrace> which displays the back trace
in beautiful HTML, with code snippet context.
<img src="https://i.gyazo.com/6ac7f82ef6fb0a05d7de9a11dbdcaa0b.png">
This library is inspired by L<Devel::StackTrace::AsHTML of perl5|https://metacpan.org/release/Devel-StackTrace-AsHTML> and much of code is taken from that.
=head1 METHODS
=item C<as-html>
C<as-html> shows the fully back trace in HTML.
This method will be added into L<Backtrace> class automatically when used this.
=head1 TODO
=item show lexical variables for each frames (How?)
=item show arguments for each frames? (How??)
=head1 AUTHOR
moznion <moznion@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright 2015 moznion
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
And license of the original perl5's Devel::StackTrace::AsHTML is
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=end pod