Suggestion: URLs clickable

Started by
28 comments, last by superpig 16 years ago
It would need to cover both image and link tags, but more importantly, it needs to only capture opening-tags that haven't also already been closed. I could work out something like this, but I'm wondering whether anyone's already got some neat tricks for capturing "freetext" URLs in text that may contain regular HTML as well.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Advertisement
I had forgotten about img tags as well. I see two possible options though (forgive me if the regex isnt correct, I'm making it up as I write this):

1) Something simple like

<(a|img)\s+[!>]*\s*(href|src)"?URL regex"?[!>]*>[!<]+<//(a|img)>

which matches <a href="URL">bla</a> but also matches <a src="URL">bla</a>

2) Two different regexes for img and a (those are the only tags the forum supports which have URLs, yes?)
Yes, I think that works - but remember, the problem is to identify URLs that are not within those tags. Writing a regexp to pull a tag and its component parts is comparatively easy; I'm not sure if you can just 'invert' it somehow, I think you can't.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

You're right, I'm being completely backwards right now. You can probably safely ignore everything I've said or what I'm about to say. [smile]

I dont think you can invert it from within the regex itself but if you can identify the expressions you dont want to linkify then you can mark them in some way so that your later search-and-replace-to-linkify regex wont linkify. Maybe if you inserted some non-typeable character into the URL itself you could run the linkify regex and then remove that character afterwards.
Ah, as a two-pass process? I guess that could be made to work, but there /must/ be a more elegant solution.

Hmm. Here are my testcases:

<a href="http://dont.link.this/">link but http://do.link.this/
<a href=http://dont.link.this/>link but http://do.link.this/
<img src="http://dont.link.this/">link but http://do.link.this/
<img src=http://dont.link.this/>link but http://do.link.this/
Your so-called "http://link.this/" domain...
My site=http://link.this/

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

If you implement auto-linking URLs, you also need to provide a way to prevent it from being done - sometimes, somebody wants to talk about a "bad" site (whether it tries to install malware, is a scam, or is otherwise malicious) and wants to present the address but really doesn't want to link to it (lest somebody click on it without reading the text and have bad things done to them or their computer).
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Oluseyi
Once you scratch the surface, it's not nearly as simple an issue as it initially may appear - not when done right.
Just use whatever is in the url tags, and replace with a link. What do TLDs have to do with it? It's a simple text replace. No different than bolding text.

Also, the original poster, you can make a clickable link by using the html code.
GOOGLE!

  GOOGLE!
Quote:Original post by Vampyre_Dark
Just use whatever is in the url tags, and replace with a link. What do TLDs have to do with it? It's a simple text replace. No different than bolding text.

It can be hard to tell because of the edits that the OP made, but the thread is about plain text links. Such as http://this.is.my.website. To make those clickable requires a regex, which is very different from bolding text.
How about adding a url button to the posting... problem solved. Then you don't need a regex.

You could do a img button, a url button, and a you tube button, so the rick rolls are brought to us instead.
Black Sky A Star Control 2/Elite like game
Quote:Original post by ViperG
How about adding a url button to the posting... problem solved. Then you don't need a regex.
Doesn't really fit the profile of 'automatic' linking, that. Not that it's a bad idea, just a separate one.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement