Created
February 25, 2013 11:37
-
-
Save purcell/5029269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For races on fellrunner.org.uk, produce a string pasteable into my org-mode race list | |
(function() { | |
function parseRace() { | |
var $ = window.jQuery; | |
var race = {}; | |
race.url = window.location.href; | |
race.name = $(".title_races").first().text().split(" – ")[1].trim(); | |
var parseDateTime = function(s) { | |
// Fri 1st Jan 2010 at 11:00 | |
return new Date(Date.parse(s.replace(/(st|nd|rd|th| at)/g, ''))); | |
}; | |
var findURL = function(e) { | |
var a = e.find("a[href]"); | |
return a.attr("href"); | |
}; | |
$(".race_info_list li").each(function(i, e) { | |
var elems = $(e).children(); | |
var strong = elems.first(); | |
if (/:$/.test(strong.text())) { | |
var key = strong.text().replace(/:$/, ''); | |
var info = $(e).clone(); | |
info.children().first().remove(); | |
var value = info.text().trim(); | |
if (key == "Website") | |
value = findURL(info); | |
else if (key == "Date & time") | |
value = parseDateTime(value); | |
race[key] = value; | |
} | |
}); | |
return race; | |
} | |
Date.prototype.toOrgString = function() { | |
function pad(number) { | |
var r = String(number); | |
if ( r.length === 1 ) { | |
r = '0' + r; | |
} | |
return r; | |
} | |
return this.getFullYear() | |
+ '-' + pad( this.getMonth() + 1 ) | |
+ '-' + pad( this.getDate() ) | |
+ ' ' + pad( ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][this.getDay()] ) | |
+ ' ' + pad( this.getHours() ) | |
+ ':' + pad( this.getMinutes() ); | |
}; | |
function raceToOrg(race) { | |
var result = "<" + race["Date & time"].toOrgString() + ">" + | |
" [[" + race.url + "][" + race.name + "]], " + race['Category'] + ", " | |
+ race['Distance'].split(" / ")[1] + "/" + race['Climb'].split(" / ")[1]; | |
var onDayFee = race['Entry on day fee']; | |
if (onDayFee) | |
result += ", " + onDayFee + " on day"; | |
return result; | |
} | |
alert(raceToOrg(parseRace())); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment