Perlを中心とした技術系の話などをつらつら

2005年02月10日

Sledge::Plugin::JavaScript::DocumentWrite

Sledge::Plugin::JavaScript::DocumentWrite - output content as javascript's document.write

Outputをdocument.writeにして出力するSledgeのプラグインを書いてみました。

フィルターの部分はApache::JavaScript::DocumentWriteのコードを参考にさせてもらいました。

それとContent-typeをtext/plainにするためにオリジナルのoutput_contentにちょっとだけ手を加えたものをAFTER_DISPATCHにHookしてます。ちょっと強引かなー。

package Sledge::Plugin::JavaScript::DocumentWrite;
use strict;
use vars qw($VERSION);
$VERSION = '0.02';
sub import {
    my $class = shift;
    my $pkg = caller(0);
    no strict 'refs';
    *{"$pkg\::add_documentwrite_filter"} = \&add_documentwrite_filter;
}
sub add_documentwrite_filter {
    my $self = shift;
    $self->add_filter(\&documentwrite_filter);
    $self->register_hook(AFTER_DISPATCH => \&output_content);
}
sub documentwrite_filter {
    my ($self, $content) = @_;
    return join "\n", map {
        s/\x27/'/g; # '
        $_ = "document.writeln('$_');";
    } split /\n/, $content;
}
sub output_content {
    my $self = shift;
    $self->r->content_type('text/plain');
    my $content = $self->make_content;
    $self->set_content_length(length $content);
    $self->send_http_header;
    $self->r->print($content);
    $self->invoke_hook('AFTER_OUTPUT');
    $self->finished(1);
}
1;

Technorati TAGTechnorati TAG ,

Posted by horiuchi at 2005年02月10日 10:19

Continuing the discussion...

このエントリーのトラックバックURL:
http://hori-uchi.com/mt/trackback/271

Comments

Post a comment




Remember personal info?