package Gitalist::View::SyntaxHighlight;
use Moose;
-use Gitalist; # ->path_to
use namespace::autoclean;
extends 'Catalyst::View';
use Syntax::Highlight::Engine::Kate ();
use Syntax::Highlight::Engine::Kate::Perl ();
+use HTML::Entities qw(encode_entities);
+
sub process {
my($self, $c) = @_;
- # via
- # http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
- 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',
- substitutions => {
- "<" => "<",
- ">" => ">",
- "&" => "&",
- q{'} => "'",
- q{"} => """,
- },
- format_table => {
- # convert Kate's internal representation into
- # <span class="<internal name>"> value </span>
- map {
- $_ => [ qq{<span class="$_">}, '</span>' ]
- }
- qw/Alert BaseN BString Char Comment DataType
- DecVal Error Float Function IString Keyword
- Normal Operator Others RegionMarker Reserved
- String Variable Warning/,
- },
- );
-
- $c->stash->{blob} = $hl->highlightText($c->stash->{blob});
- };
-
- warn $@ if $@;
-
- $c->forward('View::Default');
+
+ $c->res->body($self->render($c, $c->res->body, $c->stash));
}
-1;
+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 => $lang,
+ substitutions => {
+ "<" => "<",
+ ">" => ">",
+ "&" => "&",
+ q{'} => "'",
+ q{"} => """,
+ },
+ format_table => {
+ # convert Kate's internal representation into
+ # <span class="<internal name>"> value </span>
+ map {
+ $_ => [ qq{<span class="$_">}, '</span>' ]
+ }
+ qw/Alert BaseN BString Char Comment DataType
+ DecVal Error Float Function IString Keyword
+ Normal Operator Others RegionMarker Reserved
+ String Variable Warning/,
+ },
+ );
+
+ my $hltxt = $hl->highlightText($blob);
+ $hltxt =~ s/([^[:ascii:]])/encode_entities($1)/eg;
+ $hltxt;
+ };
+ warn $@ if $@;
+ }
+
+ 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