forked from khoinguyen/gist-embed
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgist-embed.js
129 lines (112 loc) · 3.87 KB
/
gist-embed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//author: Blair Vanderhoof
//https://github.com/blairvanderhoof/gist-embed
$(function(){
'use strict';
var gistMarkerId = 'gist-';
window.baseurl = '//gist.github.com';
//find all code elements containing "gist-" the id attribute.
$('code[id*="' + gistMarkerId + '"]').each(function(){
var $elem = $(this),
id,
url,
file,
timestamp = Date.now().toString(),
callbackFunction = "fun_" + timestamp,
evalCallbackFunction = "window.fun_" + timestamp + "=function(response){ myCallback(response, '" + timestamp + "'); }",
data = {};
$elem.addClass(timestamp);
eval(evalCallbackFunction);
id = $elem.attr('id') || '';
file = $elem.attr('data-file');
if(file){
data.file = file;
}
//if the id doesn't begin with 'gist-', then ignore the code block
if (!id || id.indexOf('gist-') !== 0) return false;
//make block level so loading text shows properly
$elem.css('display', 'block');
//get the numeric id from the id attribute of the element holder
id = id.substr(0, gistMarkerId.length) === gistMarkerId ? id.replace(gistMarkerId, '') : null;
//make sure result is a numeric id
if(!isNaN(parseInt(id, 10))){
url = baseurl + '/' + id + '.json';
//loading
$elem.html('Loading gist ' + url + (data.file ? ', file: ' + data.file : '') + '...');
//request the json version of this gist
$.ajax({
type: 'GET',
async: true,
jsonpCallback: callbackFunction,
contentType: "application/json",
url: url,
data: data,
dataType: 'jsonp',
timeout: 10000,
error: function(){
$elem.html('Failed loading gist ' + url + (data.file ? ', file: ' + data.file : '') + '...');
}
});
}else{
$elem.html('Failed loading gist with incorrect id format: ' + $elem.attr('id'));
}
});
});
function getLineNumbers(lineRangeString){
var lineNumbers = [];
var lineNumberSections = lineRangeString.split(',');
for(var k = 0; k < lineNumberSections.length; k++){
var range = lineNumberSections[k].split('-');
if(range.length == 2){
for(var i = parseInt(range[0], 10); i <= range[1]; i++){
lineNumbers.push(i);
}
}
else if(range.length == 1){
lineNumbers.push(parseInt(range[0], 10));
}
}
return lineNumbers;
}
function myCallback(response, timestamp){
var $elem = $('.' + timestamp);
var line = $elem.attr('data-line');
//the html payload is in the div property
if(response && response.div){
//add the stylesheet if it does not exist
if(response.stylesheet && $('link[href="' + response.stylesheet + '"]').length === 0){
var l = document.createElement("link"),
head = document.getElementsByTagName("head")[0];
l.type = "text/css";
l.rel = "stylesheet";
l.href = response.stylesheet;
head.insertBefore(l, head.firstChild);
}
$elem.html("<div id='" + timestamp + "'>" + response.div + "</div>");
if(line){
var lineNumbers = getLineNumbers(line);
$('#' + timestamp).find('.line').each(function(index){
if(($.inArray(index + 1, lineNumbers)) == -1){
$(this).remove();
}
});
var lineNumber = 1;
$('#' + timestamp).find('.line-number').each(function(index){
if(($.inArray(index + 1, lineNumbers)) == -1){
$(this).remove();
}
else{
$(this).html(lineNumber++);
}
});
}
if($elem.attr('data-showFooter') && $elem.attr('data-showFooter') == "false"){
$('#' + timestamp).find('.gist-meta').remove();
}
if($elem.attr('data-showLineNumbers') && $elem.attr('data-showLineNumbers') == "false"){
$('#' + timestamp).find('.line-numbers').remove();
}
$('#' + timestamp).find('.gist-file').css('margin-bottom', '0px');
}else{
$elem.html('Failed loading gist ' + url);
}
}