Skip to content

Instantly share code, notes, and snippets.

Created June 17, 2013 10:47
Show Gist options
  • Save anonymous/5796087 to your computer and use it in GitHub Desktop.
Save anonymous/5796087 to your computer and use it in GitHub Desktop.
;(function ($) {
var githubCacheFilePath = [];
var githubCacheSha = [];
var fnSuccess =
function (data, startLineNum, endLineNum, callback) {
if (data.data.content && data.data.encoding === "base64") {
var contentArray =
window
.atob(data.data.content.replace(/\n/g, ""))
.split("\n");
endLineNum = endLineNum || contentArray.length;
callback(contentArray.slice(startLineNum - 1, endLineNum).join("\n"));
}
};
$.getGithubFileByFilePath =
function(user, repo, filePath, callback, startLineNum, endLineNum) {
if(githubCacheFilePath[filePath]){
$.getGithubFile(user, repo, githubCacheFilePath[filePath], callback, startLineNum, endLineNum)
}else{
$.ajax({
type: "GET"
,url: "https://api.github.com/repos/" + user + "/" + repo + "/contents/"+filePath
,dataType: "jsonp"
,success: function(data){
githubCacheFilePath[filePath] = data.data.sha;
$.getGithubFile(user, repo, githubCacheFilePath[filePath], callback, startLineNum, endLineNum)
}
});
}
};
$.getGithubFile =
function(user, repo, sha, callback, startLineNum, endLineNum) {
if(githubCacheSha[sha]){
fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
}else{
$.ajax({
type: "GET"
,url: "https://api.github.com/repos/" + user + "/" + repo + "/git/blobs/" + sha
,dataType: "jsonp"
,success: function(data) {
githubCacheSha[sha] = data
fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
}
});
}
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment