package Gitalist::View::SyntaxHighlight;
use Moose;
-use Gitalist; # ->path_to
use namespace::autoclean;
extends 'Catalyst::View';
sub process {
my($self, $c) = @_;
- if($c->stash->{filename} =~ /\.p[lm]$/) {
- # via
- # http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
- eval {
+ $c->res->body($self->render($c, $c->res->body, $c->stash));
+}
+
+sub render {
+ my ($self, $c, $blob, $args) = @_;
+
+ # Don't bother with anything over 64kb, it'll be tragically slow.
+ return encode_entities $blob if length $blob > 8192;
+
+ my $lang = $args->{language};
+
+ my $ret;
+ if($lang) {
+ # via http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
+ $ret = eval {
no warnings 'redefine';
local *Syntax::Highlight::Engine::Kate::Template::logwarning
= sub { die @_ }; # i really don't care
my $hl = Syntax::Highlight::Engine::Kate->new(
- language => 'Perl',
+ language => $lang,
substitutions => {
"<" => "<",
">" => ">",
},
);
- $c->stash->{blob} = $hl->highlightText($c->stash->{blob});
+ my $hltxt = $hl->highlightText($blob);
+ $hltxt =~ s/([^[:ascii:]])/encode_entities($1)/eg;
+ $hltxt;
};
warn $@ if $@;
- } else {
- $c->stash->{blob} = encode_entities($c->stash->{blob});
}
- $c->forward('View::Default');
+ return $ret || encode_entities($blob);
}
__PACKAGE__->meta->make_immutable;
+
+__END__
+
+=head1 NAME
+
+Gitalist::View::SyntaxHighlight - Responsible for syntax highlighting code
+
+=head1 DESCRIPTION
+
+Catalyst View for Syntax highlighting.
+
+=head1 METHODS
+
+=head2 process
+
+=head2 highlight
+
+=head1 AUTHORS
+
+See L<Gitalist> for authors.
+
+=head1 LICENSE
+
+See L<Gitalist> for the license.
+
+=cut