Turn the syntax highlighter into a standard view with a render method
[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
225f5690 12# What should be done, but isn't currently:
13#
14# broquaint> Another Cat question - if I want to have arbitrary things highlighted is pushing things through a View at all costs terribly wrong?
15# broquaint> e.g modifying this slightly to highlight anything (or arrays of anything) http://github.com/broquaint/Gitalist/blob/a7cc1ede5f9729465bb53da9c3a8b300a3aa8a0a/lib/Gitalist/View/SyntaxHighlight.pm
16# t0m> no, that's totally fine.. I'd tend to push the rendering logic into a model, so you end up doing something like: $c->model('SyntaxDriver')->highlight_all($stuff, $c->view('SyntaxHighlight'));
17# broquaint> I'm thinking it's a bad idea because the Controller needs to munge data such that the View knows what to do
18# broquaint> You just blew my mind ;)
19# t0m> ^^ That works _much_ better if you split up your view methods into process & render..
20# t0m> ala TT..
21# t0m> i.e. I'd have 'highlight this scalar' as the ->render method in the view..
22# t0m> And then the 'default' thing (i.e. process method) will do that and shove the output in the body..
23# t0m> but then you can write foreach my $thing (@things) { push(@highlighted_things, $c->view('SyntaxHighlight')->render($thing)); }
24# t0m> and then I'd move that ^^ loop down into a model which actually knows about / abstracts walking the data structures concerned..
25# t0m> But splitting render and process is the most important bit.. :) Otherwise you need to jump through hoops to render things that don't fit 'nicely' into the bits of stash / body that the view uses by 'default'
26# t0m> I wouldn't kill you for putting the structure walking code in the view given you're walking simple arrays / hashes.. It becomes more important if you have a more complex visitor..
27# t0m> (I use Visitor in the design patterns sense)
28# t0m> As the visitor is responsible for walking the structure, delegating to the ->render call in the view which is responsible for actually mangling the content..
f5da8e7a 29
7e54e579 30sub process {
31 my($self, $c) = @_;
c8870bd3 32
f5da8e7a 33 for($c->stash->{blobs} ? @{$c->stash->{blobs}} : $c->stash->{blob}) {
1b72203c 34 $_ = $self->render($c, $_, { language => $c->stash->{language} });
f5da8e7a 35 }
36
37 $c->forward('View::Default');
38}
39
1b72203c 40sub render {
41 my ($self, $c, $blob, $args) = @_;
42
43 my $lang = $args->{language};
f5da8e7a 44
6cf4366a 45 my $ret;
f5da8e7a 46 if($lang) {
6cf4366a 47 # via http://github.com/jrockway/angerwhale/blob/master/lib/Angerwhale/Format/Pod.pm#L136
48 $ret = eval {
c8870bd3 49 no warnings 'redefine';
50 local *Syntax::Highlight::Engine::Kate::Template::logwarning
51 = sub { die @_ }; # i really don't care
52 my $hl = Syntax::Highlight::Engine::Kate->new(
f5da8e7a 53 language => $lang,
c8870bd3 54 substitutions => {
55 "<" => "&lt;",
56 ">" => "&gt;",
57 "&" => "&amp;",
58 q{'} => "&apos;",
59 q{"} => "&quot;",
60 },
61 format_table => {
62 # convert Kate's internal representation into
63 # <span class="<internal name>"> value </span>
64 map {
65 $_ => [ qq{<span class="$_">}, '</span>' ]
66 }
67 qw/Alert BaseN BString Char Comment DataType
68 DecVal Error Float Function IString Keyword
69 Normal Operator Others RegionMarker Reserved
70 String Variable Warning/,
71 },
72 );
73
4621ecf0 74 my $hltxt = $hl->highlightText($blob);
75 $hltxt =~ s/([^[:ascii:]])/encode_entities($1)/eg;
76 $hltxt;
c8870bd3 77 };
78 warn $@ if $@;
c8870bd3 79 }
6cf4366a 80
81 return $ret || encode_entities($blob);
7e54e579 82}
83
1ef8dc7d 84__PACKAGE__->meta->make_immutable;
775e96e0 85
86__END__
87
88=head1 NAME
89
90Gitalist::View::SyntaxHighlight - Responsible for syntax highlighting code
91
92=head1 DESCRIPTION
93
94Catalyst View for Syntax highlighting.
95
d137f7d5 96=head1 METHODS
97
98=head2 process
99
100=head2 highlight
101
775e96e0 102=head1 AUTHORS
103
104See L<Gitalist> for authors.
105
106=head1 LICENSE
107
108See L<Gitalist> for the license.
109
110=cut