Add JSON
authorMarius Gavrilescu <marius@ieval.ro>
Sun, 1 Jun 2014 08:42:56 +0000 (11:42 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Sun, 1 Jun 2014 08:42:56 +0000 (11:42 +0300)
lib/App/MusicExpo.pm
t/musicexpo-cache.t
t/musicexpo.t

index 60c5c087156164daec33bcb3d09cdb3b13537052..32715ae1680671c029729fa65b99f926670be995 100644 (file)
@@ -15,6 +15,7 @@ use DB_File qw//;
 use File::Basename qw/fileparse/;
 use Fcntl qw/O_RDWR O_CREAT/;
 use Getopt::Long;
+use JSON;
 use Storable qw/thaw freeze/;
 
 ##################################################
@@ -128,7 +129,10 @@ sub run {
                push @files, \%entry
        }
 
-       $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files], prefix => $prefix);
+       my $json = JSON->new->utf8->canonical->encode({files => \@files, prefix => $prefix});
+       $json =~ s/</&lt;/g;
+       $json =~ s/>/&gt;/g;
+       $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files], prefix => $prefix, json => $json);
        print $ht->output;
 }
 
@@ -142,8 +146,10 @@ $default_template = <<'HTML';
 <thead>
 <tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
 <tbody><tmpl_loop files>
-<tr><td><a href=<tmpl_var title><td><tmpl_var artist><td><tmpl_var album><td><tmpl_var genre><td><tmpl_var tracknumber>/<tmpl_var tracktotal><td><tmpl_var year><td><tmpl_loop formats><a href="<tmpl_var ...prefix><tmpl_var ESCAPE=URL file>"><tmpl_var format></a> </tmpl_loop></tmpl_loop>
+<tr><td><tmpl_var title><td><tmpl_var artist><td><tmpl_var album><td><tmpl_var genre><td><tmpl_var tracknumber>/<tmpl_var tracktotal><td><tmpl_var year><td><tmpl_loop formats><a href="<tmpl_var ...prefix><tmpl_var ESCAPE=URL file>"><tmpl_var format></a> </tmpl_loop></tmpl_loop>
 </table>
+
+<pre id="json" style="display: none"><tmpl_var ESCAPE=0 json></pre>
 HTML
 
 1;
index 1f464ed112570eaad1656d2cfde928e3822a321e..ac4afc4f05b014555a96480e2b8c78905b1fc506 100644 (file)
@@ -32,8 +32,10 @@ is $out, <<'OUT', 'output is correct';
 <thead>
 <tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
 <tbody>
-<tr><td><a href=Cellule<td>Silence<td>L&#39;autre endroit<td>Electro<td>01/09<td>2005<td><a href="/music/empty.flac">FLAC</a> <a href="/music/empty.mp3">MP3</a> 
+<tr><td>Cellule<td>Silence<td>L&#39;autre endroit<td>Electro<td>01/09<td>2005<td><a href="/music/empty.flac">FLAC</a> <a href="/music/empty.mp3">MP3</a> 
 </table>
+
+<pre id="json" style="display: none">{"files":[{"album":"L'autre endroit","artist":"Silence","file":"empty.flac","format":"FLAC","formats":[{"file":"empty.flac","format":"FLAC"},{"file":"empty.mp3","format":"MP3"}],"genre":"Electro","title":"Cellule","tracknumber":"01","tracktotal":"09","year":"2005"}],"prefix":"/music/"}</pre>
 OUT
 
 ok -e $file, 'cache exists';
index fcd4e69c833b3a8266c5c34f2296f82cb43a6604..919a59cdcba87bab3f6857e29fca464e2c8a0db9 100644 (file)
@@ -25,4 +25,6 @@ is $out, <<'OUT', 'output is correct';
 <tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
 <tbody>
 </table>
+
+<pre id="json" style="display: none">{"files":[],"prefix":"/music/"}</pre>
 OUT
This page took 0.01429 seconds and 4 git commands to generate.