[Jifty-commit] r3208 - in jifty/trunk/lib/Jifty: View/Declare

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon May 7 12:35:59 EDT 2007


Author: ruz
Date: Mon May  7 12:35:58 2007
New Revision: 3208

Modified:
   jifty/trunk/lib/Jifty/View/Declare/Handler.pm
   jifty/trunk/lib/Jifty/Web/PageRegion.pm

Log:
to sanity through insanity
* control mode of output handles, if content type has charset defined
  then we set :encoding(<charset>) output layer (or :utf8), otherwise
  binary
* regions are special as we print out them into STDOUT, but sometimes
  need them as a string. We localize STDOUT and get data, however because
  of the above canonicalization we get octets or binary, so we check
  again the current content type. If the type contains charset definition
  then we decode octets back into perl string(in terms of perl unicode support),
  otherwise we leave things as is.

jifty is sane when apps' developers are sane
* never use 'bytes' pragma
* avoid using 'encoding' pragma
* use perl strings in jifty
** when you get a text data from external sources then Encode::decode it
* set output encoding with $r->content_type('type/subtype; charset=XXX')
** by default it's UTF-8
** you can use cp1251 (or other) and things should work, user will get
   data in cp1251 and browser should display it right
** don't Encode::encode things before output

everybody have own critirea of sanity
* if you think that something is wrong then add tests to jifty 

  

Modified: jifty/trunk/lib/Jifty/View/Declare/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/Handler.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Declare/Handler.pm	Mon May  7 12:35:58 2007
@@ -74,11 +74,26 @@
         shift;    # Turn the method into a function
         goto &Template::Declare::Tags::outs_raw;
     };
-    my $content =Template::Declare::Tags::show($template);
-        unless ( Jifty->handler->apache->http_header_sent ||Jifty->web->request->is_subrequest ) {
-            Jifty->handler->apache->send_http_header();
+    my $content = Template::Declare::Tags::show( $template );
+    return unless defined $content && length $content;
+
+    my $r = Jifty->handler->apache;
+    $r->content_type || $r->content_type('text/html; charset=utf-8'); # Set up a default
+    unless ( Jifty->handler->apache->http_header_sent || Jifty->web->request->is_subrequest ) {
+        Jifty->handler->apache->send_http_header;
+    }
+
+    if ( my ($enc) = $r->content_type =~ /charset=([\w-]+)$/ ) {
+        if ( lc($enc) =~ /utf-?8/) {
+            binmode *STDOUT, ":utf8" or die "couldn't set layers: $!";
+        }
+        else {
+            binmode *STDOUT, ":encoding($enc)" or die "couldn't set layers: $!";
         }
-    utf8::downgrade($content, 1); # Just before we go to stdout, we REALLY want to convert to octets.
+    } else {
+        binmode *STDOUT or die "couldn't set layers: $!";
+    }
+
     print STDOUT $content;
     return undef;
 }

Modified: jifty/trunk/lib/Jifty/Web/PageRegion.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/PageRegion.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/PageRegion.pm	Mon May  7 12:35:58 2007
@@ -323,17 +323,24 @@
 
     # template-declare based regions are printing to stdout
     my $td_out = '';
-    Encode::_utf8_on($td_out);
-    open my $output_fh, '>>:utf8', \$td_out;
-    local *STDOUT = $output_fh;
-
-    local $main::DEBUG=1;
-    # Call into the dispatcher
-    Jifty->handler->dispatcher->handle_request;
+    {
+        open my $output_fh, '>>', \$td_out;
+        local *STDOUT = $output_fh;
+
+        local $main::DEBUG = 1;
+        # Call into the dispatcher
+        Jifty->handler->dispatcher->handle_request;
+    }
 
     Jifty->handler->mason->interp->out_method($orig_out);
 
-    $$out_method .= $td_out if length($td_out);
+    return unless length $td_out;
+
+    if ( my ($enc) = Jifty->handler->apache->content_type =~ /charset=([\w-]+)$/ ) {
+        $td_out = Encode::decode($enc, $td_out);
+    }
+    $$out_method .= $td_out;
+
     return;
 }
 


More information about the Jifty-commit mailing list