Tidied up the /blob action and the commit-nav.tt2 links.
[catagits/Gitalist.git] / lib / Gitalist / View / SyntaxHighlight.pm
CommitLineData
7e54e579 1package Gitalist::View::SyntaxHighlight;
2use Moose;
3use Gitalist; # ->path_to
4use namespace::autoclean;
5
6extends 'Catalyst::View';
7
8use Syntax::Highlight::Engine::Kate ();
9use Syntax::Highlight::Engine::Kate::Perl ();
10
c8870bd3 11use HTML::Entities qw(encode_entities);
12
7e54e579 13sub process {
14 my($self, $c) = @_;
c8870bd3 15
16 if($c->stash->{filename} =~ /\.p[lm]$/) {
17 # via
18 # http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
19 eval {
20 no warnings 'redefine';
21 local *Syntax::Highlight::Engine::Kate::Template::logwarning
22 = sub { die @_ }; # i really don't care
23 my $hl = Syntax::Highlight::Engine::Kate->new(
24 language => 'Perl',
25 substitutions => {
26 "<" => "&lt;",
27 ">" => "&gt;",
28 "&" => "&amp;",
29 q{'} => "&apos;",
30 q{"} => "&quot;",
31 },
32 format_table => {
33 # convert Kate's internal representation into
34 # <span class="<internal name>"> value </span>
35 map {
36 $_ => [ qq{<span class="$_">}, '</span>' ]
37 }
38 qw/Alert BaseN BString Char Comment DataType
39 DecVal Error Float Function IString Keyword
40 Normal Operator Others RegionMarker Reserved
41 String Variable Warning/,
42 },
43 );
44
45 $c->stash->{blob} = $hl->highlightText($c->stash->{blob});
46 };
47 warn $@ if $@;
48 } else {
49 $c->stash->{blob} = encode_entities($c->stash->{blob});
50 }
7e54e579 51
52 $c->forward('View::Default');
53}
54
1ef8dc7d 55__PACKAGE__->meta->make_immutable;