Expose serialization via 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     time_since => sub {
39       return 'never' unless $_[0];
40       return age_string(time - $_[0]->epoch);
41     },
42     short_cmt => sub {
43       my $cmt = shift;
44       my($line) = split /\n/, $cmt;
45       $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/ if defined $line;
46       return $line;
47     },
48     abridged_description => sub {
49         join(' ', grep { defined } (split / /, shift)[0..10]);
50     },
51     uri_for_gravatar => sub {
52         my $email = shift;
53         my $size = shift;
54         my $uri = 'http://www.gravatar.com/avatar/' . md5_hex($email);
55         $uri .= "?s=$size" if $size;
56         return $uri;
57     },
58   );
59 }
60
61 sub search : Chained('base') Args(0) {}
62
63 =head2 search_help
64
65 Provides some help for the search form.
66
67 =cut
68
69 sub search_help : Chained('base') Args(0) {}
70
71 sub end : ActionClass('Serialize') {
72     my ($self, $c) = @_;
73     # Give repository views the current HEAD.
74     if ($c->stash->{Repository}) {
75         $c->stash->{HEAD} = $c->stash->{Repository}->head_hash;
76     }
77     if ($c->stash->{data} && blessed $c->stash->{data}) {
78         $c->stash->{rest} = $c->stash->{data}->pack;
79     }
80 }
81
82 sub error_404 : Action {
83     my ($self, $c) = @_;
84     $c->response->status(404);
85     $c->response->body('Page not found');
86 }
87
88 __PACKAGE__->config(
89     default => 'text/html',
90     map => {
91         'application/json' => [qw/ JSON /],
92         map { $_ => [qw/ View Default /] }
93              qw( text/css text/html text/plain
94                  application/atom+xml application/rss+xml application/rss )
95     }
96 );
97
98 __PACKAGE__->meta->make_immutable;
99
100 __END__
101
102 =head1 NAME
103
104 Gitalist::Controller::Root - Root controller for the application
105
106 =head1 DESCRIPTION
107
108 This controller handles all of the root level paths for the application
109
110 =head1 METHODS
111
112 =head2 root
113
114 Root of chained actions
115
116 =head2 base
117
118 Populate the header and footer. Perhaps not the best location.
119
120 =head2 index
121
122 Provides the repository listing.
123
124 =head2 end
125
126 Attempt to render a view, if needed.
127
128 =head2 error_404
129
130 =head1 AUTHORS
131
132 See L<Gitalist> for authors.
133
134 =head1 LICENSE
135
136 See L<Gitalist> for the license.
137
138 =cut