String::Filter - a regexp-based string filter
# 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);
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.
instantiates the filter object. Takes a hash as an argument recognizing the attributes below.
arrayref of more than zero "regexp => subref"s. For more information see add_rule.
default filter function. See the default_rule accessor for more information.
Converts the input string using the given rules and returns it.
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.
setter / getter for the default conversion function. The subref should accept a string and return the filtered output of the input.
Copyright (C) 2010 Cybozu Labs, Inc. Written by Kazuho Oku.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install String::Filter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::Filter
CPAN shell
perl -MCPAN -e shell install String::Filter
For more information on module installation, please visit the detailed CPAN module installation guide.