Changeset 3477 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-parser.php
- Timestamp:
- 06/19/2016 08:40:01 PM (8 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-parser.php
r3476 r3477 1 1 <?php 2 namespace WordPressdotorg\Plugin_Directory; 2 namespace WordPressdotorg\Plugin_Directory\Readme; 3 use WordPressdotorg\Plugin_Directory\Markdown; 3 4 4 5 /** … … 7 8 * Based on Baikonur_ReadmeParser from https://github.com/rmccue/WordPress-Readme-Parser 8 9 * 9 * @package WordPressdotorg\Plugin_Directory 10 * @package WordPressdotorg\Plugin_Directory\Readme 10 11 */ 11 class Readme_Parser { 12 public $name = ''; 13 public $tags = array(); 14 public $requires = ''; 15 public $tested = ''; 16 public $contributors = array(); 17 public $stable_tag = ''; 18 public $donate_link = ''; 12 class Parser { 13 14 /** 15 * @var string 16 */ 17 public $name = ''; 18 19 /** 20 * @var array 21 */ 22 public $tags = array(); 23 24 /** 25 * @var string 26 */ 27 public $requires = ''; 28 29 /** 30 * @var string 31 */ 32 public $tested = ''; 33 34 /** 35 * @var array 36 */ 37 public $contributors = array(); 38 39 /** 40 * @var string 41 */ 42 public $stable_tag = ''; 43 44 /** 45 * @var string 46 */ 47 public $donate_link = ''; 48 49 /** 50 * @var string 51 */ 19 52 public $short_description = ''; 20 public $sections = array(); 21 public $upgrade_notice = array(); 22 public $screenshots = array(); 23 24 // These are the readme sections which we expect 53 54 /** 55 * @var array 56 */ 57 public $sections = array(); 58 59 /** 60 * @var array 61 */ 62 public $upgrade_notice = array(); 63 64 /** 65 * @var array 66 */ 67 public $screenshots = array(); 68 69 /** 70 * These are the readme sections that we expect. 71 * 72 * @var array 73 */ 25 74 private $expected_sections = array( 26 75 'description', … … 33 82 ); 34 83 35 // We alias these sections, from => to 84 /** 85 * We alias these sections, from => to 86 * 87 * @var array 88 */ 36 89 private $alias_sections = array( 37 90 'frequently_asked_questions' => 'faq', 38 'change_log' => 'changelog',39 'screenshot' => 'screenshots',91 'change_log' => 'changelog', 92 'screenshot' => 'screenshots', 40 93 ); 41 94 42 // These are the valid header mappings for the header 95 /** 96 * These are the valid header mappings for the header. 97 * 98 * @var array 99 */ 43 100 private $valid_headers = array( 44 101 'tested' => 'tested', … … 52 109 ); 53 110 111 /** 112 * Parser constructor. 113 * 114 * @param string $file 115 */ 54 116 public function __construct( $file ) { 55 117 if ( $file ) { … … 58 120 } 59 121 122 /** 123 * @param string $file 124 * @return bool 125 */ 60 126 protected function parse_readme( $file ) { 61 127 $contents = file_get_contents( $file ); 62 128 $contents = preg_split( '!\R!', $contents ); 63 64 129 $contents = array_map( array( $this, 'strip_newlines' ), $contents ); 65 130 66 // Strip UTF8 BOM if present 67 if ( strpos( $contents[0], "\xEF\xBB\xBF" ) === 0) {131 // Strip UTF8 BOM if present. 132 if ( 0 === strpos( $contents[0], "\xEF\xBB\xBF" ) ) { 68 133 $contents[0] = substr( $contents[0], 3 ); 69 134 } 70 135 71 // Convert UTF-16 files 72 if ( strpos( $contents[0], "\xFF\xFE" ) === 0) {136 // Convert UTF-16 files. 137 if ( 0 === strpos( $contents[0], "\xFF\xFE" ) ) { 73 138 foreach ( $contents as $i => $line ) { 74 $contents[ $i] = mb_convert_encoding( $line, 'UTF-8', 'UTF-16' );139 $contents[ $i ] = mb_convert_encoding( $line, 'UTF-8', 'UTF-16' ); 75 140 } 76 141 } … … 79 144 $this->name = $this->sanitize_text( trim( $line, "#= \t\0\x0B" ) ); 80 145 81 // Strip Github style header\n==== underlines 82 if ( '' === trim( $contents[0], '=-' ) ) {146 // Strip Github style header\n==== underlines. 147 if ( ! empty( $contents ) && '' === trim( $contents[0], '=-' ) ) { 83 148 array_shift( $contents ); 84 149 } … … 87 152 if ( 'plugin name' == strtolower( $this->name ) ) { 88 153 $this->name = $line = $this->get_first_nonwhitespace( $contents ); 89 // Ensure that the line read wasn't an actual header or description 154 155 // Ensure that the line read wasn't an actual header or description. 90 156 if ( strlen( $line ) > 50 || preg_match( '~^(' . implode( '|', array_keys( $this->valid_headers ) ) . ')\s*:~i', $line ) ) { 91 157 $this->name = false; … … 94 160 } 95 161 96 // Parse headers 162 // Parse headers. 97 163 $headers = array(); 98 164 … … 100 166 do { 101 167 $value = null; 102 if ( strpos( $line, ':' ) === false ) { 168 if ( false === strpos( $line, ':' ) ) { 169 103 170 // Some plugins have line-breaks within the headers. 104 171 if ( ! empty( $line ) ) { … … 140 207 } 141 208 142 // Parse the short description 209 // Parse the short description. 143 210 while ( ( $line = array_shift( $contents ) ) !== null ) { 144 211 $trimmed = trim( $line ); … … 148 215 } 149 216 if ( ( '=' === $trimmed[0] && isset( $trimmed[1] ) && '=' === $trimmed[1] ) || 150 ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] ) ) { // Stop after any Markdown heading 217 ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] ) 218 ) { 219 220 // Stop after any Markdown heading. 151 221 array_unshift( $contents, $line ); 152 222 break; … … 157 227 $this->short_description = trim( $this->short_description ); 158 228 159 // Parse the rest of the body 160 // Prefill the sections, we'll filter out empty sections later. 229 /* 230 * Parse the rest of the body. 231 * Pre-fill the sections, we'll filter out empty sections later. 232 */ 161 233 $this->sections = array_fill_keys( $this->expected_sections, '' ); 162 $current = $section_name = $section_title = '';234 $current = $section_name = $section_title = ''; 163 235 while ( ( $line = array_shift( $contents ) ) !== null ) { 164 236 $trimmed = trim( $line ); … … 168 240 } 169 241 242 // Stop only after a ## Markdown header, not a ###. 170 243 if ( ( '=' === $trimmed[0] && isset( $trimmed[1] ) && '=' === $trimmed[1] ) || 171 ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] && isset( $trimmed[2] ) && '#' !== $trimmed[2] ) ) { // Stop only after a ## Markdown header, not a ###. 244 ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] && isset( $trimmed[2] ) && '#' !== $trimmed[2] ) 245 ) { 246 172 247 if ( ! empty( $section_name ) ) { 173 248 $this->sections[ $section_name ] .= trim( $current ); … … 210 285 } 211 286 212 // Parse out the Upgrade Notice section into it's own data 287 // Parse out the Upgrade Notice section into it's own data. 213 288 if ( isset( $this->sections['upgrade_notice'] ) ) { 214 $lines = explode( "\n", $this->sections['upgrade_notice'] );289 $lines = explode( "\n", $this->sections['upgrade_notice'] ); 215 290 $version = null; 216 291 $current = ''; … … 240 315 241 316 // Markdownify! 242 $this->sections 243 $this->upgrade_notice 244 245 // Sanitize and trim the short_description to match requirements 317 $this->sections = array_map( array( $this, 'parse_markdown' ), $this->sections ); 318 $this->upgrade_notice = array_map( array( $this, 'parse_markdown' ), $this->upgrade_notice ); 319 320 // Sanitize and trim the short_description to match requirements. 246 321 $this->short_description = $this->sanitize_text( $this->short_description ); 247 322 $this->short_description = $this->trim_length( $this->short_description, 150 ); … … 252 327 preg_match_all( '#<li>(.*?)</li>#is', $this->sections['screenshots'], $screenshots, PREG_SET_ORDER ); 253 328 if ( $screenshots ) { 254 $i = 1; // Screenshots start from 1 329 $i = 1; // Screenshots start from 1. 255 330 foreach ( $screenshots as $ss ) { 256 331 $this->screenshots[ $i++ ] = $this->filter_text( $ss[1] ); … … 260 335 } 261 336 262 // Filter the HTML 337 // Filter the HTML. 263 338 $this->sections = array_map( array( $this, 'filter_text' ), $this->sections ); 264 339 … … 266 341 } 267 342 343 /** 344 * @access protected 345 * 346 * @param string $contents 347 * @return string 348 */ 268 349 protected function get_first_nonwhitespace( &$contents ) { 269 350 while ( ( $line = array_shift( $contents ) ) !== null ) { … … 277 358 } 278 359 360 /** 361 * @access protected 362 * 363 * @param string $line 364 * @return string 365 */ 279 366 protected function strip_newlines( $line ) { 280 367 return rtrim( $line, "\r\n" ); 281 368 } 282 369 370 /** 371 * @access protected 372 * 373 * @param string $desc 374 * @param int $length 375 * @return string 376 */ 283 377 protected function trim_length( $desc, $length = 150 ) { 284 378 if ( mb_strlen( $desc ) > $length ) { … … 362 456 foreach ( $users as $i => $name ) { 363 457 if ( $user = get_user_by( 'login', $name ) ) { 458 364 459 // Check the case of the user login matches. 365 460 if ( $name !== $user->user_login ) { … … 367 462 } 368 463 } elseif ( false !== ( $user = get_user_by( 'slug', $name ) ) ) { 369 // Overwrite the nicename with the user_login 464 465 // Overwrite the nicename with the user_login. 370 466 $users[ $i ] = $user->user_login; 371 467 } else { 372 // Unknown user, we'll skip these entirely to encourage correct readmes 468 469 // Unknown user, we'll skip these entirely to encourage correct readme files. 373 470 unset( $users[ $i ] ); 374 471 } 375 472 } 473 376 474 return $users; 377 475 } … … 385 483 protected function sanitize_stable_tag( $stable_tag ) { 386 484 $stable_tag = trim( $stable_tag ); 387 485 $stable_tag = trim( $stable_tag, '"\'' ); // "trunk" 388 486 $stable_tag = preg_replace( '!^/?tags/!i', '', $stable_tag ); // "tags/1.2.3" 389 487 $stable_tag = preg_replace( '![^a-z0-9_.-]!i', '', $stable_tag ); 390 488 391 // If the stable_tag begins with a ., we treat it as 0.blah 489 // If the stable_tag begins with a ., we treat it as 0.blah. 392 490 if ( '.' == substr( $stable_tag, 0, 1 ) ) { 393 491 $stable_tag = "0{$stable_tag}"; … … 397 495 } 398 496 497 /** 498 * @param string $text 499 * @return string 500 */ 399 501 protected function parse_markdown( $text ) { 400 502 static $markdown = null; 503 401 504 if ( is_null( $markdown ) ) { 402 505 $markdown = new Markdown(); … … 405 508 return $markdown->transform( $text ); 406 509 } 407 408 510 }
Note: See TracChangeset
for help on using the changeset viewer.