blib/lib/Plack/Middleware/iPhone.pm

filter: Statementstmt 81.7% 67 / 82 Branchbran 50.0% 14 / 28 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod 40.0% 2 / 5 totaltotal 69.7% 97 / 139 CC 21 SCAR 22.7
CC
21
Cov
71%
write_manifest
20.4
filter
15.8
__ANON__
3.0
CRAP 9.7
↑ Summary
1 #
2 # This file is part of Plack-Middleware-iPhone
3 #
4 # This software is copyright (c) 2010 by Patrick Donelan.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Plack::Middleware::iPhone;
10 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $Plack::Middleware::iPhone::VERSION = '1.102060';
12 }
13  
14 # ABSTRACT: Make your html more iPhone friendly
15  
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use parent qw( Plack::Middleware );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Plack::Util::Accessor qw( manifest icon startup_image tidy viewport statusbar );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
22 1 my $class = shift;
23 1 my $self = $class->SUPER::new(@_);
24  
25 1 ▶ $self->write_manifest if $self->manifest;
Branch: 1 branch
BranchTrueFalseTotal
if $self->manifest 0 1 1
26  
27 1 return $self;
28 }
29  
30 1 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
31 1 my $self = shift;
32 1 my $env = shift;
33 1 my $res = $self->app->($env);
34  
35 # Buffer the entire html response (surely there's a better way..)
36 1 my $whole_response = '';
37  
38 $self->response_cb(
39 $res,
40 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
41 1 my $res = shift;
42 1 my $h = Plack::Util::headers( $res->[1] );
43  
44 1 ▶ if ( $h->get('Content-Type') =~ m!^text/html! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->get("Content-Type") =~ m[^text/html]) 1 0 1
45 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 2 my $chunk = shift;
47 2 ▶ return unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 1 1 2
48  
49 1 $whole_response .= $chunk;
50 1 ▶ if ( $chunk =~ m{</html>}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ m[</html>]i) 1 0 1
51 1 return $self->filter($whole_response);
52 }
53 1 };
54 }
55 }
56 1 );
57 }
58  
59 1 ▶sub filter {
Subroutine filter: called CC=12 SCAR=27.6
Pod undocumented
60 1 my $self = shift;
61 1 my $chunk = shift;
62 1 require HTML::DOM;
63 1 ▶ my $dom = new HTML::DOM or return $chunk;
Branch: 1 branch
BranchTrueFalseTotal
my $dom = "HTML::DOM"->new or return $chunk 1 0 1
64 1 $dom->write($chunk);
65 1 $dom->close;
66  
67 1 ▶ if ( my $manifest = $self->manifest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $manifest = $self->manifest) 0 1 1
68 0 $dom->documentElement->setAttribute( 'manifest', $manifest );
69 }
70  
71 1 my $head = $dom->getElementsByTagName('head')->[0];
72 1 ▶ my @meta = (
Condition50%2/4
$self->viewport || 'width = device-width'
l!l
01
$self->statusbar || 'gray'
l!l
01
MC/DC0%0/1
$self->viewport || 'width = device-width'
$self->viewport
MC/DC0%0/1
$self->statusbar || 'gray'
$self->statusbar
Truth table50%1/2
A
A= $self->viewport
Aresult
11
00
Truth table50%1/2
A
A= $self->statusbar
Aresult
11
00
73 [ name => 'viewport', content => $self->viewport || 'width = device-width' ],
74 [ name => 'apple-mobile-web-app-capable', content => 'yes' ],
75 [ name => 'apple-mobile-web-app-status-bar-style', content => $self->statusbar || 'gray' ],
76 );
77 1 for my $attrs (@meta) {
78 3 $head->appendChild( $self->el( $dom, 'meta', @$attrs ) );
79 }
80  
81 0 my %rel_links = map { $_->rel => 1 } $head->getElementsByTagName('link');
82 1 my @links;
83 1 ▶ push @links, { rel => "apple-touch-icon", href => $self->icon } if $self->icon;
Branch: 1 branch
BranchTrueFalseTotal
if $self->icon 1 0 1
84 1 ▶ push @links, { rel => "apple-touch-startup-image", href => $self->startup_image } if $self->startup_image;
Branch: 1 branch
BranchTrueFalseTotal
if $self->startup_image 1 0 1
85  
86 1 for my $link_attrs (@links) {
87 2 ▶ if ( $rel_links{ $link_attrs->{rel} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_links{$link_attrs->{'rel'}}) { } 0 2 2
88 0 warn "$link_attrs->{rel} link already exists";
89 }
90 else {
91 2 $head->appendChild( $self->el( $dom, 'link', %$link_attrs ) );
92 }
93 }
94  
95 1 my $html = $dom->innerHTML;
96  
97 1 ▶ if ( $self->tidy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tidy) 1 0 1
98 1 require UNIVERSAL::require;
99 1 ▶ if ("HTML::Tidy"->require) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HTML::Tidy'->require) { } 0 1 1
100 0 my $tidy = HTML::Tidy->new( { output_html => 1, indent => 'auto', tidy_mark => 'no' } );
101 0 $html = $tidy->clean($html);
102 } else {
103 1 warn "HTML::Tidy not available"
104 }
105 }
106  
107 1 return $html;
108 }
109  
110 5 ▶sub el {
Subroutine el: called CC=2 SCAR=6.9
Pod undocumented
111 5 my $self = shift;
112 5 my $dom = shift;
113 5 my $type = shift;
114 5 my @attrs = @_;
115 5 my $el = $dom->createElement($type);
116 5 ▶ while ( my ( $attr, $val ) = splice @attrs, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@attrs, 0, 2)) 10 5 15
117 10 $el->$attr($val);
118 }
119 5 return $el;
120 }
121  
122 0 ▶sub write_manifest {
Subroutine write_manifest: not called CC=5 SCAR=30.1
Pod undocumented
123 0 my $self = shift;
124  
125 0 require Digest::MD5;
126 0 require File::Slurp;
127 0 my $manifest = $self->manifest;
128  
129 # Write the manifest once, at compile time
130 0 ▶ open my $fh, '>', $manifest or die "Unable to write manifest $manifest. $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $manifest or die "Unable to write manifest $manifest. $!" 0 0 0
131 0 $fh->print("CACHE MANIFEST\n");
132 0 ▶ for my $file (<*.*>) {
Subroutine BEGIN: called CC=1 SCAR=0.2
133  
134 # Don't put manifest or app.psgi in manifest file
135 0 ▶ next if $file eq $manifest or $file =~ m/\.psgi$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $manifest or $file =~ /\.psgi$/ 0 0 0
Condition0%0/3
$file eq $manifest or $file =~ /\.psgi$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq $manifest or $file =~ /\.psgi$/
$file eq $manifest $file =~ /\.psgi$/
Truth table0%0/3
A or B
A= $file eq $manifestB= $file =~ /\.psgi$/
ABresult
1X1
011
000
136  
137 # Write MD5 hash so that manifest changes whenever files change (auto cache updating)
138 0 $fh->print("$file #");
139 0 $fh->print( Digest::MD5::md5_hex( File::Slurp::read_file($file) ) . "\n" );
140 }
141 }
142  
143 2 1;
144  
145  
146  
147 =pod
148  
149 =head1 NAME
150  
151 Plack::Middleware::iPhone - Make your html more iPhone friendly
152  
153 =head1 VERSION
154  
155 version 1.102060
156  
157 =head1 SYNOPSIS
158  
159 # iPhone compatible directory listing..
160 plackup -MPlack::App::Directory -e 'builder { enable iPhone; Plack::App::Directory->new }'
161
162 # m.search.CPAN.org
163 plackup -MPlack::App::Proxy -e 'builder {enable iPhone; Plack::App::Proxy->new(remote => "http://search.cpan.org/") }'
164
165 # Or in your app.psgi
166 use Plack::Builder;
167 builder {
168 enable "iPhone",
169 tidy => 1,
170 manifest => 'app.manifest',
171 viewport => 'initial-scale = 1, maximum-scale = 1.5, width = device-width',
172 statusbar => 'black-translucent',
173 startup_image => 'loading.png',
174 icon => 'icon.png';
175 $app;
176 }
177  
178 =head1 DESCRIPTION
179  
180 Plack::Middleware::iPhone does on-the-fly rewriting of any html content returned by your app (mostly just the head block)
181 to make it play nicer with iPhones.
182  
183 This is a borderline ACME module. For real
184 L<HTML5|http://www.quirksmode.org/blog/archives/2010/03/html5_apps.html>
185 mobile web apps you should be writing the HTML yourself.
186  
187 =head1 ATTRIBUTES
188  
189 =head2 icon
190  
191 A 57x57 image icon that the iPhone will display as a shortcut to your app if you add it to your Home Screen
192 via the "Add to Home Screen" function.
193  
194 =head2 startup_image
195  
196 A 320x460 PNG image that is displayed while your app is loading. If this is not set, the iPhone automatically
197 uses a screenshot of the most recent app state.
198  
199 =head2 statusbar
200  
201 Sets the C<apple-mobile-web-app-status-bar-style> meta tag, which controls the status bar appearance when yourself
202 app is launched from a Home icon shortcut.
203  
204 Valid options are:
205  
206 =over 4
207  
208 =item *
209  
210 gray (default)
211  
212 =item *
213  
214 black
215  
216 =item *
217  
218 black-translucent
219  
220 =back
221  
222 =head2 viewport
223  
224 Sets the viewport meta tag, which determines how wide your iPhone thinks the screen is and scaling options.
225  
226 See
227 L<Configuring the Viewport|http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html>
228 for more information.
229  
230 =head2 manifest
231  
232 Automatically generates a manifest file for your application (with whatever name you pass in), and sets the
233 C<manifest> attribute on the html root tag, which triggers your iPhone to start using offline HTML Web App caching.
234  
235 See L<Going Offline|http://building-iphone-apps.labs.oreilly.com/ch06.html> for more information
236  
237 =head2 tidy
238  
239 Run the HTML through L<HTML::Tidy>
240  
241 =head1 SEE ALSO
242  
243 L<Building iPhone Apps with HTML, CSS, and JavaScript|http://building-iphone-apps.labs.oreilly.com>, Jonathan Stark (freely available).
244  
245 =head1 AUTHOR
246  
247 Patrick Donelan <pat@patspam.com>
248  
249 =head1 COPYRIGHT AND LICENSE
250  
251 This software is copyright (c) 2010 by Patrick Donelan.
252  
253 This is free software; you can redistribute it and/or modify it under
254 the same terms as the Perl 5 programming language system itself.
255  
256 =cut
257  
258  
259 __END__
↑ Summary