diff --git a/gulp/vendor.coffee b/gulp/vendor.coffee index 669f735d..cae83b5b 100644 --- a/gulp/vendor.coffee +++ b/gulp/vendor.coffee @@ -12,6 +12,7 @@ g.task 'vendor-scripts', => g.js [ 'vendor/bower/jquery/dist/jquery.js', 'vendor/bower/bootstrap-sass/assets/javascripts/bootstrap/collapse.js', - 'scripts/vendor/github-cards.js' + 'scripts/vendor/gist-embed.js', + 'scripts/vendor/github-cards.js', 'scripts/vendor/tweets.js' ], 'vendor.js' diff --git a/scripts/vendor/gist-embed.js b/scripts/vendor/gist-embed.js new file mode 100644 index 00000000..d4c54a65 --- /dev/null +++ b/scripts/vendor/gist-embed.js @@ -0,0 +1,199 @@ +/* + * author: Blair Vanderhoof + * https://github.com/blairvanderhoof/gist-embed + * version 2.4 + */ +(function($) { + 'use strict'; + + function getLineNumbers(lineRangeString) { + var lineNumbers = [], range, lineNumberSections; + + if (typeof lineRangeString === 'number') { + lineNumbers.push(lineRangeString); + } else { + lineNumberSections = lineRangeString.split(','); + + for (var i = 0; i < lineNumberSections.length; i++) { + range = lineNumberSections[i].split('-'); + if (range.length === 2) { + for (var j = parseInt(range[0], 10); j <= range[1]; j++) { + lineNumbers.push(j); + } + } else if (range.length === 1) { + lineNumbers.push(parseInt(range[0], 10)); + } + } + } + return lineNumbers; + } + + $.fn.gist = function() { + return this.each(function() { + var $elem = $(this), + id, + url, + file, + lines, + loading, + highlightLines, + hideFooterOption, + hideLineNumbersOption, + showLoading, + showSpinner, + data = {}; + + // make block level so loading text shows properly + $elem.css('display', 'block'); + + id = $elem.data('gist-id') || ''; + file = $elem.data('gist-file'); + hideFooterOption = $elem.data('gist-hide-footer') === true; + hideLineNumbersOption = $elem.data('gist-hide-line-numbers') === true; + lines = $elem.data('gist-line'); + highlightLines = $elem.data('gist-highlight-line'); + showSpinner = $elem.data('gist-show-spinner') === true; + if (showSpinner) { + showLoading = false; + } else { + showLoading = $elem.data('gist-show-loading') !== undefined ? + $elem.data('gist-show-loading') : true; + } + + if (file) { + data.file = file; + } + + // if the id doesn't exist, then ignore the code block + if (!id) { + return false; + } + + url = 'https://gist.github.com/' + id + '.json'; + loading = 'Loading gist ' + url + (data.file ? ', file: ' + data.file : '') + '...'; + + // loading + if (showLoading) { + $elem.html(loading); + } + + // loading spinner + if (showSpinner) { + $elem.html('' + loading + ''); + } + + // request the json version of this gist + $.ajax({ + url: url, + data: data, + dataType: 'jsonp', + timeout: 10000, + success: function(response) { + var linkTag, + head, + lineNumbers, + highlightLineNumbers, + $responseDiv; + + // the html payload is in the div property + if (response && response.div) { + // github returns /assets/embed-id.css now, but let's be sure about that + if (response.stylesheet) { + // github passes down html instead of a url for the stylehsheet now + // parse off the href + if (response.stylesheet.indexOf('