Making WordPress.org


Ignore:
Timestamp:
07/13/2020 10:40:31 PM (6 years ago)
Author:
coffee2code
Message:

Developer theme: Convert lists (defined using * or - for items) appearing in descriptions into actual HTML lists.

Props ocean90, coffee2code.
See #5304.

File:
1 edited

Legend:

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

    r10067 r10068  
    4040        add_filter( 'devhub-format-description', array( __CLASS__, 'fix_param_hash_formatting' ), 9 );
    4141        add_filter( 'devhub-format-description', array( __CLASS__, 'fix_param_description_html_as_code' ) );
     42        add_filter( 'devhub-format-description', array( __CLASS__, 'convert_lists_to_markup' ) );
    4243
    4344        add_filter( 'devhub-format-hash-param-description', array( __CLASS__, 'autolink_references' ) );
     
    316317        foreach ( $allowable_tags as $tag ) {
    317318            $text = str_replace( array( "&lt;{$tag}&gt;", "&lt;/{$tag}&gt;" ), array( "<{$tag}>", "</{$tag}>" ), $text );
    318         }
    319 
    320         // Convert asterisks to a list.
    321         // Inline lists in param descriptions aren't handled by parser.
    322         // Example: https://developer.wordpress.org/reference/functions/add_menu_page/
    323         if ( false !== strpos( $text, ' * ' ) )  {
    324             // Display as simple plaintext list.
    325             $text = str_replace( ' * ', '<br /> * ', $text );
    326319        }
    327320
     
    485478
    486479    /**
     480     * Converts simple Markdown-like lists into list markup.
     481     *
     482     * Necessary in cases like hash param descriptions which don't see Markdown
     483     * list processing during parsing.
     484     *
     485     * Recognizes lists where list items are denoted with an asterisk or dash.
     486     *
     487     * Does not handle nesting of lists.
     488     *
     489     * @param string $text The text to process for lists.
     490     * @return string
     491     */
     492    public static function convert_lists_to_markup( $text ) {
     493        $inline_list = false;
     494        $li = '<br /> * ';
     495
     496        // Convert asterisks to a list.
     497        // Example: https://developer.wordpress.org/reference/functions/add_menu_page/
     498        if ( false !== strpos( $text, ' * ' ) )  {
     499            // Display as simple plaintext list.
     500            $text = str_replace( ' * ', "\n" . $li, $text );
     501            $inline_list = true;
     502        }
     503
     504        // Convert dashes to a list.
     505        // Example: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
     506        // Example: https://developer.wordpress.org/reference/hooks/password_change_email/
     507        if ( false !== strpos( $text, ' - ' ) )  {
     508            // Display as simple plaintext list.
     509            $text = str_replace( ' - ', "\n" . $li, $text );
     510            $inline_list = true;
     511        }
     512
     513        // If list detected.
     514        if ( $inline_list ) {
     515            // Replace first item, ensuring the opening 'ul' tag is prepended.
     516            $text = preg_replace( '~^' . preg_quote( $li ) . '(.+)$~mU', "<ul><li>\$1</li>\n", $text, 1 );
     517            // Wrap subsequent list items in 'li' tags.
     518            $text = preg_replace( '~^' . preg_quote( $li ) . '(.+)$~mU', "<li>\$1</li>\n", $text );
     519            $text = trim( $text );
     520
     521            // Close the list if it hasn't been closed before start of next hash parameter.
     522            //$text = preg_replace( '~(</li>)(\s+</li>)~smU', '$1</ul>$2', $text );
     523            $text = preg_replace( '~(</li>)(\s*</li>)~smU', '$1</ul>$2', $text );
     524
     525            // Closethe list if it hasn't been closed and it's the end of the description.
     526            if ( '</li>' === substr( trim( $text ), -5 ) ) {
     527                $text .= '</ul>';
     528            }
     529        }
     530
     531        return $text;
     532    }
     533
     534    /**
    487535     * Formats the output of params defined using hash notation.
    488536     *
Note: See TracChangeset for help on using the changeset viewer.