X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FGitalist%2FView%2FSyntaxHighlight.pm;h=de745abebb6645505900c63169149bee474e41b1;hb=a8f570133294adb58620440e75c2cca0d32e0934;hp=02845c669b109ef99f91d5c7ce32de2ef44c5cc6;hpb=1ef8dc7df4864b362033f4927652b04a84a7bfde;p=catagits%2FGitalist.git diff --git a/lib/Gitalist/View/SyntaxHighlight.pm b/lib/Gitalist/View/SyntaxHighlight.pm index 02845c6..de745ab 100644 --- a/lib/Gitalist/View/SyntaxHighlight.pm +++ b/lib/Gitalist/View/SyntaxHighlight.pm @@ -1,49 +1,90 @@ 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 - # value - map { - $_ => [ qq{}, '' ] - } - 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)); +} + +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 > 65536; + + 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 + # value + map { + $_ => [ qq{}, '' ] + } + 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 for authors. + +=head1 LICENSE + +See L for the license. + +=cut