how to replace urls in (user submitted) text with active links

development No Comments »

Ever had to display user submitted content on a web site you’ve created? Then you’ve probably had a requirement to display urls in submitted text as actual live links.

Despite the fact that this must have been done a gazillion times before, I couldn’t find any site that would give me a ready to use regular expression to turn text bits that look like URLs into actual hyperlinks. But some Googling, a couple of hours and regex-refresher later I had something that appears to work quite well:

in = "go check out www.sfpeter.com or http://sfpeter.com"
in.gsub!(/([^/])(www.([w/_.~])*)/, '\\1<a href="http://\\2" target="_blank">\\2</a>')
in.gsub!(/([^"])(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)/,
    '\\1<a href="\\2" target="_blank">\\2</a>')

this will turn both links in the input string into active links (that will open in a new browser window). It will consider strings that either start with www.<something> or http(s)://<something> to be a web URL. Users often don’t add ‘http://’ to links they submit, so this will still work as long as their links start with ‘www.’ .

One of the tricky issues was making sure that after replacing www.sfpeter.com with an active hyperlink, the second regex wouldn’t come by and substitute the link again, so you end up with something like this

<a href="<a href="http://www.sfpeter.com">www.sfpeter.com</a>">www.sfpeter.com</a>

To prevent this, the second regex ignores any urls that are preceded by a double quote.

This is Ruby code of course, but the patterns should work fine in any language. I could take it one step further and hyperlink anything that ends in .com, .net or other well known top level domains, so just sfpeter.com would be a match too, but I’ll leave that for later. I’m open to suggestions for improvement!

jquery rocks

development No Comments »

I’ve been working pretty insane hours lately, because I’m now working on three projects, of which one is a fulltime one. I’m enjoying it though. Ruby on Rails rocks, and it’s fun to be doing a lot of work on front end development again. One of the things I’ve actually been enjoying is Javascript development. The general consensus amongst J2EE developers seems to be that javascript is something that loosely resembles a real programming language like java, that it is useful to script hackers, and that implementing things on the server side is always better than in javascript. Especially if you want it to work in all browsers. I probably even made statements like that myself less than a year ago.

But after playing around with various javascript libraries like the google maps api, the yahoo UI library, prototype and scriptaculous for several months, I now beg to disagree with my former J2EE co-workers. Javascript is, in fact, a real programming language, and a really cool one too. The reason for my most recent excitement about javascript is actually jQuery. If you ever need to write any client side functionality for any site (and you probably do, if marketing is telling you that you need to start using Ajax, even though they don’t really know what that means), you owe it to yourself to check out jquery. Why is it so good? Because it allows you to keep your html markup nicely separated from your javascript code, because you will be able to write your functionality in half the amount of code of what you’d need if you weren’t using jquery, and because browser compatibility issues are a thing of the past with jquery. Props to John Resig for writing such a great library, and for the ever growing user community contributing jquery plugins and jquery based functionality. (Now the only thing that’s left on my wishlist is a tool or library that will make browser CSS compatibility issues a thing of the past too.)

Hopefully, I’ll be contributing to this jquery community myself too soon, since I’ve developed my own javascript library that ecommerce sites in particular may end up being very interested in. Stay tuned, more info to follow soon…

I’m a Ruby developer now!

geek No Comments »

Woohoo! I ran into a great opportunity to become a fulltime Ruby on Rails developer, so I’m leaving my current contract to go work for a new client. This new client will also be in San Francisco but I’ll be working from home for most of the time, which is nice because that will also make it easier to put some extra hours into my personal (Rails) project. Oh - I haven’t written about that yet here, but I will soon. In any case, it’s an exciting time to be getting involved with Ruby on Rails, ajax and all this web 2.0 stuff. I think I might as well go all out and buy a Macbook Pro laptop as well to start developing on… then I’ll be really hip and up with the times. Well, hip in a geeky kind of way I suppose.

Ubuntu upgrade gone bad

development No Comments »

I spent the entire day today trying to install PostgreSQL with PostGIS support on my Ubuntu Linux server. (PostGIS will add features for geographic queries to PostgreSQL, but more about that later). Unfortunately my previous experience with Ubuntu had led me to believe that installing this PostGIS extension would be a breeze. Wrong. I spent all day trying to download, compile and install all of its dependencies… flex, libgeos, proj, etc. Even with the excellent instructions on the PostGIS site I never got it to install and actually work in PostgreSQL. But then the instructions said “At Version 6.10 of Ubuntu, using Postgresql 8.1, postgis is available using apt-get.”… OK, the prospect of being able to install this with one simple command was reason enough for me to upgrade my Ubuntu 6.06 installation to version 6.10.

But disaster didn’t really strike until after the hour and a half download and installation procedure. Ubuntu told me it was time to restart but after restarting I never got to see my desktop again. The GUI crashes with the message “Server Authorization directory (daemon/ServAuthDir) is set to /var/lib/gdm but this does not exist.”, but the problem is even far worse than that. The /home/ directory is completely empty, so all my files are gone, as well as other directories such as /var/lib and /var/spool. The current state of my server is so dire that it looks like I have no choice but to wipe everything clean and install Ubuntu 6.10 from scratch now.

Fortunately there wasn’t any personal data on there, other than the programs I had installed, but I should have done some reading before I tried this. As it turns out there are many other people for whom this upgrade ended in a nightmare as well. Aarrrrggh! I don’t like to complain about open source software because many Ubuntu developers work hard for the community, for little or no pay, but I think that at least a warning on the installation site would have been appropriate here.

Thinkgeek customer service sucks

geek No Comments »

If you would have asked me a month ago about ThinkGeek.com, I probably would have told you that it’s one of the coolest sites out there for geek toys. But right now I’m no sure sure I’d give them a recommendation. Almost 4 weeks ago I ordered a couple of those Flickr enabled wifi photo frames called eStarling. Very nice, but ThinkGeek has been advertising these in their online catalog since early this year but never actually had them in stock. Finally, they announced that they would get a batch of them in early december so I ordered 3 of them for myself as well as for mine and Rachel’s parents. But over three weeks and 3 emails to ThinkGeek later, I’m still waiting and I have no idea when, or even if I’ll ever receive these frames. My first 2 emails to ThinkGeek were never answered, but I finally received an answer to my third email. Check it out, this is what I wrote them:

Read More »

Marty & Taylor

photography 1 Comment »

Marty and Taylor, our 8 month old kittens are not normal. Really. You’d probably have to meet them to believe it. They like to stay within 5 feet from us, whatever we do. And when they’re not seeking comfort from us, these brothers are seeking comfort from eachother. In any case, sleeping and lounging cats make excellent subjects for practicing my photography skills. Two of these photos were taken with my new EF-S 60mm 2.8f Macro Lens…

IMG_1940IMG_1867_2IMG_1821IMG_1762

Recording Sessions

music No Comments »

I spent all weekend in the studio last weekend with my band LowFat. We recorded at Hyde Street Studios in San Francisco, which was quite the experience, as they have some great recording gear and have had quite a few big names record albums there. Green Day, Cake, Spearhead, Chris Isaak, Robert Cray, Joe Satriani, the Grateful Dead, and many others. You’ve probably heard songs on the radio that were recorded in the same room that we were in. Whether or not we produced music of the same quality remains to be seen. Overall I was pretty happy with the sound quality (and the way the horns came through on the recording), but we could have been better prepared, and had to put 15 songs down in only 2 1/2 days. Some tracks are still unfinished, only scratch vocals were laid down, and there is a lot of editing, mixing and mastering work that will need to be done. Our singer’s wife is expecting a baby next week, and we’re all going to take a break from rehearsing and gigging for a few months. I expect we’ll start playing again and finish this album in early 2007.

Here’s some photos (on Flickr) of our recording sessions…

PC Crash

geek No Comments »

So I had a computer meltdown last week and spent all weekend fixing my computer. After messing around for a long time with my pc I finally discovered that something was going on with my motherboard or power supply. After running a while, one of the ATX voltages would start dropping, eventually resulting in spontaneous restarts (or not being able to start up at all anymore). What to do? I didn’t know either. I finally bit the bullet and decided to upgrade pretty much the whole system… motherboard, power supply, memory, CPU, and a new harddrive as well while I was at it. Unfortunately, as I was putting it together I discovered that AGP is apparently also an outdated technology, making my old $300 ATI graphics card useless, so a trip to Best Buy and a couple of hours of assembly work later I was ready to install the operating system on a virgin harddisk.

I’ve been reading a lot about Vista lately and Vista RC1 has recently been released… People are claiming it’s stable and nice looking so I figured why not give it a try. So far so good… well, almost good. I do like the new look a lot, as well as the gadgets on the desktop. But my favorite new feature is the search option from the start menu. Finally an easy (and quick) way to search for an application or document. Two major annoyances: Quickbooks won’t install on Vista, and all the security popups (’do you really want to run this as administrator?’) are driving me nuts… .. I hope there’s a way to disable those.
And still, my system is freezing up at random sometimes… I could blame vista for this, but I really think it might be some kind of memory or timing issue with my new motherboard. Probably need to play around with the bios settings a little more. I think I remember now why most other people buy complete PCs instead of a dozen separate components…

Lightroom

photography No Comments »

I’ve only owned my Canon EOS 30D for less than two months so I have a lot to learn still about digital photography. Only recently I discovered what an amazing difference a few simple tweaks to white balance, color temperature or hue can make to a digital photo. You could argue that I shouldn’t need to do any color adjustments if I took the photo right in the first place, but I bet that even the best of digital photographers use photoshop to fix and enhance their work.

I’ve been using Photoshop Album for my point-and-shoot camera, which is great for organizing photos but not so great for fixing them up. Adobe Photoshop can do it of course, but editing a whole set of photos is cumbersome and the controls are not that easy to get to. I think I’ve found the answer to this problem and it’s called Adobe Lightroom. It puts all the controls that a digital photographer needs right at your fingertips and can manage entire photo shoots. It will simply save your settings in a project file and will not touch your original shots. When you’re all done you can export your photo selection, but you can still go back later on and tweak more if you need to. I can’t wait to get started with raw images now, which Lightroom supports as well (in fact, it was probably designed for raw editing).

Lightroom is still in beta, which is downloadable for free. It’s a little slow sometimes and it seems to be missing some basic features you’d expect to find (like a full screen view or the ability to remove red-eye effect), but all in all in all I think it’s already a great tool for digital photographers. Check it out.

new ISP

general No Comments »

So I switched to a new ISP… Eapps has been causing me too much headache lately and I didn’t want to deal with administering a Linux virtual server any longer. A no-root-login Linux ISP like Bluehost will do just fine. Plus, I have 10 times more disk space here for less than half the price.

Likewise for Mambo… it was cool and all, but the site was acting up so an upgrade would have been appropriate. I decided not to stick with Mambo and migrate to Wordpress instead. I still think Mambo/Joomla is very cool, but I just have better things to do than maintaining a full CMS with all its bells and whistles. A simple blog is all I wanted anyway.