Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - converting URL into links, images, objects

I'm creating simple comment-like application and need to convert normal urls into links, image links into images and yt/vimeo/etc. links into flash objects. E.g.:

http://foo.bar to <a href="http://foo.bar">http://foo.bar</a>
http://foo.bar/image.gif to <img src="http://foo.bar/image.gif"/>
etc.

Of course i can write all of that by myself, but i think it's such obvious piece of code that somebody has already wrote it (maybe even with splitting text into

paragraphs). I was googling for some time but couldn't find anything complex, just few snippets. Does filter (or something like that) exist?

Thanks!

PS. There is urlize but it works only for the first case.

like image 319
radious Avatar asked Oct 13 '22 21:10

radious


1 Answers

Write a custom filter to handle all the necessary cases. Look at the source code for urlize to get started. You'll also need the urlize function from utils.

In your filter, first test for the first case and call urlize on that. Handle the second case and any other cases you may have.

like image 89
rz. Avatar answered Oct 18 '22 04:10

rz.