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