Created
September 19, 2012 09:37
-
-
Save saurini/3748715 to your computer and use it in GitHub Desktop.
Github for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Inserts an embedded gist into the content usage [gist id="12345"] | |
function saurini_gist_func( $atts ){ | |
extract(shortcode_atts(array( | |
'id' => '1337'), $atts)); | |
// Only allow gist ids that are letters or numbers | |
if ( preg_match( '/[^0-9^a-z^A-Z]/', $id ) ) | |
return; | |
// Dollar signs are escaped in the heredoc so I don't give myself a concatenation headache. | |
return <<<HTML | |
<style type="text/css"> | |
html body div .gist .gist-file .gist-data pre { | |
padding: 0 !important; | |
} | |
.gist .line { | |
height: 20px; | |
line-height: 20px; | |
} | |
.gist .line:nth-child( 2n+1 ){ | |
background-color: #f0f0f0; | |
} | |
.gist .line-number{ | |
border-right: 3px solid #AEC7BA; | |
color: #3AA1C9; | |
display: inline-block; | |
font-weight: bold; | |
height: 20px; | |
margin: 0 5px; | |
} | |
</style> | |
<script type="text/javascript" src="http://gist.github.com/{$id}.js"></script> | |
<script type="text/javascript"> | |
( function( \$ ){ | |
// store this in global scope so it can be modified and then accessed with the each is done | |
var line_number; | |
var longest_line = 0; | |
\$( '.gist .line' ).each( function(){ | |
var line_id = \$( this ).attr( 'id' ); | |
var line_length = 0; | |
line_number = line_id.match( /\d+/ )[0]; | |
\$( this ).prepend( '<span class="line-number">' + line_number + '.</span>' ); | |
\$( this ).children().each( function(){ | |
line_length += \$( this ).text().length; | |
}); | |
if ( line_length > longest_line ) | |
longest_line = line_length; | |
}); | |
\$( '.gist .line' ).css( 'width', ( longest_line * 9 ) + 'px' ); | |
var max_line_number_length = ( line_number.toString().length ) * 9 + 9+ 'px'; | |
\$( '.gist .line-number' ).css( 'width', max_line_number_length ); | |
\$( '.gist-meta a' ).each( function(){ | |
\$( this ).attr( 'target', '_blank' ); | |
var href = \$( this ).attr( 'href' ); | |
if ( href.match( /\/raw/ ) ){ | |
\$( this ).on( 'click', function(){ | |
var raw_window = window.open( href , "Raw Gist", "toolbar=0,status=0,width=500,height=600,scrollbars=1" ); | |
raw_window.moveTo( 500, 50 ); | |
return false; | |
}); | |
} | |
}); | |
} )( jQuery ); | |
</script> | |
HTML; | |
} | |
add_shortcode( 'gist', 'saurini_gist_func' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment