Web dev & programming

Regex for replacing URL with anchor tag

Alex June 26th, 2009

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 :-)

Chisimba framework

Alex June 8th, 2009

chisimbaRecently I was sent link to Chisimba, a ‘Web 2.0 enabled, Free Software development framework for creating web and distributed applications’, developed by African Virtual Open Initiatives and Resources (AVOIR). From the info on the site it looked an interesting project, although my first impression was that its trying to be all things to everyone, with modules for blogs, wikis, CMS, LMS and more. It appears to have taken elements from Moodle, Joomla, Drupal and Elgg and packaged them all into one application, which may or may not be a good thing.

On downloading and installing on my laptop, I soon found that this was still very much a work in progress, as there appeared to be lots of little bugs/annoyances that would probably have been resolved if it was a little more developed. A very minor example of this was the fact that the next/previous buttons on the installation were the wrong way round (for me anyway), with next being on the left hand side and prevous on the right, rather than the more usual arrangement. The installation itself was a little fiddly, however that was mainly because I had to upgrade my version of PHP, install some PHP libraries and some PEAR libraries too, before it would install.

A lot of effort seems to have gone into creating modules, 100’s of them by the looks of it, so thought I’d take a look at installing the blog module. Unfortunately the automatic ‘download and install’ kept giving me an error, so then tried an offline installation of the module – a 35Mb download of all(?) the modules is available – but I couldn’t find any info on how to install a module in this way. I’ve posted a query on their discussion forums, so hopefully I’ll get a reply to have this resolved soon.

Overall I was a little disappointed that I couldn’t have a proper play with the modules, but will come back to it once I get a resolution as to how to install them. Having said that I can’t see this framework taking off, there are many other established frameworks out there and I don’t think Chisimba offers anything different enough from these.

Ubiquity

Alex August 28th, 2008

Mozilla Labs have just launched the Ubiquity plugin for Firefox and to me it looks fantastic. The plugin makes it really easy to embed a map say into an email – without all the faffing around of cutting and pasting URLs from Google Maps into emails etc – it also does much more than this – but I’ve only just started to play!

Even though I’m not generally one for command-line type interfaces, this one works for me. Will be interesting to see how many developers jump on this to create new commands. I can see how you might want to add an IM command (for MSG of course) or one for Cohere. Also, how will this affect some other Firefox plugins, for example if Delicious wrote a set of commands for this (maybe they already have?) then would you really need the Delicious Firefox plugin?

When doesn’t “removeelement” remove an element?

Alex August 5th, 2008

When you’re trying to use XUL. I’ve just been working on enhancing the Cohere Firefox plugin/extension/add-on so that you can optionally show a toolbar, this toolbar will also show the ideas from Cohere which are linked to the webpage you’re currently looking at (quick screengrab below):

Anyway, getting back to my original point, I needed to be able to dynamically change this drop down list whenever you browse to another page, or select another tab. Getting my plugin to go off and query the Cohere webservice to find the relevant ideas all worked fine, the problem I had was being able to remove the current items from this drop down list.

The documentation seemed to show that I can use ‘removeelement’ to remove the element from the list by setting the removeelement attribute to true (though does seem to be bit of an odd way to do things – setting an attribute to remove an element), but it didn’t work and I spent a little while figuring out why .

In the end it turned out that setting the removeelement attribute to true only means that the element is removable, so you can create the following:

<menulist id="menulist">
    <menupopup id="menupopup">
        <menuitem label="my label" value="a value" removeelement="true"/>
    </menupopup>
</menulist>

but the item “my label” will appear in the drop down list, to actually remove the item you need to do something like this in your javascript:

var ilist = document.getElementById('menupopup');
for (var i=0; i<ilist.childNodes.length;i++ ){
    ilist.removeChild(ilist.childNodes[i]);
}

Thought I’d post it up here as it confused me and might save others struggling to figure out what’s going on ;-)

Recording Video on Asus Eee

Alex June 22nd, 2008

I’ve just spent a frustrating couple of hours unsuccessfully attempting to get my Asus Eee (running Ubuntu Hardy Heron) to allow me to record short videos using the built in webcam and mic.

I started off by applying these fixes and have checked that it all works fine using skype. I first tried recording using ‘Cheese’ – this resulted in the video working fine – but no sound ( I also tried with plugged in mic/headphones). So I installed ucview and gave that a go instead,  but his was quite erratic too, most of the time it would appear to record, but then on playback, only the first frame would be visible, and no sound (using either built in mic or a plugged in mic). I also found that sometimes ucview wouldn’t stop recording even after I’d clicked the button and I had a to kill the process to get it to stop.

I then went back to looking at cheese only to find that it now won’t start, the top window bar appears, but with big grey square underneath – no menus or webcam preview. Restarting, removing and reinstalling all made no difference :-(

Really not sure what is causing these problems, from googling & reading wikis and blogs most other people seem to have got ucview and cheese working fine – so just me then ;-) If anyone has an idiots guide as to what to check then feel free to send on to me

I’ve been trying to get this working in the hope that when I go away to Ethiopia I’ll be able to record short video blogs to post up – but I may have to fall back to just using my digital camera and the video/sound recorder on that, but would’ve been nice to be able to use the webcam and mic.

On a different note, I hope that Ubuntu updates will soon include FireFox3…

More IE/CSS/div fun

Alex June 18th, 2008

Not being a designer spending all my time getting to know the ins and outs of how floating divs work, it tends to take me a little while to get my head around how to lay out pages using pure CSS. I thought I’d pretty much got how to do 2 and 3 column layouts with CSS and divs, but today I found a new problem which really had me stumped for a while.

Essentially I wanted an ordered list, then within each <li> element a couple of <div>s which I could float to layout how I wanted. Everything was working fine in Firefox, but not in IE(6), the images below show the output given in Firefox and IE:

IE (6):

Firefox:

HTML code which generated the above images.

The items numbered 1-3 show how I want the output to appear, but my first stab at the code resulted in items numbered 4-6. As you can see, in Firefox it appears fine, but in IE the ‘float:left’ is being ignored, making the div containing the text appear below the checkbox. [Aside: you might ask why I'm using divs and floats for something as simple as just some text next to a checkbox, well, this is just my starting point, my final page will be more complicated than this example!]

The only difference between the working and non-working examples is that the div containing text has width property defined (set to 100%) in the working version. I’m not sure why this should be necessary – my example shows that it’s not needed if the wrapper div isn’t part of an ordered list.

My only explanation is that this is another (frustrating) little IE CSS bug – unless anyone can give me another explanation? Alternatively if there are better ways to do what I’m trying to achieve?

Through the Square (javascript) Window

Alex May 22nd, 2008

For the Cohere site, I’ve been trying to figure out how you can keep track of a pop up window using Javascript even when the parent page changes (to another page on same domain) and to know whether the pop up window is already open. I’m having exactly the problem mentioned in this forum post – but as yet no solution.

All the example scripts I’ve found so far will only work if the parent page remains the same. I.e. you’re testing whether the pop up is already open, but applying this test on the document/page that originally opened the popup. What I’m looking to do is subtly different – so the pop up remains open so as you are browsing Cohere you can add ideas to make connections between.

I would think that this ought to be possible, because if you open a popup window with the same name as an existing one, the old one will get overwritten – so it ‘knows’ that the pop up exists. Plus, this is all running from the same domain, so there won’t be any cross domain issues.

If anyone has any pointers as to how to achieve this, it would be much appreciated. Or if you have other suggestions for how the same results could be achieved. Cheers ;-)

Cohere reCaptcha-ed

Alex May 22nd, 2008

Yesterday, we discovered that some of the users on Cohere were actually spam, the usernames were random characters and the descriptions on these users profiles consisted entirely of links for loans, pills etc (all the usual spam). To try and stop this happening again I’ve added reCaptcha to the user registration form.

This was really easy to implement, the only slight change I had to make was to fix the code so requests to the recaptcha servers went through our proxy (shame there aren’t optional params to add proxy details – but easy enough to fix).

Time for Timelines

Alex May 14th, 2008

A couple of weeks ago, Peter (one of the OU graphic designers) asked me if Cohere could produce nice graphical timelines (the BBC has some really nice ones). He’s been tasked to produce one for one of the OpenLearn courses and was thinking about the best way to do it – and so it could be replicated easily (when he gets asked to make another one for another course). Peter pointed me at Simile from MIT – which is a kind of Google Maps API for timelines.

My response was that Cohere couldn’t currently provide exactly what he was looking for (we’ve not really worked on many different visualisations yet), however, you could use Cohere as the repository for providing data (via the Cohere API) to Simile. All we’d need to do would be add the facility to add a date to an idea/node, and another formatting output options to get the data in the way that Simile requires. (Note to self: should also add location option for ideas/nodes so they could be displayed on a Google Map as another visualisation option for Cohere)

Yesterday I had a go with this on my Cohere development server, so can now add dates to nodes and export in the correct format for Simile. I also created a little script so that I can show my blog postings on a Simile timeline:

This timeline is dynamically generated and shows the last 100 of my blog postings. If you’d like to have a look at the script I’ve used for generating this, then you can download it. Depending on where you post it up (relative to your Wordpress installation), you may need to slightly alter line (no. 14) to point to your wp-config.php file.

The main downside that I’ve found so far with Simile is that is will only allow data to be dynamically loaded via XMLHttpRequest. This means that the data source must reside on the same domain as the page you’re displaying the timeline on (unless you can use a iframe as I have done above) – it would be fantastic if Similie could also accept data via JSON and so avoid the cross-domain problem (I’ll need to check whether this has already been added as new feature request in their issue tracker).

Then completely coincidentally, this morning I saw this post from Stuart about Dipity – an online timeline creator. So I’ve had a very quick play with it this morning – creating the following timeline (also of my blog):

This took about 30 seconds to produce, buy just adding the URL of my blog feed.

These two timelines, although displaying the same data, are obviously quite different with Dipity is far more graphical, though IMHO a bit cluttered and not so easy to read. Simile looks quite grey in comparission, though I’ve not yet explored the API fully and how you can customise the colours/display. Dipity is much quicker to get up and running with, but my feeling is that Simile would offer more flexibility (within bounds), for example I can’t tell yet if Dipity has the capability to show events which span a period of time (i.e. have start and end dates – as Simile allows).

(Looking at Dipity has reminded me that we should create an RSS feed for Cohere nodes).

Any pointers to other timeline visualisations appreciated :-)

iPlayer/Flash on Ubuntu Hardy Heron

Alex May 6th, 2008

A few days ago I mentioned that I was having problems with playing BBC iPlayer programs on my Asus Eee. I was also having problems with _some_ other Flash content – though not all.

Turns out that I’d installed the wrong version of the Flash plugin – actually I’d installed more than one and the conflicts were creating the same problems as if the plugins hadn’t been installed at all. However the instructions in the forum posting fixed me up.

Next »