root/trunk/plagger/lib/Plagger/Plugin/Filter/Regexp.pm

Revision 782 (checked in by woremacx, 14 years ago)

decode regexp

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Filter::Regexp;
2 use strict;
3 use base qw( Plagger::Plugin::Filter::Base );
4 use Encode;
5
6 sub init {
7     my $self = shift;
8     $self->SUPER::init(@_);
9
10     unless ($self->conf->{regexp}) {
11         Plagger->context->error("regexp is missing");
12         return;
13     }
14 }
15
16 sub filter {
17     my($self, $body) = @_;
18
19     local $_ = $body;
20     my $regexp = decode_utf8($self->conf->{regexp}, Encode::FB_CROAK);
21     my $count = eval $regexp;
22
23     if ($@) {
24         Plagger->context->log(error => "Error: $@ in " . $self->conf->{regexp});
25         return (0, $body);
26     }
27
28     return ($count, $_);
29 }
30
31 1;
32
33 __END__
34
35 =head1 NAME
36
37 Plagger::Plugin::Filter::Regexp - Rewrite entry body using regular expression
38
39 =head1 SYNOPSIS
40
41   - module: Filter::Regexp
42     config:
43       regexp: s/Plagger/$1, the pluggable Aggregator/g
44       text_only: 1
45
46 =head1 DESCRIPTION
47
48 This plugin applies regular expression to each entry body by using
49 C<eval>.
50
51 =head1 CONFIG
52
53 =over 4
54
55 =item text_only
56
57 When set to 1, uses HTML::Parser so that the regexp substition should
58 be applied only to HTML text part. Defaults to 0.
59
60 =back
61
62 =head1 AUTHOR
63
64 Tatsuhiko Miyagawa
65
66 =head1 SEE ALSO
67
68 L<Plagger>, L<HTML::Parser>
69
70 =cut
Note: See TracBrowser for help on using the browser.