Browse files

Merge branch 'master' of github.com:kashif-umair/gist-embed into feat…

…ure/my_own_blog
  • Loading branch information...
2 parents 3608dff + 85ee074 commit eb0c51862444e6b8cb73a113b242f88312f87b66 @kashif-umair committed Feb 4, 2013
Showing with 30 additions and 28 deletions.
  1. +30 −28 gist-embed.js
View
58 gist-embed.js
@@ -12,13 +12,13 @@ $(function(){
line,
data = {};
- id = $elem.attr('id') || '';
- file = $elem.attr('data-file');
- line = $elem.attr('data-line');
- splittedFileName = file.split('.').join('-');
+ id = $elem.attr('id') || '';
+ file = $elem.attr('data-file');
+ line = $elem.attr('data-line');
if(file){
data.file = file;
+ splittedFileName = file.split('.').join('-');
}
//if the id doesn't begin with 'gist-', then ignore the code block
@@ -54,20 +54,35 @@ $(function(){
l.href = response.stylesheet;
head.insertBefore(l, head.firstChild);
}
+
+ var random = Math.floor(Math.random() * 100000);
+ $elem.html("<div id='" + random + "'>" + response.div + "</div>");
+
if(line){
var lineNumbers = getLineNumbers(line);
- var lineCodes = new Array(lineNumbers.length);
- for(var i = 0; i < lineNumbers.length; i++){
- if($(response.div).find('#file-' + splittedFileName + '-LC' + lineNumbers[i])[0]){
- lineCodes[i] = $(response.div).find('#file-' + splittedFileName + '-LC' + lineNumbers[i])[0].innerHTML;
+ $('#' + random).find('.line').each(function(index){
+ if(($.inArray(index + 1, lineNumbers)) == -1){
+ $(this).remove();
+ }
+ });
+
+ lineNumber = 1;
+ $('#' + random).find('.line-number').each(function(index){
+ if(($.inArray(index + 1, lineNumbers)) == -1){
+ $(this).remove();
+ }
+ else{
+ $(this).html(lineNumber++);
}
- }
- html = basicStructureForMultipleLines(id, lineCodes, splittedFileName);
- $elem.html(html);
+ });
}
- else{
- $elem.html(response.div);
+ if($elem.attr('data-showFooter') && $elem.attr('data-showFooter') == "false"){
+ $('#' + random).find('.gist-meta').remove();
+ }
+
+ if($elem.attr('data-showLineNumbers') && $elem.attr('data-showLineNumbers') == "false"){
+ $('#' + random).find('.line-numbers').remove();
}
$('#' + random).find('.gist-file').css('margin-bottom', '0px');
}else{
@@ -90,26 +105,13 @@ function getLineNumbers(lineRangeString){
for(var k = 0; k < lineNumberSections.length; k++){
var range = lineNumberSections[k].split('-');
if(range.length == 2){
- for(var i = range[0]; i <= range[1]; i++){
+ for(var i = parseInt(range[0]); i <= range[1]; i++){
lineNumbers.push(i);
}
}
else if(range.length == 1){
- lineNumbers.push(range[0]);
+ lineNumbers.push(parseInt(range[0]));
}
}
return lineNumbers;
}
-
-function basicStructureForMultipleLines(gistId, lineCodes, splittedFileName){
- html = '<div id="gist' + gistId + '" class="gist"><div class="gist-file">' +
- '<div class="gist-data gist-syntax"><div class="file-data">' +
- '<table cellpadding="0" cellspacing="0" class="lines highlight">' +
- '<tbody><tr><td class="line-data"><pre class="line-pre">';
- for(var i = 0; i < lineCodes.length; i++){
- html += '<div class="line" id="file-' + splittedFileName + '-LC' + (i + 1) + '">' +
- lineCodes[i] + '</div>';
- }
- html += '</pre></td></tr></tbody></table></div></div></div></div>';
- return html;
-}

0 comments on commit eb0c518

Please sign in to comment.