Expose serialization via JSON.
[catagits/Gitalist.git] / lib / Gitalist / Controller / Root.pm
index 78d26f2..ddcc83a 100644 (file)
 package Gitalist::Controller::Root;
+
 use Moose;
+use Moose::Autobox;
+use Digest::MD5 qw(md5_hex);
+use Gitalist::Utils qw/ age_string /;
+
 use namespace::autoclean;
 
-BEGIN { extends 'Catalyst::Controller' }
+BEGIN { extends 'Gitalist::Controller' }
+
+__PACKAGE__->config(namespace => '');
+
+sub root : Chained('/') PathPart('') CaptureArgs(0) {}
+
+sub index : Chained('base') PathPart('') Args(0) {
+    my ( $self, $c ) = @_;
+    $c->stash( search_text => $c->req->param('s') || '' ) # FIXME - XSS?
+}
+
+# XXX Fragile much?
+sub css : Chained('/root') PathPart('core.css') Args(0) {
+    my ( $self, $c ) = @_;
+
+    $c->response->content_type('text/css');
+    $c->stash(template => 'static/css/core.css');
+}
+
+sub base : Chained('/root') PathPart('') CaptureArgs(0) {
+  my($self, $c) = @_;
+
+  my $git_version = `git --version`;
+  chomp($git_version);
+  $c->stash(
+    git_version => $git_version,
+    version     => $Gitalist::VERSION,
+
+    time_since => sub {
+      return 'never' unless $_[0];
+      return age_string(time - $_[0]->epoch);
+    },
+    short_cmt => sub {
+      my $cmt = shift;
+      my($line) = split /\n/, $cmt;
+      $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/ if defined $line;
+      return $line;
+    },
+    abridged_description => sub {
+        join(' ', grep { defined } (split / /, shift)[0..10]);
+    },
+    uri_for_gravatar => sub {
+        my $email = shift;
+        my $size = shift;
+        my $uri = 'http://www.gravatar.com/avatar/' . md5_hex($email);
+        $uri .= "?s=$size" if $size;
+        return $uri;
+    },
+  );
+}
+
+sub search : Chained('base') Args(0) {}
+
+=head2 search_help
+
+Provides some help for the search form.
+
+=cut
+
+sub search_help : Chained('base') Args(0) {}
+
+sub end : ActionClass('Serialize') {
+    my ($self, $c) = @_;
+    # Give repository views the current HEAD.
+    if ($c->stash->{Repository}) {
+        $c->stash->{HEAD} = $c->stash->{Repository}->head_hash;
+    }
+    if ($c->stash->{data} && blessed $c->stash->{data}) {
+        $c->stash->{rest} = $c->stash->{data}->pack;
+    }
+}
 
-#
-# Sets the actions in this controller to be registered with no prefix
-# so they function identically to actions created in MyApp.pm
-#
-__PACKAGE__->config->{namespace} = '';
+sub error_404 : Action {
+    my ($self, $c) = @_;
+    $c->response->status(404);
+    $c->response->body('Page not found');
+}
+
+__PACKAGE__->config(
+    default => 'text/html',
+    map => {
+        'application/json' => [qw/ JSON /],
+        map { $_ => [qw/ View Default /] }
+             qw( text/css text/html text/plain
+                 application/atom+xml application/rss+xml application/rss )
+    }
+);
+
+__PACKAGE__->meta->make_immutable;
+
+__END__
 
 =head1 NAME
 
-Gitalist::Controller::Root - Root Controller for Gitalist
+Gitalist::Controller::Root - Root controller for the application
 
 =head1 DESCRIPTION
 
-[enter your description here]
+This controller handles all of the root level paths for the application
 
 =head1 METHODS
 
-=cut
+=head2 root
 
-=head2 index
+Root of chained actions
 
-=cut
+=head2 base
 
-use IO::Capture::Stdout;
-use File::Slurp qw(slurp);
-
-sub default :Path {
-  my ( $self, $c ) = @_;
-
-  my $capture = IO::Capture::Stdout->new();
-  $capture->start();
-  eval {
-    my $action = gitweb::main($c);
-    $action->();
-  };
-  $capture->stop();
-
-  gitweb::git_header_html();
-  gitweb::git_footer_html();
-  my $output = join '', $capture->read;
-  $c->stash->{content} = $output
-    unless $c->stash->{content};
-  $c->stash->{template} = 'default.tt2';
-}
+Populate the header and footer. Perhaps not the best location.
 
-sub index :Path :Args(0) {
-    my ( $self, $c ) = @_;
+=head2 index
 
-  my $order = $c->req->param('order');
-  if($order && $order !~ m/none|project|descr|owner|age/) {
-    die "Unknown order parameter";
-  }
-
-  my @list = $c->model('Git')->projects;
-  if (!@list) {
-    die "No projects found";
-  }
-
-  if (-f $c->config->{home_text}) {
-    print "<div class=\"index_include\">\n";
-    print slurp($c->config->{home_text});
-    print "</div>\n";
-  }
-
-  my $cgi;
-  print $cgi->startform(-method => "get") .
-    "<p class=\"projsearch\">Search:\n" .
-    $cgi->textfield(-name => "s", -value => $c->req->param('searchtext')) . "\n" .
-    "</p>" .
-    $cgi->end_form() . "\n";
-
-  git_project_list_body(\@list, $order);
-}
+Provides the repository listing.
 
 =head2 end
 
 Attempt to render a view, if needed.
 
-=cut
-
-sub end : ActionClass('RenderView') {}
+=head2 error_404
 
-=head1 AUTHOR
+=head1 AUTHORS
 
-Dan Brook,,,
+See L<Gitalist> for authors.
 
 =head1 LICENSE
 
-This library is free software. You can redistribute it and/or modify
-it under the same terms as Perl itself.
+See L<Gitalist> for the license.
 
 =cut
-
-__PACKAGE__->meta->make_immutable;