File Coverage

blib/lib/Plack/Middleware/HTMLify.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 14 100.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::HTMLify;
2             BEGIN {
3 1     1   86015 $Plack::Middleware::HTMLify::VERSION = '0.1.1';
4             }
5 1     1   10 use strict;
  1         2  
  1         37  
6 1     1   5 use warnings;
  1         2  
  1         36  
7 1     1   1092 use parent qw( Plack::Middleware );
  1         345  
  1         7  
8              
9             use Plack::Util;
10             use Plack::Util::Accessor
11             qw( set_doctype set_head set_body_start set_body_end );
12              
13             __PACKAGE__->{'count'} = 0;
14              
15             sub call {
16             my ( $self, $env ) = @_;
17              
18             my $res = $self->app->($env);
19             $self->response_cb(
20             $res,
21             sub {
22             my $res = shift;
23             my $headers = $res->[1];
24             Plack::Util::header_set( $headers, 'Content-Type', 'text/html' );
25              
26             return sub {
27             my $chunk = shift;
28             if ( !defined $chunk ) {
29             __PACKAGE__->{'count'} = 0;
30             $chunk = qq[\n\n];
31             $chunk = "\n" . $self->set_body_end . $chunk
32             if $self->set_body_end;
33             return $chunk;
34             }
35             else {
36             if ( __PACKAGE__->{'count'} == 0 ) {
37             my $start_chunk = "";
38             if ( $self->set_doctype ) {
39             $start_chunk = $self->set_doctype . "\n";
40             }
41             else {
42             $start_chunk =
43             qq[\n];
44             }
45             if ( $start_chunk =~ /xhtml/i ) {
46             $start_chunk .=
47             qq[\n];
48             }
49             else {
50             $start_chunk .= qq[\n];
51             }
52             $start_chunk .=
53             qq[\n] . $self->set_head . "\n\n"
54             if $self->set_head;
55             $start_chunk .= qq[\n];
56             $start_chunk .= $self->set_body_start . "\n"
57             if $self->set_body_start;
58             $chunk = $start_chunk . $chunk;
59             }
60             __PACKAGE__->{'count'}++;
61             return $chunk;
62             }
63             }
64             }
65             );
66             }
67              
68             1;
69              
70              
71              
72             =pod
73              
74             =head1 NAME
75              
76             Plack::Middleware::HTMLify - Transform a non-html page into html.
77              
78             =head1 VERSION
79              
80             version 0.1.1
81              
82             =head1 SYNOPSIS
83              
84             use Plack::Builder;
85              
86             my $app = sub {
87             return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
88             };
89              
90             builder {
91             enable "HTMLify",
92             set_doctype => "...", # html for the doctype
93             set_head => "...", # html to include in
94             set_body_start => "...", # html for the beginning of the
95             set_body_end => "..."; # html for the end of the
96             $app;
97             };
98              
99             =head1 DESCRIPTION
100              
101             Plack::Middleware::HTMLify is meant to be used to transform non-html web content
102             into html.
103              
104             Use case:
105             On CPAN, the 'source' link is delivered as 'text/plain'. If you wanted to do
106             some post-processing of this page to add syntax highlighting you would need the
107             page to be set as 'text/html' and have some basic HTML formatting.
108              
109             =head1 SEE ALSO
110              
111             L
112              
113             =head1 AUTHOR
114              
115             Mark Jubenville
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2010 by Mark Jubenville.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut
125              
126              
127             __END__