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