Skip to content

Instantly share code, notes, and snippets.

@saurini
Created September 19, 2012 09:37
Show Gist options
  • Select an option

  • Save saurini/3748715 to your computer and use it in GitHub Desktop.

Select an option

Save saurini/3748715 to your computer and use it in GitHub Desktop.
Github for WordPress
<?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
<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 * 8 ) + '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