From 25bc6d8f99de5ca2241e0411882b0e527003a87a Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 23 Sep 2013 00:25:07 +0100 Subject: [PATCH] remove blank lines from CSV responses --- Netdisco/lib/App/Netdisco/Web.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Netdisco/lib/App/Netdisco/Web.pm b/Netdisco/lib/App/Netdisco/Web.pm index 757f52ec..fdf3c4f4 100644 --- a/Netdisco/lib/App/Netdisco/Web.pm +++ b/Netdisco/lib/App/Netdisco/Web.pm @@ -68,6 +68,23 @@ hook 'before_template' => sub { $Template::Stash::PRIVATE = undef; }; +# remove empty lines from CSV response +# this makes writing templates much more straightforward! +hook 'after' => sub { + my $r = shift; # a Dancer::Response + + if ($r->content_type and $r->content_type eq 'text/comma-separated-values') { + my @newlines = (); + my @lines = split m/\n/, $r->content; + + foreach my $line (@lines) { + push @newlines, $line if $line !~ m/^\s*$/; + } + + $r->content(join "\n", @newlines); + } +}; + get qr{^/(?:login(?:/denied)?)?} => sub { template 'index'; };