Committer: atyurin
LJSUP-14684: [Journal, feed] Display GitHub gistsU trunk/htdocs/js/journal.js
Modified: trunk/htdocs/js/journal.js =================================================================== --- trunk/htdocs/js/journal.js 2012-12-21 13:45:43 UTC (rev 23496) +++ trunk/htdocs/js/journal.js 2012-12-21 13:54:23 UTC (rev 23497) @@ -414,4 +414,68 @@ 'use strict'; jQuery(document.body).ljLikes(); -}); \ No newline at end of file +}); + +/** + * Embed gists from GitHub + * + * Parses the page for: + * '<a href="https://gist.github.com/fcd584d3a351c3e9728b"></a>' + */ +(function($) { + 'use strict'; + + var gistBase = 'https://gist.github.com/', + gistCss = { + 'clear' : 'both', + 'display' : 'block', + 'padding-top' : '10px' + }; + + $(function() { + var gist = $('a[href^="' + gistBase + '"]'), + head = $('head'); + + gist.each(function(_, element) { + var link = $(element), + href = link.attr('href'), + match = href && href.match(/gist.github.com\/([a-zA-Z0-9]+)/), + id = match && match[1]; + + if (!id) { + console.error('Bad GitHub id'); + return; + } + + link + .attr('target', '_blank') + .css(gistCss) + .html('Loading the gist...'); + + $.ajax({ + url: gistBase + id + '.json', + dataType: 'jsonp', + timeout: 10000 + }).done(function(result) { + if (!result.div || !result.stylesheet) { + console.error('Data error', result); + } + + head.append( + '<link rel="stylesheet" href="' + result.stylesheet + '">' + ); + + var div = $(result.div).css(gistCss); + + div.find('a').attr('target', '_blank'); + + link.replaceWith(div); + }) + .fail(function(error) { + link.html('Gist loading error'); + }); + }); + + }); + +})(jQuery);