It's Friday afternoon and my brain is fried.
I have a boatload of plain text that contains things that look like http links. If I find one, then I want to wrap it in HTML anchors.
I don't think there's a module that does this, but I welcome suggestions. Otherwise, the spec is in the test below. If someone could show me the error of my ways I'd be a happy man.
Bonus points for converting the first link (http://www.example.com) to http://www.example.com/ in the HREF (add the omitted slash).
use strict;
use warnings;
my $html = <<END_HTML;
Blah blah blah
Web: http://www.example.com
info.example.com
Web: http://info.example.com/
And another
Web site: http://another.example.com/ (doesn't always work)
Nice blog here: http://blog.example.com/niceblog/
END_HTML
my $target = <<END_HTML;
Blah blah blah
Web: <a href="http://www.example.com">www.example.com</a>
job.example.com
Web: <a href="http://info.example.com/">info.example.com</a>
And another
Web site: <a href="http://another.example.com/">another.example.com</a
+> (doesn't always work)
Nice blog here: <a href="http://blog.example.com/niceblog/">blog.examp
+le.com</a>
END_HTML
use Test::More tests => 1;
$html =~ s{(http://(\S+)(?:/\S*)?)}{<a href="$1">$2</a>}g;
is($html, $target);
• another intruder with the mooring in the heart of the Perl
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|