convert links to links

6 views
Skip to first unread message

Balazs Koren

unread,
Oct 5, 2009, 6:59:57 AM10/5/09
to habari...@googlegroups.com
Alright sorry for the dumb title. What I'd like to have is what is
default in wordpress and other cms'. If I type in a link it will be
automatically converted to a clickable link on the site.

For example I type in my comment http://habariproject.org it will be
displayed as <a
href="http://habariproject.org">http://habariproject.org</a> and I
don't have to add the anchors myself. Is there a plugin somewhere to
support this functionality? Thanks.

Balazs

--
http://kobak.org
http://fotolog.hu

rick c

unread,
Oct 5, 2009, 7:37:09 PM10/5/09
to habari-users
On Oct 5, 6:59 am, Balazs Koren <balazs.ko...@gmail.com> wrote:
> Alright sorry for the dumb title. What I'd like to have is what is
> default in wordpress and other cms'. If I type in a link it will be
> automatically converted to a clickable link on the site.
>
> For example I type in my commenthttp://habariproject.orgit will be
> displayed as <a
> href="http://habariproject.org">http://habariproject.org</a> and I
> don't have to add the anchors myself. Is there a plugin somewhere to
> support this functionality? Thanks.
>
> Balazs
>
> --http://kobak.orghttp://fotolog.hu

Balazs,

I don't know of any plugins that will do that for you.

Rick

Caius Durling

unread,
Oct 5, 2009, 8:16:25 PM10/5/09
to habari...@googlegroups.com
On 5 Oct 2009, at 11:59, Balazs Koren wrote:

For example I type in my comment http://habariproject.org it will be
displayed as <a
href="http://habariproject.org">http://habariproject.org</a> and I
don't have to add the anchors myself. Is there a plugin somewhere to
support this functionality? Thanks.

It adds a lot more than just auto-linking, but if you install the markdown plugin you can write <http://foo.com> and it'll autolink it for you.

I'm pretty lazy when it comes to penning things like blog posts, so I love writing them in markdown[1], makes writing stuff *really* easy, and then it all displays nicely when you hit publish :)



Balazs Koren

unread,
Oct 6, 2009, 5:07:55 AM10/6/09
to habari...@googlegroups.com
Thanks Markdown is cool, but the problem isn't myself, but the
commenters, who post a link, and it won't display clickable, what they
are used to. It's not a great problem, but would be nice, if there is
a solution for it.

Balazs

Ali B.

unread,
Oct 6, 2009, 7:34:42 AM10/6/09
to habari...@googlegroups.com
Hey Balazs,

Add the following to your active theme's theme.php, within the theme class definition

public function filter_comment_content_out($content){
    return preg_replace( '([http:\/\/]\S*)', '<a href="$0" title="Visit $0">$0</a>', $content );
}

URLs that start with "http://" will be converted to an anchor pointing to that URL. I hope it's what you need.


--
Ali
http://awhitebox.com

Balazs Koren

unread,
Oct 6, 2009, 5:47:34 PM10/6/09
to habari...@googlegroups.com
Hey Ali,

thanks, but it does something creepy. I understand it should work, but
not really that hapends:

http://screen.kobak.org/8d2c910f.png - original comments
http://screen.kobak.org/af046ce3.png - same with the filter activated
http://screen.kobak.org/fe6387e9.png - original source
http://screen.kobak.org/e7e637c8.png - source with the filter.

The filter is somehow activated even if there is no http:// . Is there
a failure in my side?

My theme.php : http://pastie.org/642080

I pasted the code after line 53.

Thanks for an further advice.

Balazs

drzax

unread,
Oct 6, 2009, 6:31:48 PM10/6/09
to habari-users
The regex just isn't quite right. Try this:

public function filter_comment_content_out($co ntent){
return preg_replace( '(https?:\/\/\S*)', '<a href="$0" title="Visit
$0">$0</a>', $content );

I've also added a little something so it will recognise http://

S.


On Oct 7, 7:47 am, Balazs Koren <balazs.ko...@gmail.com> wrote:
> Hey Ali,
>
> thanks, but it does something creepy. I understand it should work, but
> not really that hapends:
>
> http://screen.kobak.org/8d2c910f.png - original commentshttp://screen.kobak.org/af046ce3.png - same with the filter activatedhttp://screen.kobak.org/fe6387e9.png - original sourcehttp://screen.kobak.org/e7e637c8.png - source with the filter.
>
> The filter is somehow activated even if there is no http:// . Is there
> a failure in my side?
>
> My theme.php :http://pastie.org/642080
>
> I pasted the code after line 53.
>
> Thanks for an further advice.
>
> Balazs
>
>
>
> On Tue, Oct 6, 2009 at 1:34 PM, Ali B. <dmond...@gmail.com> wrote:
> > Hey Balazs,
>
> > Add the following to your active theme's theme.php, within the theme class
> > definition
>
> > public function filter_comment_content_out($content){
> >     return preg_replace( '([http:\/\/]\S*)', '<a href="$0" title="Visit
> > $0">$0</a>', $content );
> > }
>
> > URLs that start with "http://" will be converted to an anchor pointing to
> > that URL. I hope it's what you need.
>
> > --
> > Ali
> >http://awhitebox.com
>

Ali B.

unread,
Oct 7, 2009, 5:48:32 AM10/7/09
to habari...@googlegroups.com
Indeed. Using the square brackets is what messes things up as it will match any of the enclosed characters. I apologize.
Use what drzax suggest. Thanks Simon.
--
Ali
http://awhitebox.com

Balazs Koren

unread,
Oct 8, 2009, 3:41:01 PM10/8/09
to habari...@googlegroups.com
Thank you both for your help. It works.

Balazs

Lenard, Gabor

unread,
Oct 5, 2009, 11:20:41 AM10/5/09
to habari...@googlegroups.com
I use the Markdown plugin that does things like that (although a URL must be enclosed in <> signs to be converted).

Consider installing it as Markdown makes adding markup to your text quite natural (lists, quotes, etc.). See here: http://daringfireball.net/projects/markdown/

Gabor

Balazs Koren

unread,
Oct 22, 2009, 11:19:20 AM10/22/09
to habari...@googlegroups.com
Hi all,

I just googled a bit because links ending with /another dir etc. were
not really recognized with the simple regexp.
I found the following, but thanks again to pointing me for looking for
a regexp: http://codesnippets.joyent.com/posts/show/2104

So the function in my theme.php is now:

public function filter_comment_content_out($content){
$pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";
return preg_replace($pattern, '<a rel="external" href="\0">\0</a>', $content);

It converts any url to html link. Maybe someone else can use it too.

Balazs

Ali B.

unread,
Oct 22, 2009, 11:34:12 AM10/22/09
to habari...@googlegroups.com
Thanks so much for sharing this back with the list, Balazs.
--
Ali
http://awhitebox.com
Reply all
Reply to author
Forward
0 new messages