Making WordPress.org

Changeset 3180


Ignore:
Timestamp:
05/19/2016 06:54:53 AM (8 years ago)
Author:
dd32
Message:

Plugin Directory: Split the Markdown specific items into their own class.

See #1724

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-readme-parser.php

    r3163 r3180  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory;
    3 use Michelf\MarkdownExtra;
     3use WordPressdotorg\Plugin_Directory\Markdown;
    44
    55/**
     
    77 *
    88 * Based on Baikonur_ReadmeParser from https://github.com/rmccue/WordPress-Readme-Parser
    9  *
    10  * Relies on \Michaelf\Markdown_Extra
    119 *
    1210 * @package WordPressdotorg\Plugin_Directory
     
    384382    protected function parse_markdown( $text ) {
    385383        static $markdown = null;
    386         if ( ! class_exists( '\\Michelf\\MarkdownExtra' ) ) {
    387             // TODO: Autoloader?
    388             include __DIR__ . '/libs/michelf-php-markdown-1.6.0/Michelf/MarkdownExtra.inc.php';
    389         }
    390384        if ( is_null( $markdown ) ) {
    391             $markdown = new MarkdownExtra();
    392         }
    393 
    394         $text = $this->code_trick( $text );
    395         $text = preg_replace( '/^[\s]*=[\s]+(.+?)[\s]+=/m', "\n" . '<h4>$1</h4>' . "\n", $text );
    396         $text = $markdown->transform( trim( $text ) );
    397 
    398         return trim( $text );
    399     }
    400 
    401     protected function code_trick( $text ) {
    402         // If doing markdown, first take any user formatted code blocks and turn them into backticks so that
    403         // markdown will preserve things like underscores in code blocks
    404         $text = preg_replace_callback( "!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( $this, 'code_trick_decodeit_cb' ), $text );
    405         $text = str_replace( array( "\r\n", "\r" ), "\n", $text );
    406 
    407         // Markdown can do inline code, we convert bbPress style block level code to Markdown style
    408         $text = preg_replace_callback( "!(^|\n)([ \t]*?)`(.*?)`!s", array( $this, 'code_trick_indent_cb' ), $text );
    409 
    410         return $text;
    411     }
    412 
    413     protected function code_trick_indent_cb( $matches ) {
    414         $text = $matches[3];
    415         $text = preg_replace( '|^|m', $matches[2] . '    ', $text );
    416 
    417         return $matches[1] . $text;
    418     }
    419 
    420     protected function code_trick_decodeit_cb( $matches ) {
    421         $text        = $matches[2];
    422         $trans_table = array_flip( get_html_translation_table( HTML_ENTITIES ) );
    423         $text        = strtr( $text, $trans_table );
    424         $text        = str_replace( '<br />', '', $text );
    425         $text        = str_replace( '&#38;', '&', $text );
    426         $text        = str_replace( '&#39;', "'", $text );
    427 
    428         if ( '<pre><code>' == $matches[1] ) {
    429             $text = "\n$text\n";
    430         }
    431 
    432         return "`$text`";
    433     }
     385            $markdown = new Markdown();
     386        }
     387
     388        return $markdown->transform( $text );
     389    }
     390
    434391}
Note: See TracChangeset for help on using the changeset viewer.