Initial stab at a working ContentMangler.
Dan Brook [Sat, 15 May 2010 13:05:03 +0000 (14:05 +0100)]
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.

lib/Gitalist/ContentMangler/Resolver/Default.pm
lib/Gitalist/ContentMangler/Transformer/SyntaxHighlight.pm [new file with mode: 0644]
lib/Gitalist/Model/ContentMangler.pm
root/inc/syntax_highlight_css.tt2

index 607f4d4..29aeda4 100644 (file)
@@ -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};
     }
-}
\ No newline at end of file
+}
diff --git a/lib/Gitalist/ContentMangler/Transformer/SyntaxHighlight.pm b/lib/Gitalist/ContentMangler/Transformer/SyntaxHighlight.pm
new file mode 100644 (file)
index 0000000..8d53aaf
--- /dev/null
@@ -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);
+    }
+  }
+}
index 62a3356..6182362 100644 (file)
@@ -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;
index 71a132f..e710a44 100644 (file)
@@ -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 %]