631a71eb97f4f32957164abeeb22c6331969c614
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize / YAML / HTML.pm
1 package Catalyst::Action::Serialize::YAML::HTML;
2
3 use Moose;
4 use namespace::autoclean;
5
6 extends 'Catalyst::Action';
7 use YAML::Syck;
8 use URI::Find;
9
10 # VERSION
11
12 sub execute {
13     my $self = shift;
14     my ( $controller, $c ) = @_;
15
16     my $stash_key = (
17             $controller->{'serialize'} ?
18                 $controller->{'serialize'}->{'stash_key'} :
19                 $controller->{'stash_key'} 
20         ) || 'rest';
21     my $app = $c->config->{'name'} || '';
22     my $output = "<html>";
23     $output .= "<title>" . $app . "</title>";
24     $output .= "<body><pre>";
25     my $text = HTML::Entities::encode(Dump($c->stash->{$stash_key}));
26     # Straight from URI::Find
27     my $finder = URI::Find->new(
28                               sub {
29                                   my($uri, $orig_uri) = @_;
30                                   my $newuri;
31                                   if ($uri =~ /\?/) {
32                                       $newuri = $uri . "&content-type=text/html";
33                                   } else {
34                                       $newuri = $uri . "?content-type=text/html";
35                                   }
36                                   return qq|<a href="$newuri">$orig_uri</a>|;
37                               });
38     $finder->find(\$text);
39     $output .= $text;
40     $output .= "</pre>";
41     $output .= "</body>";
42     $output .= "</html>";
43     $c->response->output( $output );
44     return 1;
45 }
46
47 __PACKAGE__->meta->make_immutable;
48
49 1;