Making WordPress.org

Ticket #1810: 1810.2.diff

File 1810.2.diff, 3.7 KB (added by obenland, 9 years ago)
  • readme/class-parser.php

     
    6767        public $screenshots = array();
    6868
    6969        /**
     70         * @var array
     71         */
     72        public $faq = array();
     73
     74        /**
    7075         * These are the readme sections that we expect.
    7176         *
    7277         * @var array
     
    286291
    287292                // Parse out the Upgrade Notice section into it's own data.
    288293                if ( isset( $this->sections['upgrade_notice'] ) ) {
    289                         $lines   = explode( "\n", $this->sections['upgrade_notice'] );
    290                         $version = null;
    291                         $current = '';
    292                         while ( ( $line = array_shift( $lines ) ) !== null ) {
    293                                 $trimmed = trim( $line );
    294                                 if ( empty( $trimmed ) ) {
    295                                         continue;
    296                                 }
    297 
    298                                 if ( '=' === $trimmed[0] || '#' === $trimmed[0] ) {
    299                                         if ( ! empty( $current ) ) {
    300                                                 $this->upgrade_notice[ $version ] = $this->sanitize_text( trim( $current ) );
    301                                         }
    302 
    303                                         $current = '';
    304                                         $version = trim( $line, "#= \t" );
    305                                         continue;
    306                                 }
    307 
    308                                 $current .= $line . "\n";
    309                         }
    310                         if ( ! empty( $version ) && ! empty( $current ) ) {
    311                                 $this->upgrade_notice[ $version ] = $this->sanitize_text( trim( $current ) );
    312                         }
     294                        $this->upgrade_notice = $this->parse_section( $this->sections['upgrade_notice'] );
    313295                        unset( $this->sections['upgrade_notice'] );
    314296                }
    315297
     298                // Display FAQs as a definition list.
     299                if ( isset( $this->sections['faq'] ) ) {
     300                        $this->faq = $this->parse_section( $this->sections['faq'] );
     301                        $this->sections['faq'] = '';
     302                }
     303
    316304                // Markdownify!
    317305                $this->sections       = array_map( array( $this, 'parse_markdown' ), $this->sections );
    318306                $this->upgrade_notice = array_map( array( $this, 'parse_markdown' ), $this->upgrade_notice );
     307                $this->faq            = array_map( array( $this, 'parse_markdown' ), $this->faq );
    319308
    320309                // Sanitize and trim the short_description to match requirements.
    321310                $this->short_description = $this->sanitize_text( $this->short_description );
     
    334323                        unset( $this->sections['screenshots'] );
    335324                }
    336325
     326                if ( ! empty( $this->faq ) ) {
     327                        $plain_text = false;
     328
     329                        foreach( $this->faq as $question => $answer ) {
     330                                if ( empty( $question ) ) {
     331                                        $this->sections['faq'] = $answer;
     332                                        $plain_text            = true;
     333                                        break;
     334                                }
     335
     336                                $this->sections['faq'] .= "<dt>{$question}</dt>\n<dd>{$answer}</dd>\n";
     337                        }
     338
     339                        if ( ! $plain_text ) {
     340                                $this->sections['faq'] = "<dl>{$this->sections['faq']}</dl>\n";
     341                        }
     342
     343                        unset( $this->faq );
     344                }
     345
    337346                // Filter the HTML.
    338347                $this->sections = array_map( array( $this, 'filter_text' ), $this->sections );
    339348
     
    413422                        'strong'     => true,
    414423                        'ul'         => true,
    415424                        'ol'         => true,
     425                        'dl'         => true,
     426                        'dt'         => true,
     427                        'dd'         => true,
    416428                        'li'         => true,
    417429                        'h3'         => true,
    418430                        'h4'         => true,
     
    494506                return $stable_tag;
    495507        }
    496508
     509        protected function parse_section( $lines ) {
     510                $key = $value = '';
     511                $return = array();
     512
     513                if ( ! is_array( $lines ) ) {
     514                        $lines = explode( "\n", $lines );
     515                }
     516
     517                while ( ( $line = array_shift( $lines ) ) !== null ) {
     518                        $trimmed = trim( $line );
     519                        if ( empty( $trimmed ) ) {
     520                                continue;
     521                        }
     522
     523                        if ( in_array( $trimmed[0], array( '=', '#', '*' ), true ) ) {
     524                                if ( ! empty( $value ) ) {
     525                                        $return[ $key ] = $this->sanitize_text( trim( $value ) );
     526                                }
     527
     528                                $value = '';
     529                                $key   = trim( $line, "=#* \t" );
     530                                continue;
     531                        }
     532
     533                        $value .= $line . "\n";
     534                }
     535
     536                if ( ! empty( $key ) || ! empty( $value ) ) {
     537                        $return[ $key ] = $this->sanitize_text( trim( $value ) );
     538                }
     539
     540                return $return;
     541        }
     542
    497543        /**
    498544         * @param string $text
    499545         * @return string