Skip to content

Commit

Permalink
Initial stab at a working ContentMangler.
Browse files Browse the repository at this point in the history
The idea is that the Resolver will figure out which Transformer will do
the mangling. There can only be one Transformer presently, mostly so I
don't get lost in a sea of complications. Much follow up work is needed
and ideally another mangler e.g POD.
  • Loading branch information
Dan Brook committed Aug 1, 2010
1 parent d23c396 commit 52990a5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
11 changes: 7 additions & 4 deletions lib/Gitalist/ContentMangler/Resolver/Default.pm
Expand Up @@ -2,8 +2,11 @@ use MooseX::Declare;

class Gitalist::ContentMangler::Resolver::Default with Gitalist::ContentMangler::Resolver {
method resolve ($data) {
return unless $data->{filename};
my $language = 'Perl' if $data->{filename} =~ /\.p[lm]$/i;
return (['SyntaxHighlight', {language => $language, css => $language}]);
# This should be pulled out of $self->config
my $language;
$language = 'Perl' if $data->{filename} =~ /\.p[lm]$/i;
$language = 'Diff' if $data->{action} eq 'diff_fancy';
return unless $language;
return 'Gitalist::ContentMangler::Transformer::SyntaxHighlight' => {language => $language, css => $language};
}
}
}
13 changes: 13 additions & 0 deletions lib/Gitalist/ContentMangler/Transformer/SyntaxHighlight.pm
@@ -0,0 +1,13 @@
use MooseX::Declare;

class Gitalist::ContentMangler::Transformer::SyntaxHighlight {
method transform($c, $config) {
$c->stash(
syntax_css => $c->uri_for("/static/css/syntax/$config->{css}.css"),
mangled => 1,
);
for (grep $_, $c->stash->{blobs} ? @{$c->stash->{blobs}} : $c->stash->{blob}) {
$_ = $c->view('SyntaxHighlight')->render($c, $_, $config);
}
}
}
33 changes: 17 additions & 16 deletions lib/Gitalist/Model/ContentMangler.pm
Expand Up @@ -40,22 +40,23 @@ has _resolver => (
# We need to support multiple languages, and we also want to be able to do HTMLizing (for e.g. Pod)

sub process {
my ($self, $c) = @_;

my @steps = $self->resolve({ filename => $c->stash->{filename} });
my @css = map { $_->[1]->{css} } grep { exists $_->[1] && exists $_->[1]->{css} && defined $_->[1]->{css} && length $_->[1]->{css} } @steps;
$c->stash(
syntax_css => [ map { $c->uri_for('/static/css/syntax/' . $_ . '.css') } @css ],
mangled => scalar @steps,
);

if ($c->stash->{blobs} || $c->stash->{blob}) {
foreach my $step (@steps) {
for ($c->stash->{blobs} ? @{$c->stash->{blobs}} : $c->stash->{blob}) {
$_ = $c->view($step->[0])->render($c, $_, $step->[1]);
}
}
}
my ($self, $c) = @_;

# Find appropriate mangler based on filename,action,config
# Mangler should provide a transform e.g what part of the stash to mangle
# Then call the transform with the appropriate mangling

my($transformer, $config) = $self->resolve({
filename => $c->stash->{filename} || '',
config => Gitalist->config->{'Model::ContentMangler'},
action => $c->action->name,
});

return
unless $transformer;

Class::MOP::load_class($transformer);
$transformer->new($config)->transform($c, $config);
}

__PACKAGE__->meta->make_immutable;
3 changes: 1 addition & 2 deletions root/inc/syntax_highlight_css.tt2
@@ -1,2 +1 @@
[%- IF language == 'Diff' %]<link rel="stylesheet" type="text/css" href="[% c.uri_for('/static/css/syntax/Diff.css') %]"/>[% END -%]
[%- IF language == 'Perl' %]<link rel="stylesheet" type="text/css" href="[% c.uri_for('/static/css/syntax/Perl.css') %]"/>[% END -%]
[% IF syntax_css %]<link rel="stylesheet" type="text/css" href="[% syntax_css %]"/>[% END %]

0 comments on commit 52990a5

Please sign in to comment.