root/lang/perl/plagger/lib/Plagger/Plugin/Publish/XSLT.pm

Revision 85, 1.7 kB (checked in by cho45, 15 months ago)

lang/perl/plagger/lib/Plagger/Plugin,
lang/perl/plagger/lib/Plagger/Plugin/Publish,
lang/perl/plagger/lib/Plagger/Plugin/Publish/XSLT.pm:

Added.

Line 
1package Plagger::Plugin::Publish::XSLT;
2
3use strict;
4use base qw( Plagger::Plugin::Publish::Feed );
5
6use XML::LibXML;
7use XML::LibXSLT;
8use File::Spec;
9
10our $stylesheet;
11
12sub 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
24sub 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
32sub 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
591;
60__END__
61
62=head1 NAME
63
64Plagger::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
78This plugin publish translated feeds by XSLT.
79
80=head1 AUTHOR
81
82cho45 <cho45@lowreal.net>, http://lowreal.net/
83
84=head1 SEE ALSO
85
86L<Plagger>
87
88=cut
Note: See TracBrowser for help on using the browser.