views:

300

answers:

3

SVN keyword substition gives is not pretty. E.g.,

Last updated: $Date$ by $Author$

yields

Last updated: $Date: 2008-09-22 14:38:43 -0400 (Mon, 22 Sep 2008) $ by $Author: cconway $"

Does anybody have a Javascript snippet that prettifies things and outputs some HTML? The result should be more like:

Last update: 22 Sep 2008 by cconway

P.S. Is there a way to replace "cconway" with a display name?

[EDIT] Could the people who are down-voting this question please post a comment or answer explaining what's so bad about it? If it's blindingly obvious, answer it and I'll vote you up.

A: 

Some JavaScript libraries provide templating functionality.

Prototype - http://www.prototypejs.org/api/template
Ext JS - http://extjs.com/deploy/dev/docs/?class=Ext.DomHelper

I'm sure you could find a plugin for JQuery.

Chris MacDonald
+2  A: 

Errr.. This feels a bit like me doing your job for you :), but here goes:

function formatSvnString(string){
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

    var re = /\$Date: (\d{4})-(\d\d)-(\d\d).*?\$Author: (\S+) \$/
    return string.replace(re, function(match, year, month, day, author){ 
        var date = new Date([year, month, day].join('/'))
        return date.getDate()
            + ' ' + months[date.getMonth()]
            + ' ' + date.getFullYear()
            + ' by ' + author
    })
}

Usage:

formatSvnString("$Date: 2008-09-22 14:38:43 -0400 (Mon, 22 Sep 2008) $ by $Author: cconway $")
// returns: 22 Sep 2008 by cconway

I'll leave it up to you to work out how to find these SVN string and apply the above code automatically :)

To do the user's display name, you'll either have to convince SVN to insert it (I don't think it can do that, but I might be wrong), or somehow provide a means for JS to either fetch it, or to have access to a table full of usernames and associated display name (might be a bit too much like a security risk though. Hey kids, see if you can break into my server using one of these usernames!)

Dan
A: 

Here's a little jQuery code to do the job:

 $(document).ready(function() {
     var date = "$Date: 2008-09-23 00:10:56 -0400 (Tue, 23 Sep 2008) $"
         .replace(/\$Date:.*\((.*)\)\s*\$/,"$1");
     $(".timestamp").text( "Last updated: " + date );
 });

where there is a <div class="timestamp" /> in the HTML wherever you want the timestamp text to appear.

Chris Conway