forked from nothingmuch/jquery-gist-upgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist_upgrade.js
70 lines (59 loc) · 2.77 KB
/
gist_upgrade.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
/* This script finds pre.fake-gist elements with an ID containing the gist ID,
* and upgrades them in place into the syntax highlighted version */
/* Copyright 2009 Yuval Kogman, MIT license */
jQuery(function ($) {
$(document).ready(function () {
$('pre.fake-gist[id]').each(function () {
var id = $(this).attr('id');
var gistID = $(this).attr('id').match(/\d+/)[0];
$(this).removeAttr('id').wrap(
/* first we wrap with the various classes */
'<div class="gist">' +
'<div class="gist-file">' +
'<div class="gist-data gist-syntax">' +
'<div class="gist-highlight">' +
'<div class="line nn"></div>' +
'</div>' +
'</div>' +
/* and add the blurb at the bottom (no raw link though) */
'<div class="gist-meta">' +
'<a href="http://gist.github.com/'+ gistID +'">This Gist</a>' +
' brought to you by <a href="http://github.com">GitHub</a>.' +
'</div>' +
'</div>' +
'</div>'
).parents('div.gist:first').attr('id', id);
});
});
$(window).load(function () {
$('div.gist[id]').each(function () {
var $this = $(this);
var id = $this.attr('id');
var gistID = $this.attr('id').match(/\d+/)[0];
/* check whether filename specified */
function fileCheck() {
if (file = id.match(/fake-gist-\d+-(.+)/)) {
return "file=" + file[1] + "&"
} else { return "" }
}
/* asynchronously fetch the gist data */
$.getJSON("http://gist.github.com/"+ gistID +".json?" + fileCheck() + "callback=?", function (gist) {
/* Figure out if we need to add the stylesheet
*
* for some reason
* $("link[rel=stylesheet][href='" + gist.stylehseet + "']").length == 0
* doesn't work */
var add_stylesheet = true;
$("link[rel=stylesheet]").each(function (i,e) {
if ( $(e).attr('href') == gist.stylesheet )
add_stylesheet = false;
});
/* if the stylesheet is not yet in the document, add it */
if ( add_stylesheet )
$("head").append('<link rel="stylesheet" href="' + gist.stylesheet + '" />');
/* find the fake gist and replace it with the marked up one */
$this.replaceWith(gist.div);
});
});
});
});