Merge remote branch 't0m/json' into json
[catagits/Gitalist.git] / lib / Gitalist / Controller / Root.pm
1 package Gitalist::Controller::Root;
2
3 use Moose;
4 use Moose::Autobox;
5 use Digest::MD5 qw(md5_hex);
6 use Gitalist::Utils qw/ age_string /;
7
8 use namespace::autoclean;
9
10 BEGIN { extends 'Gitalist::Controller' }
11
12 __PACKAGE__->config(namespace => '');
13
14 sub root : Chained('/') PathPart('') CaptureArgs(0) {}
15
16 sub index : Chained('base') PathPart('') Args(0) {
17     my ( $self, $c ) = @_;
18     $c->stash( search_text => $c->req->param('s') || '' ) # FIXME - XSS?
19 }
20
21 # XXX Fragile much?
22 sub css : Chained('/root') PathPart('core.css') Args(0) {
23     my ( $self, $c ) = @_;
24
25     $c->response->content_type('text/css');
26     $c->stash(template => 'static/css/core.css');
27 }
28
29 sub base : Chained('/root') PathPart('') CaptureArgs(0) {
30   my($self, $c) = @_;
31
32   my $git_version = `git --version`;
33   chomp($git_version);
34   $c->stash(
35     git_version => $git_version,
36     version     => $Gitalist::VERSION,
37
38     # XXX Move these to a plugin!
39     time_since => sub {
40       return 'never' unless $_[0];
41       return age_string(time - $_[0]->epoch);
42     },
43     short_cmt => sub {
44       my $cmt = shift;
45       my($line) = split /\n/, $cmt;
46       $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/ if defined $line;
47       return $line;
48     },
49     abridged_description => sub {
50         join(' ', grep { defined } (split / /, shift)[0..10]);
51     },
52     uri_for_gravatar => sub { # FIXME - Cache these?
53         my $email = shift;
54         my $size = shift;
55         my $uri = 'http://www.gravatar.com/avatar/' . md5_hex($email);
56         $uri .= "?s=$size" if $size;
57         return $uri;
58     },
59   );
60 }
61
62 sub end : ActionClass('Serialize') {
63     my ($self, $c) = @_;
64     # Give repository views the current HEAD.
65     if ($c->stash->{Repository}) {
66         $c->stash->{HEAD} = $c->stash->{Repository}->head_hash;
67     }
68     if ($c->stash->{data} && blessed $c->stash->{data}) {
69         $c->stash->{rest} = $c->stash->{data}->pack;
70     }
71 }
72
73 sub error_404 : Action {
74     my ($self, $c) = @_;
75     $c->response->status(404);
76     $c->response->body('Page not found');
77 }
78
79 __PACKAGE__->config(
80     default => 'text/html',
81     map => {
82         'text/html'        => [qw/ View Default /],
83         'application/json' => [qw/ JSON /],
84     }
85 );
86
87 __PACKAGE__->meta->make_immutable;
88
89 __END__
90
91 =head1 NAME
92
93 Gitalist::Controller::Root - Root controller for the application
94
95 =head1 DESCRIPTION
96
97 This controller handles all of the root level paths for the application
98
99 =head1 METHODS
100
101 =head2 root
102
103 Root of chained actions
104
105 =head2 base
106
107 Populate the header and footer. Perhaps not the best location.
108
109 =head2 index
110
111 Provides the repository listing.
112
113 =head2 end
114
115 Attempt to render a view, if needed.
116
117 =head2 error_404
118
119 =head1 AUTHORS
120
121 See L<Gitalist> for authors.
122
123 =head1 LICENSE
124
125 See L<Gitalist> for the license.
126
127 =cut