| 1 | package Text::FindLinks; | ||||||||||||
| 2 | |||||||||||||
| 3 | =encoding utf8 | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Text::FindLinks - Find and markup URLs in plain text | ||||||||||||
| 8 | |||||||||||||
| 9 | =cut | ||||||||||||
| 10 | |||||||||||||
| 11 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 5 | ▶ | use Exporter; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 5 | ▶ | use Params::Validate qw/validate CODEREF/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 5 | our @ISA = 'Exporter'; | |||||||||||
| 17 | 5 | our @EXPORT_OK = qw/find_links markup_links/; | |||||||||||
| 18 | 5 | our $VERSION = '0.04'; | |||||||||||
| 19 | |||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||
| 21 | |||||||||||||
| 22 | use Text::FindLinks 'markup_links'; | ||||||||||||
| 23 | my $text = "Have you seen www.foo.com yet?"; | ||||||||||||
| 24 | # Have you seen <a href="http://www.foo.com">http://www.foo.com</a> yet? | ||||||||||||
| 25 | print markup_links(text => $text); | ||||||||||||
| 26 | |||||||||||||
| 27 | =head1 FUNCTIONS | ||||||||||||
| 28 | |||||||||||||
| 29 | =head2 markup_links(text => ..., [handler => sub { ... }]) | ||||||||||||
| 30 | |||||||||||||
| 31 | Finds all URLs in the given text and replaces them using | ||||||||||||
| 32 | the given handler. The handler gets passed three arguments: | ||||||||||||
| 33 | the URL itself, all the text to the left from it and all the | ||||||||||||
| 34 | text to the right. (The context is passed in case you would | ||||||||||||
| 35 | like to keep some URLs untouched.) If no handler is given, | ||||||||||||
| 36 | the default handler will be used that simply creates a link | ||||||||||||
| 37 | to the URL and skips URLs already turned into links. | ||||||||||||
| 38 | |||||||||||||
| 39 | =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | sub markup_links | ||||||||||||
| 42 | 12 | ▶ | { | ||||||||||
|
Subroutine
markup_links: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 43 | 12 | validate(@_, | |||||||||||
| 44 | { | ||||||||||||
| 45 | text => 1, | ||||||||||||
| 46 | handler => | ||||||||||||
| 47 | { | ||||||||||||
| 48 | type => CODEREF, | ||||||||||||
| 49 | optional => 1, | ||||||||||||
| 50 | }, | ||||||||||||
| 51 | }); | ||||||||||||
| 52 | |||||||||||||
| 53 | 12 | my %args = @_; | |||||||||||
| 54 | 12 | my $text = $args{'text'}; | |||||||||||
| 55 | 12 | ▶ | my $decorator = $args{'handler'} || \&decorate_link; | ||||||||||
|
Condition100%2/2
$args{'handler'} || \&Text::FindLinks::decorate_link
MC/DC100%1/1
$args{'handler'} || \&Text::FindLinks::decorate_link
$args{'handler'}
Truth table100%2/2
A
A= $args{'handler'}
| |||||||||||||
| 56 | |||||||||||||
| 57 | 12 | $text =~ s{( | |||||||||||
| 58 | ( | ||||||||||||
| 59 | (((https?)|(ftp))://) # either a schema... | ||||||||||||
| 60 | | (www\.) # or the ‘www’ token | ||||||||||||
| 61 | ) | ||||||||||||
| 62 | [^\s<]+ # anything except whitespace and ‘<’ | ||||||||||||
| 63 | (?<![,.;?!]) # must not end with given characters | ||||||||||||
| 64 | )} | ||||||||||||
| 65 | 13 | {&$decorator($1, $`, $')}gex; | |||||||||||
| 66 | |||||||||||||
| 67 | 12 | return $text; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | =head2 find_links(text => ...) | ||||||||||||
| 71 | |||||||||||||
| 72 | Returns an array with all the URLs found in given text. | ||||||||||||
| 73 | Just a simple wrapper around C<markup_links>, see the | ||||||||||||
| 74 | sources. | ||||||||||||
| 75 | |||||||||||||
| 76 | =cut | ||||||||||||
| 77 | |||||||||||||
| 78 | sub find_links | ||||||||||||
| 79 | 1 | ▶ | { | ||||||||||
|
Subroutine
find_links: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 80 | 1 | validate(@_, { text => 1 }); | |||||||||||
| 81 | 1 | my %args = @_; | |||||||||||
| 82 | 1 | my @links; | |||||||||||
| 83 | markup_links( | ||||||||||||
| 84 | text => $args{'text'}, | ||||||||||||
| 85 | 1 | ▶ | handler => sub { push @links, shift }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 86 | 1 | return @links; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | =head2 decorate_link($url, $left, $right) | ||||||||||||
| 90 | |||||||||||||
| 91 | Default URL handler that will be used if you don’t pass your | ||||||||||||
| 92 | own to the C<markup_links> sub using the C<handler> attribute. | ||||||||||||
| 93 | It turns an URL into a HTML link and skips URLs that are | ||||||||||||
| 94 | apparently already links. Not exported. | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | sub decorate_link | ||||||||||||
| 99 | 3 | ▶ | { | ||||||||||
|
Subroutine
decorate_link: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 100 | 3 | my ($url, $left, $right) = @_; | |||||||||||
| 101 | |||||||||||||
| 102 | # Skip already marked links. | ||||||||||||
| 103 | 3 | ▶ | return $url if ($left =~ /href=["']$/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 2 | ▶ | return $url if ($right =~ qr|^</a>|); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | |||||||||||||
| 106 | 1 | my $label = $url; | |||||||||||
| 107 | 1 | ▶ | $url = "http://$url" if ($url =~ /^www/i); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 1 | return qq|<a href="$url">$label</a>|; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | =head1 BUGS | ||||||||||||
| 112 | |||||||||||||
| 113 | The algorithm is extremely naive, a simple regex. It is almost | ||||||||||||
| 114 | certain that some URLs will not be recognized and some things | ||||||||||||
| 115 | that are not URLs will (to keep the balance). I’d be glad to | ||||||||||||
| 116 | hear if there is some URL that misbehaves. | ||||||||||||
| 117 | |||||||||||||
| 118 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-FindLinks> | ||||||||||||
| 119 | |||||||||||||
| 120 | =head1 AUTHOR | ||||||||||||
| 121 | |||||||||||||
| 122 | Tomáš Znamenáček, zoul@fleuron.cz | ||||||||||||
| 123 | |||||||||||||
| 124 | L<http://github.com/zoul/Text-FindLinks> | ||||||||||||
| 125 | |||||||||||||
| 126 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 127 | |||||||||||||
| 128 | Copyright 2009 Tomáš Znamenáček | ||||||||||||
| 129 | |||||||||||||
| 130 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 131 | under the terms of either: the GNU General Public License as published | ||||||||||||
| 132 | by the Free Software Foundation; or the Artistic License. | ||||||||||||
| 133 | |||||||||||||
| 134 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||
| 135 | |||||||||||||
| 136 | =cut | ||||||||||||
| 137 | |||||||||||||
| 138 | 5 | 1; | |||||||||||