The blob action now has simple (but functioning) syntax highlighting (thanks to jrock...
[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 sub process {
12     my($self, $c) = @_;
13     # via
14     # http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
15     eval {
16         no warnings 'redefine';
17         local *Syntax::Highlight::Engine::Kate::Template::logwarning
18           = sub { die @_ }; # i really don't care
19         my $hl = Syntax::Highlight::Engine::Kate->new(
20             language      => 'Perl',
21             substitutions => {
22                 "<"  => "&lt;",
23                 ">"  => "&gt;",
24                 "&"  => "&amp;",
25                 q{'} => "&apos;",
26                 q{"} => "&quot;",
27             },
28             format_table => {
29                 # convert Kate's internal representation into
30                 # <span class="<internal name>"> value </span>
31                 map {
32                     $_ => [ qq{<span class="$_">}, '</span>' ]
33                 }
34                   qw/Alert BaseN BString Char Comment DataType
35                      DecVal Error Float Function IString Keyword
36                      Normal Operator Others RegionMarker Reserved
37                      String Variable Warning/,
38             },
39         );
40
41         $c->stash->{blob} = $hl->highlightText($c->stash->{blob});
42     };
43
44     warn $@ if $@;
45
46     $c->forward('View::Default');
47 }
48
49 1;