| 1 | package Plagger::Plugin::Publish::XSLT; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use base qw( Plagger::Plugin::Publish::Feed ); |
|---|
| 5 | |
|---|
| 6 | use XML::LibXML; |
|---|
| 7 | use XML::LibXSLT; |
|---|
| 8 | use File::Spec; |
|---|
| 9 | |
|---|
| 10 | our $stylesheet; |
|---|
| 11 | |
|---|
| 12 | sub register { |
|---|
| 13 | my ($self, $context) = @_; |
|---|
| 14 | |
|---|
| 15 | $self->SUPER::register($context); |
|---|
| 16 | |
|---|
| 17 | $context->register_hook( |
|---|
| 18 | $self, |
|---|
| 19 | 'publish.feed' => \&publish_feed, |
|---|
| 20 | 'plugin.init' => \&plugin_init, |
|---|
| 21 | ); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub plugin_init { |
|---|
| 25 | my ($self, $context, $args) = @_; |
|---|
| 26 | |
|---|
| 27 | my $xslt = XML::LibXSLT->new(); |
|---|
| 28 | $context->log(debug => "loading " . $self->conf->{xsl} . " as StyleSheet"); |
|---|
| 29 | $stylesheet = $xslt->parse_stylesheet_file($self->conf->{xsl}); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | sub publish_feed { |
|---|
| 33 | my ($self, $context, $args) = @_; |
|---|
| 34 | |
|---|
| 35 | $context->log(info => "XSLT processing Start.."); |
|---|
| 36 | my $f = $args->{feed}; |
|---|
| 37 | my $filepath = File::Spec->catfile($self->conf->{dir}, $self->gen_filename($f)); |
|---|
| 38 | |
|---|
| 39 | my $parser = XML::LibXML->new(); |
|---|
| 40 | |
|---|
| 41 | $context->log(debug => "loading $filepath as source XML"); |
|---|
| 42 | my $source = $parser->parse_file($filepath); |
|---|
| 43 | |
|---|
| 44 | my $result = $stylesheet->output_string( |
|---|
| 45 | $stylesheet->transform($source) |
|---|
| 46 | ); |
|---|
| 47 | |
|---|
| 48 | my $ext = $self->conf->{extension}; |
|---|
| 49 | $filepath =~ s/\..+?$/.$ext/; |
|---|
| 50 | $context->log(info => "save feed for " . $f->link . " to $filepath"); |
|---|
| 51 | |
|---|
| 52 | utf8::decode($result) unless utf8::is_utf8($result); |
|---|
| 53 | open my $output, ">:utf8", $filepath or $context->error("$filepath: $!"); |
|---|
| 54 | print $output $result; |
|---|
| 55 | close $output; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | 1; |
|---|
| 60 | __END__ |
|---|
| 61 | |
|---|
| 62 | =head1 NAME |
|---|
| 63 | |
|---|
| 64 | Plagger::Plugin::Publish::XSLT - Publish with translating by XSLT |
|---|
| 65 | |
|---|
| 66 | =head1 SYNOPSIS |
|---|
| 67 | |
|---|
| 68 | - module: Publish::XSLT |
|---|
| 69 | config: |
|---|
| 70 | format: Atom |
|---|
| 71 | dir: /path/to/output/dir |
|---|
| 72 | xsl: /path/to/xslt/file |
|---|
| 73 | extension: html |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | =head1 DESCRIPTION |
|---|
| 77 | |
|---|
| 78 | This plugin publish translated feeds by XSLT. |
|---|
| 79 | |
|---|
| 80 | =head1 AUTHOR |
|---|
| 81 | |
|---|
| 82 | cho45 <cho45@lowreal.net>, http://lowreal.net/ |
|---|
| 83 | |
|---|
| 84 | =head1 SEE ALSO |
|---|
| 85 | |
|---|
| 86 | L<Plagger> |
|---|
| 87 | |
|---|
| 88 | =cut |
|---|