NAME
String::Filter - a regexp-based string filter
SYNOPSIS
# define the rules that convert tweets to HTML
# (handles url, @user, #hash)
my $sf = String::Filter->new(
rules => [
'http://[A-Za-z0-9_\-\~\.\%\?\#\@/]+' => sub {
my $url = shift;
sprintf(
'<a href="%s">%s</a>',
encode_entities($url),
encode_entities($url),
);
},
'(?:^|\s)\@[A-Za-z0-9_]+' => sub {
$_[0] =~ /^(.*?\@)(.*)$/;
my ($prefix, $user) = ($1, $2);
sprintf(
'%s<a href="http://twitter.com/%s">%s</a>',
encode_entities($prefix),
encode_entities($user),
encode_entities($user),
);
},
'(?:^|\s)#[A-Za-z0-9_]+' => sub {
$_[0] =~ /^(.?)(#.*)$/;
my ($prefix, $hashtag) = ($1, $2);
sprintf(
'%s<a href="http://twitter.com/search?q=%s">%s</a>',
encode_entities($prefix),
encode_entities(uri_escape($hashtag)),
$hashtag,
);
},
],
default_rule => sub {
my $text = shift;
encode_entities($text);
},
);
# convert a tweet to HTML
my $html = $sf->filter($tweet);
DESCRIPTION
The module is a regexp-based string filter, that can merge multiple conversion rules for converting strings. The primary target of the module is to convert inline markups (such as the tweets of Twitter) to HTML.
FUNCTIONS
new
instantiates the filter object. Takes a hash as an argument recognizing the attributes below.
rules
arrayref of more than zero "regexp => subref"s. For more information see add_rule.
default_rule
default filter function. See the default_rule accessor for more information.
filter($input)
Converts the input string using the given rules and returns it.
add_rule($regexp => $subref)
adds a conversion rule. For each substring matching the regular expression the subref will be invoked with the substring as the only argument. The subref should return the filtered output of the substring.
default_rule([$subref])
setter / getter for the default conversion function. The subref should accept a string and return the filtered output of the input.
COPYRIGHT
Copyright (C) 2010 Cybozu Labs, Inc. Written by Kazuho Oku.
LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.