Syntax higlighting improvements
[catagits/Gitalist.git] / lib / Gitalist / View / SyntaxHighlight.pm
1 package Gitalist::View::SyntaxHighlight;
2 use Moose;
3 use namespace::autoclean;
4
5 extends 'Catalyst::View';
6
7 use Syntax::Highlight::Engine::Kate ();
8
9 use HTML::Entities qw(encode_entities);
10
11 sub process {
12     my($self, $c) = @_;
13
14     $c->res->body($self->render($c, $c->res->body, $c->stash));
15 }
16
17 sub render {
18     my ($self, $c, $blob, $args) = @_;
19
20     # Don't bother with anything over 64kb, it'll be tragically slow.
21     return encode_entities $blob if length $blob > 65536;
22
23     my $lang = $args->{language};
24
25     my $ret;
26     if($lang) {
27         # via http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
28         $ret = eval {
29             no warnings 'redefine';
30             local *Syntax::Highlight::Engine::Kate::Template::logwarning
31               = sub { die @_ }; # i really don't care
32             my $hl = Syntax::Highlight::Engine::Kate->new(
33                 language      => $lang,
34                 substitutions => {
35                     "<"  => "&lt;",
36                     ">"  => "&gt;",
37                     "&"  => "&amp;",
38                     q{'} => "&apos;",
39                     q{"} => "&quot;",
40                 },
41                 format_table => {
42                     # convert Kate's internal representation into
43                     # <span class="<internal name>"> value </span>
44                     map {
45                         $_ => [ qq{<span class="$_">}, '</span>' ]
46                     }
47                       qw/Alert BaseN BString Char Comment DataType
48                          DecVal Error Float Function IString Keyword
49                          Normal Operator Others RegionMarker Reserved
50                          String Variable Warning/,
51                 },
52             );
53
54             my $hltxt = $hl->highlightText($blob);
55             $hltxt =~ s/([^[:ascii:]])/encode_entities($1)/eg;
56             $hltxt;
57         };
58         warn $@ if $@;
59     }
60
61     return $ret || encode_entities($blob);
62 }
63
64 __PACKAGE__->meta->make_immutable;
65
66 __END__
67
68 =head1 NAME
69
70 Gitalist::View::SyntaxHighlight - Responsible for syntax highlighting code
71
72 =head1 DESCRIPTION
73
74 Catalyst View for Syntax highlighting.
75
76 =head1 METHODS
77
78 =head2 process
79
80 =head2 highlight
81
82 =head1 AUTHORS
83
84 See L<Gitalist> for authors.
85
86 =head1 LICENSE
87
88 See L<Gitalist> for the license.
89
90 =cut