Making WordPress.org

Changeset 1429


Ignore:
Timestamp:
03/19/2015 10:36:17 PM (9 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Enable expand/collapse functionality for syntax-highlighted code in handbooks.

  • Enqueue function-reference.js on handbook pages as well
  • Wrap handbook syntax-highlighted code in similar markup the JS expects as used on coderef pages
  • Modify JS to handle multiple expandable code blocks on a page
  • Change styling for expand/collapse links since for handbooks only one link will be present in that section

Fixes #959.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/handbooks.php

    r954 r1429  
    4545            add_action( 'wporg_action_links', array( 'WPorg_Handbook_Watchlist', 'display_action_link' ) );
    4646        }
     47
     48        // Modify SyntaxHighlighter Evolved code output to facilitate code collapse/expand.
     49        add_filter( 'syntaxhighlighter_htmlresult', array( __CLASS__, 'syntaxhighlighter_htmlresult' ) );
     50    }
     51
     52    /**
     53     * Is the current (or specified) post_type one of the DevHub handbook post types?
     54     *
     55     * TOOD: The handbook plugin should probably have this.
     56     *
     57     * @param string $post_type Optional. The post_type to check for being a handbook post type. Default '' (the current post type).
     58     * @return bool
     59     */
     60    public static function is_handbook_post_type( $post_type = '' ) {
     61        if ( ! $post_type ) {
     62            $post_type = get_post_type();
     63        }
     64
     65        return in_array( str_replace( '-handbook', '', $post_type ), self::$post_types );
     66    }
     67
     68    /**
     69     * If a syntax highlighted code block exceeds a given number of lines, wrap the
     70     * markup with other markup to trigger the code expansion/collapse JS handling
     71     * already implemented for the code reference.
     72     *
     73     * @param string  $text The pending result of the syntax highlighting.
     74     * @return string
     75     */
     76    public static function syntaxhighlighter_htmlresult( $text ) {
     77        $new_text      = '';
     78        // Collapse is handled for >10 lines. But just go ahead and show the full
     79        // code if that is just barely being exceeded (no one wants to expand to
     80        // see one or two more lines).
     81        $lines_to_show = 12;
     82        $do_collapse   = ( substr_count( $text, "\n" ) - 1 ) > $lines_to_show;
     83
     84        if ( $do_collapse )  {
     85            $new_text .= '<section class="source-content">';
     86            $new_text .= '<div class="source-code-container">';
     87        }
     88
     89        $new_text .= $text;
     90
     91        if ( $do_collapse ) {
     92            $new_text .= '</div>';
     93            $new_text .= '<p class="source-code-links"><span>';
     94            $new_text .= '<a href="#" class="show-complete-source">' . __( 'Expand full source code', 'wporg' ) . '</a>';
     95            $new_text .= '<a href="#" class="less-complete-source">' . __( 'Collapse full source code', 'wporg' ) . '</a>';
     96            $new_text .= '</span></p>';
     97            $new_text .= '</section>';
     98        }
     99
     100        return $new_text;
    47101    }
    48102
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/user-content.php

    r1198 r1429  
    6666    public static function scripts_and_styles() {
    6767        if ( is_singular() ) {
    68             if ( '0' != get_comments_number() || \DevHub\post_type_has_source_code() ) {
     68            if ( '0' != get_comments_number() || \DevHub\post_type_has_source_code() || Devhub_Handbooks::is_handbook_post_type() ) {
    6969                wp_enqueue_script( 'wporg-developer-function-reference', get_template_directory_uri() . '/js/function-reference.js', array( 'jquery', 'syntaxhighlighter-core', 'syntaxhighlighter-brush-php' ), '20150126', true );
    7070                wp_enqueue_style( 'syntaxhighlighter-core' );
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/function-reference.js

    r1196 r1429  
    77    'use strict';
    88
    9     var $sourceContent, $sourceCodeContainer, $sourceCodeTable, $showCompleteSource, $lessCompleteSource, sourceCollapsedHeight;
     9    var $sourceCollapsedHeight;
    1010
    1111    var $usesList, $usedByList, $showMoreUses, $hideMoreUses, $showMoreUsedBy, $hideMoreUsedBy;
     
    2424        }
    2525
    26         // We only expect one source-content per document
    27         $sourceContent = $( '.source-content' );
    28         $sourceCodeContainer = $( '.source-code-container' );
    29 
    3026        SyntaxHighlighter.highlight();
    31 
    32         $sourceCodeTable = $sourceContent.find( 'table' );
    3327
    3428        // 1em (margin) + 10 * 17px + 10. Lines are 1.1em which rounds to 17px: calc( 1em + 17px * 10 + 10 ).
    3529        // Extra 10px added to partially show next line so it's clear there is more.
    36         sourceCollapsedHeight = 196;
     30        $sourceCollapsedHeight = 196;
    3731
    38         if ( ( sourceCollapsedHeight - 12 ) < $sourceCodeTable.height() ) {
     32        $( '.source-content' ).find( 'table' ).each( function( t ) {
     33            if ( ( $sourceCollapsedHeight - 12 ) < $( this ).height() ) {
    3934
    40             // Do this with javascript so javascript-less can enjoy the total sourcecode
    41             $( '.source-code-container' ).css( { height: sourceCollapsedHeight + 'px' } );
     35                var sourceContent = $( this ).closest( '.source-content' );
    4236
    43             $showCompleteSource = $( '.show-complete-source' );
    44             $lessCompleteSource = $( '.less-complete-source' );
     37                // Do this with javascript so javascript-less can enjoy the total sourcecode
     38                sourceContent.find( '.source-code-container' ).css( { height: $sourceCollapsedHeight + 'px' } );
    4539
    46             $( '.source-code-links span:first' ).show();
    47             $showCompleteSource.show();
    48             $showCompleteSource.on( 'click', toggleCompleteSource );
    49             $lessCompleteSource.on( 'click', toggleCompleteSource );
    50         }
     40                sourceContent.find( '.source-code-links').find('span:first' ).show();
     41                sourceContent.find( '.show-complete-source' ).show();
     42                sourceContent.find( '.show-complete-source' ).on( 'click', toggleCompleteSource );
     43                sourceContent.find( '.less-complete-source' ).on( 'click', toggleCompleteSource );
     44            }
     45        } );
    5146    }
    5247
     
    5449        e.preventDefault();
    5550
    56         if ( $showCompleteSource.is(':visible') ) {
    57             var heightGoal = $sourceCodeTable.height() + 45; // takes into consideration potential x-scrollbar
     51        var sourceContent = $( this ).closest( '.source-content' );
     52
     53        if ( $( this ).parent().find( '.show-complete-source' ).is( ':visible' ) ) {
     54            var heightGoal = sourceContent.find( 'table' ).height() + 45; // takes into consideration potential x-scrollbar
    5855        } else {
    59             var heightGoal = sourceCollapsedHeight;
     56            var heightGoal = $sourceCollapsedHeight;
    6057        }
    6158
    62         $sourceCodeContainer.animate( { height: heightGoal + 'px' } );
     59        sourceContent.find( '.source-code-container:first' ).animate( { height: heightGoal + 'px' } );
    6360
    64         $showCompleteSource.toggle();
    65         $lessCompleteSource.toggle();
    66 
     61        $( this ).parent().find( 'a' ).toggle();
    6762    }
    6863
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/scss/main.scss

    r1380 r1429  
    10631063    }
    10641064
     1065    .source-code-links span {
     1066        border-left: 1px solid #999;
     1067        margin-left: 1em;
     1068        padding-left: 1em;
     1069    }
     1070
    10651071    .source-code-links span:first-child {
    10661072        display: none;
    1067         border-right: 1px solid #999;
    1068         margin-right: 1em;
    1069         padding-right: 1em;
     1073        border-left: 0;
     1074        margin-left: 1em;
     1075        padding-left: 0;
    10701076    }
    10711077
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/stylesheets/main.css

    r1380 r1429  
    12421242  margin-top: 1em;
    12431243}
     1244.devhub-wrap .source-code-links span {
     1245  border-left: 1px solid #999;
     1246  margin-left: 1em;
     1247  padding-left: 1em;
     1248}
    12441249.devhub-wrap .source-code-links span:first-child {
    12451250  display: none;
    1246   border-right: 1px solid #999;
    1247   margin-right: 1em;
    1248   padding-right: 1em;
     1251  border-left: 0;
     1252  margin-left: 1em;
     1253  padding-left: 0;
    12491254}
    12501255.devhub-wrap .source-code-container {
Note: See TracChangeset for help on using the changeset viewer.