File Coverage

blib/lib/Data/iRealPro/Output/Forum.pm
Criterion Covered Total %
statement 69 87 79.3
branch 10 28 35.7
condition 7 15 46.6
subroutine 14 14 100.0
pod 0 3 0.0
total 100 147 68.0


line stmt bran cond sub pod time code
1             #! perl
2              
3             # Data::iRealPro::Output::Forum -- produce iRealPro forum posting
4              
5             ################ Common stuff ################
6              
7 1     1   1225 use strict;
  1         3  
  1         29  
8 1     1   5 use warnings;
  1         1  
  1         24  
9 1     1   5 use Carp;
  1         2  
  1         45  
10 1     1   6 use utf8;
  1         2  
  1         5  
11              
12             package Data::iRealPro::Output::Forum;
13              
14 1     1   57 use parent qw( Data::iRealPro::Output::Base );
  1         2  
  1         8  
15              
16 1     1   43 use Data::iRealPro::URI;
  1         3  
  1         18  
17 1     1   5 use Data::iRealPro::Playlist;
  1         2  
  1         21  
18 1     1   5 use Encode qw( encode_utf8 );
  1         2  
  1         38  
19 1     1   6 use HTML::Entities;
  1         2  
  1         923  
20              
21             my $regtest = $ENV{IRP_REGTEST};
22              
23             sub options {
24 1     1 0 3 my $self = shift;
25 1         2 [ @{ $self->SUPER::options }, qw( split ) ];
  1         6  
26             }
27              
28             sub process {
29 2     2 0 1807 my ( $self, $u, $options ) = @_;
30              
31 2         4 my $html = "";
32 2   50     14 $self->{output} //= $options->{output} || "-";
      66        
33 2   66     10 $self->{split} //= $options->{split};
34              
35 2 100       6 unless ( $self->{split} ) {
36              
37 1 50       4 if ( $u->{transpose} ) {
38 0         0 foreach my $song ( @{ $u->{playlist}->{songs} } ) {
  0         0  
39             # Do not change key to actual.
40 0         0 local $song->{_transpose} = 0;
41              
42 0   0     0 $song->{key} = $song->xpose($song->{key} // "C");
43 0 0       0 if ( $song->{actual_key} ne '' ) {
44             $song->{actual_key} =
45 0         0 ( $song->{actual_key} + $song->{transpose} ) % 12;
46             }
47 0         0 $song->tokenize;
48 0 0       0 $song->{data} = $song->{dataxp} if $song->{dataxp};
49             }
50             }
51              
52 1         4 $html .= to_html($u);
53             }
54             else {
55 1         3 foreach my $song ( @{ $u->{playlist}->{songs} } ) {
  1         4  
56             # Do not change key to actual.
57 1         3 local $song->{_transpose} = 0;
58              
59 1 50       5 if ( $song->{transpose} ) {
60 0   0     0 $song->{key} = $song->xpose($song->{key} // "C");
61 0 0       0 if ( $song->{actual_key} ne '' ) {
62             $song->{actual_key} =
63 0         0 ( $song->{actual_key} + $song->{transpose} ) % 12;
64             }
65 0         0 $song->tokenize;
66 0 0       0 $song->{data} = $song->{dataxp} if $song->{dataxp};
67             }
68              
69             # Make a playlist with just this song.
70 1         4 my $pls = Data::iRealPro::Playlist->new( song => $song );
71              
72             # Make an URI for this playlist.
73 1         5 my $uri = Data::iRealPro::URI->new( playlist => $pls );
74              
75             # Write it out.
76 1         4 $html .= to_html($uri);
77             }
78             }
79              
80 2 50       9 if ( ref( $self->{output} ) ) {
81 2         4 ${ $self->{output} } = $html;
  2         8  
82             }
83             else {
84 0         0 my $fd;
85 0 0       0 if ( $self->{output} eq "-" ) {
86 0         0 $fd = \*STDOUT;
87             }
88             else {
89             open( $fd, ">:utf8", $self->{output} )
90 0 0       0 or croak( "Cannot create ", $self->{output}, " [$!]\n" );
91             }
92 0         0 print $fd $html;
93             }
94             }
95              
96             sub to_html {
97 2     2 0 5 my ( $u ) = @_;
98              
99 2         3 my $pl = $u->{playlist};
100 2         5 my $html = "";
101              
102 2 100 66     8 if ( $pl->{name} || @{ $pl->{songs} } > 1 ) {
  1         3  
103 1         6 $html .= "[URL=\"irealb://" . _esc($pl->as_string) .
104             "\"]All songs[/URL]";
105             $html .= " - " . _html($pl->{name})
106 1 50       9 if $pl->{name};
107 1         22 $html .= "\n";
108              
109 1         2 $html .= "[LIST=1]\n";
110 1         2 foreach my $s ( @{ $pl->{songs} } ) {
  1         4  
111 1         4 my @c = split(' ', $s->{composer});
112 1 50       5 my $c = @c == 2 ? "$c[1] $c[0]" : $s->{composer};
113             $html .= "[*][URL=\"irealb://" .
114             _esc($s->as_string) .
115 1         5 "\"]". _html($s->{title}) .
116             "[/URL] - " .
117             _html($c) .
118             "\n";
119             }
120 1         27 $html .= "[/LIST]\n";
121             }
122             else {
123 1         2 my $s = $pl->{songs}->[0];
124 1         5 my @c = split(' ', $s->{composer});
125 1 50       4 my $c = @c == 2 ? "$c[1] $c[0]" : $s->{composer};
126             $html .= "[URL=\"irealb://" . _esc($s->as_string) .
127 1         5 "\"]" . _html($s->{title}) .
128             "[/URL] - " .
129             _html($c) .
130             "\n";
131             }
132              
133 2         24 return $html;
134             }
135              
136             sub _esc {
137 3     3   14 goto \&Data::iRealPro::URI::esc;
138             }
139              
140             sub _html {
141 5     5   42 encode_entities($_[0]);
142             }
143              
144             1;