Skip to content

Instantly share code, notes, and snippets.

@saurini
Created September 19, 2012 09:37
Show Gist options
  • Save saurini/3748715 to your computer and use it in GitHub Desktop.
Save saurini/3748715 to your computer and use it in GitHub Desktop.

Revisions

  1. saurini revised this gist Sep 23, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -47,8 +47,6 @@ function saurini_gist_func( $atts ){
    \$( this ).prepend( '<span class="line-number">' + line_number + '.</span>' );
    \$( this ).children().each( function(){
    line_length += \$( this ).text().length;
    });
  2. saurini revised this gist Sep 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ function saurini_gist_func( $atts ){
    longest_line = line_length;
    });
    \$( '.gist .line' ).css( 'width', ( longest_line * 8 ) + 'px' );
    \$( '.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 );
  3. saurini revised this gist Sep 20, 2012. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,26 @@ function saurini_gist_func( $atts ){

    // 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( \$ ){
  4. saurini revised this gist Sep 20, 2012. 1 changed file with 27 additions and 14 deletions.
    41 changes: 27 additions & 14 deletions shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -14,41 +14,54 @@ function saurini_gist_func( $atts ){
    <script type="text/javascript">
    ( function( \$ ){
    // Store line_number in global scope so it can be modified and then accessed with the each is done
    // 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;
    // Iterate over each line in the embedded gist
    \$( '.gist .line' ).each( function(){
    // We need the id of each line to grab the number
    var line_id = \$( this ).attr( 'id' )
    // Said number grabbing
    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>' );
    // We'll use line_length to set the width of the pre element later
    var line_length = 0;
    // Now we count the text length inside each span
    \$( this ).children().each( function(){
    line_length = line_length + \$( this ).text().length;
    line_length += \$( this ).text().length;
    });
    if ( line_length > longest_line )
    longest_line = line_length;
    });
    // Set the width of the pre according to how many characters on the longest line
    \$( '.gist .line' ).css( 'width', ( longest_line * 8 ) + 'px' );
    // Calculate the width of the pre element ( so that the
    var max_line_number_length = ( line_number.toString().length ) * 9 + 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>
  5. saurini revised this gist Sep 19, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function saurini_gist_func( $atts ){
    // We'll use line_length to set the width of the pre element later
    var line_length = 0;
    // Now we grab count the text length inside each span
    // Now we count the text length inside each span
    \$( this ).children().each( function(){
    line_length = line_length + \$( this ).text().length;
    });
    @@ -45,7 +45,8 @@ function saurini_gist_func( $atts ){
    // Set the width of the pre according to how many characters on the longest line
    \$( '.gist .line' ).css( 'width', ( longest_line * 8 ) + 'px' );
    var max_line_number_length = ( line_number.toString().length ) * 9 + 9+ 'px';
    // Calculate the width of the pre element ( so that the
    var max_line_number_length = ( line_number.toString().length ) * 9 + 9 + 'px';
    \$( '.gist .line-number' ).css( 'width', max_line_number_length );
    } )( jQuery );
  6. saurini revised this gist Sep 19, 2012. 1 changed file with 14 additions and 7 deletions.
    21 changes: 14 additions & 7 deletions shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -4,30 +4,35 @@
    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
    // Store line_number in global scope so it can be modified and then accessed with the each is done
    var line_number;
    var longest_line = 0;
    // Iterate over each line in the embedded gist
    \$( '.gist .line' ).each( function(){
    // We need the id of each line to grab the number
    var line_id = \$( this ).attr( 'id' )
    var line_id = \$( this ).attr( 'id' );
    var line_length = 0;
    // Said number grabbing
    line_number = line_id.match( /\d+/ )[0];
    \$( this ).prepend( '<span class="line-number">' + line_number + '.</span>' );
    // We'll use line_length to set the width of the pre element later
    var line_length = 0;
    // Now we grab count the text length inside each span
    \$( this ).children().each( function(){
    line_length = line_length + \$( this ).text().length;
    });
    @@ -36,6 +41,8 @@ function saurini_gist_func( $atts ){
    longest_line = line_length;
    });
    // Set the width of the pre according to how many characters on the longest line
    \$( '.gist .line' ).css( 'width', ( longest_line * 8 ) + 'px' );
    var max_line_number_length = ( line_number.toString().length ) * 9 + 9+ 'px';
    @@ -51,4 +58,4 @@ function saurini_gist_func( $atts ){

    add_shortcode( 'gist', 'saurini_gist_func' );

    ?>
    ?>
  7. saurini created this gist Sep 19, 2012.
    54 changes: 54 additions & 0 deletions shortcodes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    <?php

    // Inserts an embedded gist into the content usage [gist id="12345"]
    function saurini_gist_func( $atts ){
    extract(shortcode_atts(array(
    'id' => '1337'), $atts));

    if ( preg_match( '/[^0-9^a-z^A-Z]/', $id ) )
    return;

    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 = 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 );
    } )( jQuery );
    </script>
    HTML;


    }

    add_shortcode( 'gist', 'saurini_gist_func' );

    ?>