Tidied up the /blob action and the commit-nav.tt2 links.
[catagits/Gitalist.git] / lib / Gitalist / View / SyntaxHighlight.pm
1 package Gitalist::View::SyntaxHighlight;
2 use Moose;
3 use Gitalist; # ->path_to
4 use namespace::autoclean;
5
6 extends 'Catalyst::View';
7
8 use Syntax::Highlight::Engine::Kate ();
9 use Syntax::Highlight::Engine::Kate::Perl ();
10
11 use HTML::Entities qw(encode_entities);
12
13 sub process {
14     my($self, $c) = @_;
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     }
51
52     $c->forward('View::Default');
53 }
54
55 __PACKAGE__->meta->make_immutable;