Hi Jamie,

On Thu, Jul 17, 2003 at 11:46:08AM -0400, Jamie Risk wrote:
> I'm trying to add HTML anchors to a lines of text.  Quick example would be
> to take the line:
>    "Search the internet using an engine like Google."
> and turn it into:
>    "Search the internet using an engine like <a
> href="www.google.com">Google</a>."

> I can do this in a hacking sort of manner but it'll be slow and inelegant.
> I apologize for asking for design template/suggestions rather than
> help, 

Don't sweat it--those qualify as help too.  Personally, I think it
shows you're thinking.


As always, TIMTOWDI.  The best way depends on other constraints--where
are you planning on running this?  What kind of load will it see?  How
cross-platform does it need to be?  etc.


One solution is to tokenize your files yourself, then Reduce To
Problem Already Solved. (*) You can do a rough version of that with
something as simple as:

my $delims = qr/[^-\w']+/;
my @tokens = split /$delims/o, $text_of_file;

Or you can check CPAN (there is a Parse::Tokens module, but I haven't
used it; there are probably others too).  


Another way is to do a  s/$token/$replacement/eg on the text of your
file...it may not work perfectly, but it should go a long way.


A heavier-weight solution would be to use a real templating system
like Mason, The Template Toolkit, or Text::Template (to name a few...I
think CPAN has over 20).  Mason and TTT both come very highly
recommended to me.


--Dks

(*) Note that tokenizing English is hard--how do you handle hyphens vs
em-dashes (this sentence offering examples of each)?  You can't forget
the 'apostrophes vs single-quotes' issue either, and remember that
some possessive words' apostrophes are at the end.  How about
abbreviations with embedded punctuation (e.g. i.e.)?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to