Archive for June 2009

Regex for replacing URL with anchor tag

Despite there being 1001 regexes on the web for matching a URL in a string and then replacing it with an anchor tag (commonly used in chat applications etc.), I couldn’t find one which did exactly what I wanted, so I thought I’d share my regex for replacing URLs with anchor tags.

Firstly, the problem with all the other regexs I found. Take for example the following text block:

you should have a look at <a href='http://example1.com'>http://example1.com</a>, and http://example2.com

All of the regexes I found for matching and replacing URLs with anchor tags would have transformed my text block into:

you should have a look at <a href='<a href='http://example1.com'>http://example1.com</a>'>http://example1.com</a>, and <a href='http://example2.com'>http://example2.com</a>

None of the example regexes I found coped with the possibility that some URLs may already be contained in an anchor tag, as in the example above, or in other instances where you don’t want to insert an anchor tag, such as: <img src='http://example.com/logo.png'/>

The regex and code I am now using to get around this problem is (in Javascript):

var mystring = "text with some URLs in";
var myregex = /(^|[^'">])((ftp|http|https):\/\/(\S+))(\b|$)/gi;
var newstring = mystring.replace(myregex ,"$1<a href='$2'>$2</a>");

All appears to be working well so far, but I’m sure my regex is far from perfect. This regex doesn’t check that the URL is really well formed, for example valid top level domain etc.

So please post any possible improvements below, in the meantime I hope it’s useful 🙂

Event mapping

eventmapAfter a little time away from programming and active web development, I thought I’d get back into things by giving myself a little project to create a Google maps and Upcoming mashup.

At the moment it’s fairly basic as I’ve only spent a few hours working on it, but seems to be working well. I’ve deliberately tried to keep the number of options and fields down to a minimum (I know there are far more options and functions I could add).

Enter some search text in the ‘What?’ field, then a location in the ‘Where?’, finally select ‘When?’. All the events matching your search (within a 50mile radius of the location) will be shown on the map. Click on the marker to get more info about the event.

Neither the ‘what?’ or ‘where?’ field are required, either or both may be left blank. If the ‘what?’ is omitted then all events in the area are shown and if the ‘where?’ is omitted then the centre of the current view of the map is used.

All the events are from Upcoming and you can use their interface to add your own event, to then appear on the map.

For the technically minded, the site uses the Google Maps API (including for geocoding the location) and the Upcoming API.

If you know of any other feeds I could use for adding events to the map, then please let me know so I can try adding them.

I’d also be grateful for any feedback, problems or suggestions – just leave a comment below.

Donations reduced to 100% of target

New widgetThe JustGiving website has a had a bit of a redesign, which meant that my sidebar widget showing how much money I’d raised stopped working. I’ve now updated to use their new widget, which, by coincidence, happens to match the colour scheme of my blog.

The only downside I’ve found so far is the new widget only goes up to 100%. I’d raised 106% of my target, which the old widget displayed, but the new one doesn’t.
Old JustGiving widget
If you feel like proving it to yourself that it won’t got over 100%, then please feel free to make a donation 😉

Attempts at video editing on the Asus Eee

RecordMyDesktopI’ve used Camtasia Studio (Windows) a few times before for creating some training/how-to screencasts, but now I wanted to have a go doing something similar using open source software (on Ubuntu Jaunty) instead. To make it more interesting I also wanted to run it all on my Asus Eee PC (4G) – not exactly a machine designed for heavy video processing.

First issue was how to record the screen, for this I used RecordMyDesktop. A very simple program to install and run, which does exactly what it says on the tin. Selecting the area to record (I wasn’t selecting whole window) was a bit fiddly and sometimes took me several attempts to capture the right area, without chopping off a side or two.

If you’re recording a for a long time and your screensaver kicks in (or screen goes blank for power saving), you’ll need to disable the screensaver and power saving – unless you particularly need a recording of it.

What I had most trouble with – and only came up with a workarounds solution to – was capturing the audio. RecordMyDesktop wil capture the audio fine but had problems getting the recording volume level right without creating a lot of hissing. The hardware on the Asus for sound isn’t exactly fantastic. In the end I found that I had best results when using a jack lead to plug the headphone output into the microphone input! If anyone has a better solution to this or some advice about on the recording levels to capture the audio more reliably, then please let me know.

So, now I’ve created a .ogv (OGG video) file but want to edit it a little. I wanted a really simple video editor for 2 reasons, firstly I don’t wand to spend the time learning a bit video editing package and, more practically, secondly, I don’t have a huge amount of disk space left on my 4Gb drive!

I opted for Aviremux, though unfortunately this doesn’t accept OGG video files, so I used mencoder to convert to AVI (basic instructions).

For the actual editing I used Aviremux. I found it very easy to get started with and use – unlike some other video editing software.

In the end I found that my original screen recording had sound out of synch with the video – not in the initial few mins of the capture, but further in. I’m not sure why this should be the case – if anyone has any idea why, I’d be pleased to hear them.

Overall, it was a little frustrating getting this to work. I’ve not managed to achieve what I set out to do, but think this was more to do with the hardware than anything else.

Blog tinkering

IMG_4216Over the last few days I’ve added a few features to my blog which other blogs have had for ages…

  1. Comments subscription. Get email updates to post’s comments. I’ve never quite understood why this isn’t a standard feature of WordPress. Does anyone ever subscribe to an RSS feed of comments? I don’t think I ever have.
  2. Gravatar. Updated my theme to include comment author’s gravatars
  3. Tag cloud. Seemed an obvious thing to add to the side bar, given the time I’d spent tagging old posts.
  4. Clustr maps. A little sidebar widget to show where the site visitors are coming from.

As, I assume, most people will be reading this through a feed reader, all of my tinkering will be irrelevant/invisible. Which means you won’t see the long (pointless?) sidebar of widgets now on display.

Does anyone ever look at these sidebar widgets? I’m sure when I view others’ blogs/sites I tend to block out anything down either side of the page assuming it’s peripheral, unless it looks like a menu for navigation. So why should I expect anyone to look at the ones on my site?

On the OpenLearn project the number of Moodle blocks (down either side of the page) grew to an unmanageable number. Deciding which (or rather whose!) block should be removed or put at the top was a tricky task. Though, this problem has now been solved by moving all the tools into one block.

[The photo above, which I took yesterday, has nothing to do with this posting, other than to show I have done something else over the last few days apart from tinkering with my blog.]

Using WordPress categories or tags?

tagsWhilst I have the time I’ve been fixing a few little things on my blog, as well as doing some tidying up. One thing I wanted to do, but wasn’t really sure whether or not it was worthwhile, was look at adding tags to my blog postings. Plenty has already been written (start here) about the difference and where and when you should use each.

When I’d started the blog only categories were available, so I’ve just continued with this, almost ignoring the tag facility, despite the fact that tags seemed a more flexible approach. Using the categories, with their hierarchical nature, felt as though you ought to come up with some big taxonomy for organising blog categories.

I’ve now severely trimmed the number of categories, whilst created umpteen tags – so is that an improvement for my blog? will it help people (including) find what they want? I’m not sure!

An interesting side effect of tagging (nearly) all my blog postings, was that I got to look back through what I’d written years ago and remember some of the projects and problems encountered – fixing many of the numerous typos on the way.

I also noticed how tweet like some of my early blog postings were, e.g. here and here – OK, maybe not 140 characters, but close.

Blog now OpenID enabled

I’ve finally got around to allowing you to post a comment using an OpenID, by installing one of the WordPress OpenID plugins.

I had tried to do this over a year ago, but couldn’t quite get it working correctly. I had a number of problems before, notably that my hosting server only ran PHP4, whereas (at the time) most OpenID plugins seemed designed for PHP5, or a specific version of PHP4 that I wasn’t running. The other main factor was that most of the OpenID plugins at the time were very early (beta or earlier) versions, so may have been a little buggy still, whereas the plugin I installed today was very easy to get set up and running.

Anyone using a verified OpenID to post a comment on this site will find that their name and email is not required, plus your comments skip moderation.

Please let me know if you find any problems in using your OpenID on this site.

Online Users Map for Moodle 2

moodleAfter a long time of not doing much (ok, any) development on my Moodle online users map block, I’ve finally had chance to return to the code to make the necessary changes for the block to run on Moodle 2.

It was always going to be tricky trying to maintain this block whilst in Ethiopia, not only with the slow internet connection, but the university firewall blocks many ports and this is my normal internet access point. So now that I’m temporarily back in the UK, I thought I’ve got chance to get back up to speed.

In the end only minor changes were needed to get the block running on Moodle 2. If you’re downloading the block from the Moodle modules & plugins pages, then please check you get the right one (v1.9 and earlier or v2) for your Moodle installation.

If you’re getting the code via CVS then HEAD currently contains the version for v2 and the MOODLE_19_STABLE branch has the version for 1.9 and earlier.

I’ve also set up a demonstration Moodle site so you can see the block in action. Just sign up, and you should see yourself marked on the Google map. You may have to wait until the location lookup script has run before you appear on the map, but this should only be a few minutes.

Please let me know if you have any problems with either getting the block to run on Moodle 2 or with the demonstration site.

Personal FAQs

Apologies to those of you who I didn’t get to see when I went up to the OU a couple of days ago, but hopefully I’ll be up again soon (maybe for the open unlimited day in couple of weeks), here is a very brief summary of the days FAQs (in hindsight I should probably just have done presentation at the OU!):

How am I feeling?
Good, although not yet 100%

What’s wrong with me?
Not yet sure, doctor has a few ideas, but nothing confirmed until I get my blood test results back sometime next week – if they say anything at all.

When am I going back?

Hopefully soon, as soon as I can get medical clearance to return. All is going to depend on when the docs find out what’s wrong and how quickly it can be sorted.

Have I made a difference?
Difficult to tell, probably better asking my colleagues, obviously I hope that I have, but maybe it’ll take a little while for anything to show.

Am I enjoying it?
Certainly, despite getting ill, I’m very keen to get back asap.

When am I coming back to UK?
Unsure yet, my contract is due to finish in Sept, but I’ve requested an extension to my contract, so might not be until March 2010 (if my extension is approved)

What am I going to do when I come back?
hmmm… not sure yet!

Sorry for the rather succinct answers, but you probably don’t want the full ins & outs, ups & downs of my illness!

Finally, I found this map the other day and found it quite amusing:

africa

[Image from: GraphJam]

Ethiopia photo album

I’ve put up a selection of the photos I’ve taken so far in Ethiopia – in case you have a spare 5 (or so!) minutes. I’ve not yet had chance to get them all labelled up, but will do when I get the time.