[Catalyst-commits] r9183 - in Catalyst-View-TT-XHTML/1.000/trunk: . lib/Catalyst/View lib/Catalyst/View/ContentNegotiation t

t0m at dev.catalyst.perl.org t0m at dev.catalyst.perl.org
Tue Feb 3 21:23:46 GMT 2009


Author: t0m
Date: 2009-02-03 21:23:45 +0000 (Tue, 03 Feb 2009)
New Revision: 9183

Added:
   Catalyst-View-TT-XHTML/1.000/trunk/lib/Catalyst/View/ContentNegotiation/
   Catalyst-View-TT-XHTML/1.000/trunk/lib/Catalyst/View/ContentNegotiation/XHTML.pm
Modified:
   Catalyst-View-TT-XHTML/1.000/trunk/Changes
   Catalyst-View-TT-XHTML/1.000/trunk/Makefile.PL
   Catalyst-View-TT-XHTML/1.000/trunk/README
   Catalyst-View-TT-XHTML/1.000/trunk/t/podspelling.t
Log:
Merge to trunk, add version note

Modified: Catalyst-View-TT-XHTML/1.000/trunk/Changes
===================================================================
--- Catalyst-View-TT-XHTML/1.000/trunk/Changes	2009-02-03 21:18:50 UTC (rev 9182)
+++ Catalyst-View-TT-XHTML/1.000/trunk/Changes	2009-02-03 21:23:45 UTC (rev 9183)
@@ -1,16 +1,19 @@
+1.100
+  - Refactor into a Moose Role for use with alternate views. (rafl)
+  - Additional documentation (t0m)
 1.004
   - Nick the OSX fragment out of the Catalyst::Runtime Makefile.PL to
-    beat my Mac into generating a correct dist.
+    beat my Mac into generating a correct dist. (t0m)
 1.003
   - Fixes an tests to be fully Internet Explorer compatible (David Dorward)
   - Change to MRO::Compat for perl 5.10 (t0m)
 1.002 2008-12-13
   - Add 'use Class::C3' so that the module works on the currently 
-    released Catalyst version.
+    released Catalyst version. (t0m)
 1.001 2008-12-12
   - Add tests for other Accept header cases where the current code
     will get it wrong. (David Dorward)
   - Fix all of these tests. (t0m)
 1.000 2008-12-12
   - First working version of the module extracted from the quick hack
-    I have in every Catalyst application I've ever written.
+    I have in every Catalyst application I've ever written. (t0m)

Modified: Catalyst-View-TT-XHTML/1.000/trunk/Makefile.PL
===================================================================
--- Catalyst-View-TT-XHTML/1.000/trunk/Makefile.PL	2009-02-03 21:18:50 UTC (rev 9182)
+++ Catalyst-View-TT-XHTML/1.000/trunk/Makefile.PL	2009-02-03 21:23:45 UTC (rev 9183)
@@ -1,16 +1,16 @@
 use inc::Module::Install;
 
-name 'Catalyst-View-TT-XHTML';
-all_from 'lib/Catalyst/View/TT/XHTML.pm';
+name 'Catalyst-View-ContentNegotiation-XHTML';
+all_from 'lib/Catalyst/View/ContentNegotiation/XHTML.pm';
 
 requires 'Catalyst::Runtime';
 requires 'Catalyst::View::TT';
 requires 'HTTP::Negotiate';
 requires 'MRO::Compat';
 
-build_requires 'Catalyst::Action::RenderView';
-build_requires 'Test::WWW::Mechanize::Catalyst';
-build_requires 'Test::More';
+test_requires 'Catalyst::Action::RenderView';
+test_requires 'Test::WWW::Mechanize::Catalyst';
+test_requires 'Test::More';
 
 resources repository => 'http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-View-TT-XHTML';
 

Modified: Catalyst-View-TT-XHTML/1.000/trunk/README
===================================================================
--- Catalyst-View-TT-XHTML/1.000/trunk/README	2009-02-03 21:18:50 UTC (rev 9182)
+++ Catalyst-View-TT-XHTML/1.000/trunk/README	2009-02-03 21:23:45 UTC (rev 9183)
@@ -1,49 +1,73 @@
 NAME
-    Catalyst::View::TT::XHTML - A sub-class of the standard TT view which
-    serves application/xhtml+xml content if the browser accepts it.
+    Catalyst::View::ContentNegotiation::XHTML - A Moose Role to apply to
+    Catalyst views adjusts the response Content-Type header to
+    application/xhtml+xml content if the browser accepts it.
 
 SYNOPSIS
-        package MyApp::View::XHTML;
-        use strict;
-        use warnings;
-        use base qw/Catalyst::View::TT::XHTML MyApp::View::TT/;
-    
+        package Catalyst::View::TT;
+
+        use Moose;
+        use namespace::clean -except => 'meta';
+
+        extends qw/Catalyst::View::TT/;
+        with qw/Catalyst::View::ContentNegotiation::XHTML/;
+
         1;
-    
+
 DESCRIPTION
-    This is a very simple sub-class of Catalyst::View::TT, which sets the
-    response "Content-Type" to be "application/xhtml+xml" if the user's
-    browser sends an "Accept" header indicating that it is willing to
-    process that MIME type.
+    This is a very simple Role which uses a method modifier to run after the
+    "process" method, and sets the response "Content-Type" to be
+    "application/xhtml+xml" if the users browser sends an "Accept" header
+    indicating that it is willing to process that MIME type.
 
     Changing the "Content-Type" causes browsers to interpret the page as
-    strict XHTML, meaning that the markup must be well formed.
+    XML, meaning that the markup must be well formed.
 
     This is useful when you're developing your application, as you know that
-    all pages you view are rendered strictly, so any markup errors will show
-    up at once.
+    all pages you view are parsed as XML, so any errors caused by your
+    markup not being well-formed will show up at once.
 
+METHOD MODIFIERS
+  after process
+    Changes the response "Content-Type" if appropriate (from the requests
+    "Accept" header).
+
 METHODS
-  process
-    Overrides the standard process method, delegating to Catalyst::View::TT
-    to render the template, and then changing the response "Content-Type" if
-    appropriate (from the requests "Accept" header).
+  pragmatic_accept
+    Some browsers (such as Internet Explorer) have a nasty way of sending
+    Accept */* and this claiming to support XHTML just as well as HTML.
+    Saving to a file on disk or opening with another application does count
+    as accepting, but it really should have a lower q value then text/html.
+    This sub takes a pragmatic approach and corrects this mistake by
+    modifying the Accept header before passing it to content negotiation.
 
+ATTRIBUTES
+  variants
+    Returns an array ref of 3 part arrays, comprising name, priority, output
+    mime-type, which is used for the content negotiation algorithm.
+
+PRIVATE METHODS
+  _build_variants
+    Returns the default variant attribute contents.
+
+SEE ALSO
+    Catalyst::View::TT::XHTML - Trivial Catalyst TT view using this role.
+    <http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html> - Content
+    negotiation RFC.
+
 BUGS
-    There should be a more elegant way to inherit the config of your normal
-    TT view.
+    Will only work with Views which implement a process method.
 
-    Configuration (as loaded by Catalyst::Plugin::ConfigLoader) for the TT
-    view is not used.
+    Should be split into a base ContentNegotiation role which is consumed by
+    ContentNegotiation::XHTML.
 
-    No helper to generate the view file needed (just copy the code in the
-    SYNOPSIS).
-
 AUTHOR
-    Tomas Doran "<bobtfish at bobtfish.net>"
+    Tomas Doran (t0m) "<bobtfish at bobtfish.net>"
 
 CONTRIBUTORS
-    David Dorward - test patches
+    David Dorward - test patches and */* pragmatism.
+    Florian Ragwitz (rafl) "<rafl at debian.org>" - Conversion into a Moose
+    Role
 
 COPYRIGHT
     This module itself is copyright (c) 2008 Tomas Doran and is licensed

Added: Catalyst-View-TT-XHTML/1.000/trunk/lib/Catalyst/View/ContentNegotiation/XHTML.pm
===================================================================
--- Catalyst-View-TT-XHTML/1.000/trunk/lib/Catalyst/View/ContentNegotiation/XHTML.pm	                        (rev 0)
+++ Catalyst-View-TT-XHTML/1.000/trunk/lib/Catalyst/View/ContentNegotiation/XHTML.pm	2009-02-03 21:23:45 UTC (rev 9183)
@@ -0,0 +1,148 @@
+package Catalyst::View::ContentNegotiation::XHTML;
+
+use Moose::Role;
+use MooseX::Types::Moose qw/Num Str ArrayRef/;
+use MooseX::Types::Structured qw/Tuple/;
+use HTTP::Negotiate qw/choose/;
+
+use namespace::clean -except => 'meta';
+
+our $VERSION = '1.100';
+
+has variants => (
+    is      => 'ro',
+    isa     => ArrayRef[Tuple[Str, Num, Str]],
+    lazy    => 1,
+    builder => '_build_variants',
+);
+
+sub _build_variants {
+    return [
+        [qw| xhtml 1.000 application/xhtml+xml |],
+        [qw| html  0.900 text/html             |],
+    ];
+}
+
+after process => sub {
+    my ($self, $c) = @_;
+    if ($c->request->header('Accept') && $c->response->headers->{'content-type'} =~ m|text/html|) {
+        $self->pragmatic_accept($c);
+        my $var = choose($self->variants, $c->request->headers);
+        if ($var eq 'xhtml') {
+            $c->response->headers->{'content-type'} =~ s|text/html|application/xhtml+xml|;
+        }
+    }
+};
+
+sub pragmatic_accept {
+    my ($self, $c) = @_;
+    my $accept = $c->request->header('Accept');
+    if ($accept =~ m|text/html|) {
+        $accept =~ s!\*/\*\s*([,]+|$)!*/*;q=0.5$1!;
+    } else {
+        $accept =~ s!\*/\*\s*([,]+|$)!text/html,*/*;q=0.5$1!;
+    }
+    $c->request->header('Accept' => $accept);
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Catalyst::View::ContentNegotiation::XHTML - A Moose Role to apply to
+Catalyst views adjusts the response Content-Type header to 
+application/xhtml+xml content if the browser accepts it.
+
+=head1 SYNOPSIS
+
+    package Catalyst::View::TT;
+
+    use Moose;
+    use namespace::clean -except => 'meta';
+
+    extends qw/Catalyst::View::TT/;
+    with qw/Catalyst::View::ContentNegotiation::XHTML/;
+
+    1;
+
+=head1 DESCRIPTION
+
+This is a very simple Role which uses a method modifier to run after the
+C<process> method, and sets the response C<Content-Type> to be 
+C<application/xhtml+xml> if the users browser sends an C<Accept> header 
+indicating that it is willing to process that MIME type.
+
+Changing the C<Content-Type> causes browsers to interpret the page as
+XML, meaning that the markup must be well formed.
+
+This is useful when you're developing your application, as you know that
+all pages you view are parsed as XML, so any errors caused by your markup
+not being well-formed will show up at once.
+
+=head1 METHOD MODIFIERS
+
+=head2 after process
+
+Changes the response C<Content-Type> if appropriate (from the requests C<Accept> header).
+
+=head1 METHODS
+
+=head2 pragmatic_accept
+
+Some browsers (such as Internet Explorer) have a nasty way of sending
+Accept */* and this claiming to support XHTML just as well as HTML.
+Saving to a file on disk or opening with another application does
+count as accepting, but it really should have a lower q value then
+text/html. This sub takes a pragmatic approach and corrects this mistake
+by modifying the Accept header before passing it to content negotiation.
+
+=head1 ATTRIBUTES
+
+=head2 variants
+
+Returns an array ref of 3 part arrays, comprising name, priority, output 
+mime-type, which is used for the content negotiation algorithm.
+
+=head1 PRIVATE METHODS
+
+=head2 _build_variants
+
+Returns the default variant attribute contents.
+
+=head1 SEE ALSO
+
+=over
+
+=item L<Catalyst::View::TT::XHTML> - Trivial Catalyst TT view using this role.
+
+=item L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html> - Content negotiation RFC.
+
+=back
+
+=head1 BUGS
+
+Will only work with Views which implement a process method.
+
+Should be split into a base ContentNegotiation role which is consumed by ContentNegotiation::XHTML.
+
+=head1 AUTHOR
+
+Tomas Doran (t0m) C<< <bobtfish at bobtfish.net> >>
+
+=head1 CONTRIBUTORS
+
+=over
+
+=item David Dorward - test patches and */* pragmatism. 
+
+=item Florian Ragwitz (rafl) C<< <rafl at debian.org> >> - Conversion into a Moose Role
+
+=back
+
+=head1 COPYRIGHT
+
+This module itself is copyright (c) 2008 Tomas Doran and is licensed under the same terms as Perl itself.
+
+=cut

Modified: Catalyst-View-TT-XHTML/1.000/trunk/t/podspelling.t
===================================================================
--- Catalyst-View-TT-XHTML/1.000/trunk/t/podspelling.t	2009-02-03 21:18:50 UTC (rev 9182)
+++ Catalyst-View-TT-XHTML/1.000/trunk/t/podspelling.t	2009-02-03 21:23:45 UTC (rev 9183)
@@ -18,3 +18,6 @@
 TT
 Doran
 Dorward
+rafl
+ContentNegotiation
+Ragwitz




More information about the Catalyst-commits mailing list