Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

One way of doing it would be defining a hash whose keys are the keywords (the ones you want to provide links for) -- this will work with phrases too, BTW -- and whose values are URIs or URLs. Then just use s///g to do it (that's the quick n' dirty way).

#!/usr/bin/perl -w use strict; my %keywords = ( foo=>'/foo.html', bar=>'bar.html', cult=>'http://perlmonks.org' ); my $textfile ="/file/to/link"; open TEXTFILE, $textfile or die "Yikes! $textfile won't open: $!\n"; my $data; { local $/; #sets input list separator to undef #so we can 'slurp' the file in $data = <TEXTFILE>; } close TEXTFILE; foreach (keys %keywords) { my $url = $keywords{$_}; # EDITED ... I'd forgotten to escape # the / in front of the closing <a> # credit to Albannach $data =~ s/($_)/<a href="$url">$1<\/a>/g; } # now do something with $data

But that's *oh so quick* and *oh so dirty*, it probably raises a lot of problems and only works on very simple kinds of words (e.g. if you have phrases in your keywords hash, you could really mess things up).

UPDATE Implementing something like this site's linking mechanism wouldn't be so hard. just put delimiters around the words / phrases you want to link, and something like the above should work fairly well. E.g. [bob] would say 'put a link around "bob"' (what link, you could determine in a number of ways, but the hash idea seems to work well enough.)

Just alter the above substitution line to:

$data =~ s/\[($_)]<a href="$url">$1<\/a>/g;

Bonus: that takes care of phrases, too. (credit for this idea goes to whathisname

Seems like there must be a module that does something like this (I'll go to CPAN and give you an update)

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to Re: Auto linking to words in a text file by arturo
in thread Auto linking to words in a text file by belize

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 06:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found