Apache-Filter-HanConvert

 view release on metacpan or  search on metacpan

HanConvert.pm  view on Meta::CPAN

=head1 DESCRIPTION

This module utilizes the B<Encode::HanConvert> module with B<Apache2>'s
output filtering mechanism, to provide a flexible and customizable
solution for serving multiple encoding/variants from the same source
documents.

From the settings in L</SYNOPSIS>, the server would negotiate with the
client's browser about their C<Accept-Language> preference (C<zh-cn> and
C<zh> means Simplified, other C<zh-*> means Traditional), as well as the
preferred C<Accept-Charset> setting (defaults to C<utf8> if nothing
was explicitly specified).

The C<Content-Type> header will be rewritten to reflect the final
encoding used.

If you want to use other encodings, try adding these lines:

    PerlSetVar HanConvertFromEncoding "UTF-8"
    PerlSetVar HanConvertToEncodingTraditional "big5"
    PerlSetVar HanConvertToEncodingSimplified "gbk"

HanConvert.pm  view on Meta::CPAN


    $to_encoding ||= $r->dir_config($encodings{$to_variant});
    
    if (!$to_encoding) {
	my $chars = $r->headers_in->get('Accept-Charset');

	$to_encoding = $1
	    if $chars =~ /\b($charsets{$to_variant})\b/i;
    }

    my $var_enc	   = $variants{"$from_variant$to_variant"} || 'utf8';
    $from_encoding = Encode::resolve_alias($from_encoding) || 'utf8';
    $to_encoding   = Encode::resolve_alias($to_encoding)   || 'utf8';

    return Apache::DECLINED if $from_encoding eq $to_encoding
			    and $from_variant eq $to_variant;

    my $charset = ($to_encoding eq 'utf8' ? 'utf-8' : $to_encoding);
    $content_type =~ s/(?:;charset=[^;]+(.*))?$/;charset=$charset$1/;
    $r->content_type($content_type);

    my $c = $filter->c;
    my $bb_ctx = APR::Brigade->new($c->pool, $c->bucket_alloc);
    my $data = '';

    while (!$bb->empty) {
	my $bucket = $bb->first;

HanConvert.pm  view on Meta::CPAN


	if ($bucket->is_eos) {
	    $bb_ctx->insert_tail($bucket);
	    last;
	}

	my $buffer;
	my $status = $bucket->read($buffer);
	return $status unless $status == APR::SUCCESS;

	Encode::from_to($buffer, $from_encoding => 'utf8', Encode::FB_HTMLCREF)
	    if $from_encoding ne 'utf8';

	if ($var_enc eq $to_encoding) {
	    $bucket = APR::Bucket->new( $buffer );
	}
	elsif ($data .= $buffer) {
	    $bucket = APR::Bucket->new( Encode::encode(
		$to_encoding, Encode::decode($var_enc, $data, Encode::FB_QUIET)
	    ) );
	}



( run in 2.158 seconds using v1.01-cache-2.11-cpan-df04353d9ac )