|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Rewrite README.md into WordPress's readme.txt |
| 5 | + * |
| 6 | + * @codeCoverageIgnore |
| 7 | + * @package Syntax_Highlighting_Code_Block |
| 8 | + */ |
| 9 | + |
| 10 | +if ( 'cli' !== php_sapi_name() ) { |
| 11 | + fwrite( STDERR, "Must run from CLI.\n" ); |
| 12 | + exit( __LINE__ ); |
| 13 | +} |
| 14 | + |
| 15 | +$readme_md = file_get_contents( __DIR__ . '/../README.md' ); |
| 16 | + |
| 17 | +$readme_txt = $readme_md; |
| 18 | + |
| 19 | +// Transform the sections above the description. |
| 20 | +$readme_txt = preg_replace_callback( |
| 21 | + '/^.+?(?=## Description)/s', |
| 22 | + static function ( $matches ) { |
| 23 | + // Delete lines with images. |
| 24 | + $input = trim( preg_replace( '/\[?!\[.+/', '', $matches[0] ) ); |
| 25 | + |
| 26 | + $parts = preg_split( '/\n\n+/', $input ); |
| 27 | + |
| 28 | + if ( 3 !== count( $parts ) ) { |
| 29 | + fwrite( STDERR, "Too many sections in header found.\n" ); |
| 30 | + exit( __LINE__ ); |
| 31 | + } |
| 32 | + |
| 33 | + $header = $parts[0]; |
| 34 | + |
| 35 | + $description = $parts[1]; |
| 36 | + if ( strlen( $description ) > 150 ) { |
| 37 | + fwrite( STDERR, "The short description is too long: $description\n" ); |
| 38 | + exit( __LINE__ ); |
| 39 | + } |
| 40 | + |
| 41 | + $metadata = []; |
| 42 | + foreach ( explode( "\n", $parts[2] ) as $meta ) { |
| 43 | + $meta = trim( $meta ); |
| 44 | + if ( ! preg_match( '/^\*\*(?P<key>.+?):\*\* (?P<value>.+)/', $meta, $matches ) ) { |
| 45 | + fwrite( STDERR, "Parse error for meta line: $meta.\n" ); |
| 46 | + exit( __LINE__ ); |
| 47 | + } |
| 48 | + |
| 49 | + $unlinked_value = preg_replace( '/\[(.+?)]\(.+?\)/', '$1', $matches['value'] ); |
| 50 | + |
| 51 | + $metadata[ $matches['key'] ] = $unlinked_value; |
| 52 | + |
| 53 | + // Extract License URI from link. |
| 54 | + if ( 'License' === $matches['key'] ) { |
| 55 | + $license_uri = preg_replace( '/\[.+?]\((.+?)\)/', '$1', $matches['value'] ); |
| 56 | + |
| 57 | + if ( 0 !== strpos( $license_uri, 'http' ) ) { |
| 58 | + fwrite( STDERR, "Unable to extract License URI from: $meta.\n" ); |
| 59 | + exit( __LINE__ ); |
| 60 | + } |
| 61 | + |
| 62 | + $metadata['License URI'] = $license_uri; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $expected_metadata = [ |
| 67 | + 'Contributors', |
| 68 | + 'Tags', |
| 69 | + 'Requires at least', |
| 70 | + 'Tested up to', |
| 71 | + 'Stable tag', |
| 72 | + 'License', |
| 73 | + 'License URI', |
| 74 | + 'Requires PHP', |
| 75 | + ]; |
| 76 | + foreach ( $expected_metadata as $key ) { |
| 77 | + if ( empty( $metadata[ $key ] ) ) { |
| 78 | + fwrite( STDERR, "Failed to parse metadata. Missing: $key\n" ); |
| 79 | + exit( __LINE__ ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + $replaced = "$header\n"; |
| 84 | + foreach ( $metadata as $key => $value ) { |
| 85 | + $replaced .= "$key: $value\n"; |
| 86 | + } |
| 87 | + $replaced .= "\n$description\n\n"; |
| 88 | + |
| 89 | + return $replaced; |
| 90 | + }, |
| 91 | + $readme_txt |
| 92 | +); |
| 93 | + |
| 94 | +// Replace image-linked YouTube videos with bare URLs. |
| 95 | +$readme_txt = preg_replace( |
| 96 | + '#\[!\[.+?]\(.+?\)]\((https://www\.youtube\.com/.+?)\)#', |
| 97 | + '$1', |
| 98 | + $readme_txt |
| 99 | +); |
| 100 | + |
| 101 | +// Fix up the screenshots. |
| 102 | +$screenshots_captioned = 0; |
| 103 | +$readme_txt = preg_replace_callback( |
| 104 | + '/(?<=## Screenshots\n\n)(.+?)(?=## Changelog)/s', |
| 105 | + static function ( $matches ) use ( &$screenshots_captioned ) { |
| 106 | + if ( ! preg_match_all( '/### (.+)/', $matches[0], $screenshot_matches ) ) { |
| 107 | + fwrite( STDERR, "Unable to parse screenshot headings.\n" ); |
| 108 | + exit( __LINE__ ); |
| 109 | + } |
| 110 | + |
| 111 | + $screenshot_txt = ''; |
| 112 | + foreach ( $screenshot_matches[1] as $i => $screenshot_caption ) { |
| 113 | + $screenshot_txt .= sprintf( "%d. %s\n", $i + 1, $screenshot_caption ); |
| 114 | + $screenshots_captioned++; |
| 115 | + } |
| 116 | + $screenshot_txt .= "\n"; |
| 117 | + |
| 118 | + return $screenshot_txt; |
| 119 | + }, |
| 120 | + $readme_txt, |
| 121 | + 1, |
| 122 | + $replace_count |
| 123 | +); |
| 124 | +if ( 0 === $replace_count ) { |
| 125 | + fwrite( STDERR, "Unable to transform screenshots.\n" ); |
| 126 | + exit( __LINE__ ); |
| 127 | +} |
| 128 | + |
| 129 | +$screenshot_files = glob( __DIR__ . '/../.wordpress-org/screenshot-*' ); |
| 130 | +if ( count( $screenshot_files ) !== $screenshots_captioned ) { |
| 131 | + fwrite( STDERR, "Number of screenshot files does not match number of screenshot captions.\n" ); |
| 132 | + exit( __LINE__ ); |
| 133 | +} |
| 134 | +foreach ( $screenshot_files as $i => $screenshot_file ) { |
| 135 | + if ( 0 !== strpos( basename( $screenshot_file ), sprintf( 'screenshot-%d.', $i + 1 ) ) ) { |
| 136 | + fwrite( STDERR, "Screenshot filename is not sequential: $screenshot_file.\n" ); |
| 137 | + exit( __LINE__ ); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// Convert markdown headings into WP readme headings for good measure. |
| 142 | +$readme_txt = preg_replace_callback( |
| 143 | + '/^(#+)\s(.+)/m', |
| 144 | + static function ( $matches ) { |
| 145 | + $md_heading_level = strlen( $matches[1] ); |
| 146 | + $heading_text = $matches[2]; |
| 147 | + |
| 148 | + // #: === |
| 149 | + // ##: == |
| 150 | + // ###: = |
| 151 | + $txt_heading_level = 4 - $md_heading_level; |
| 152 | + if ( $txt_heading_level <= 0 ) { |
| 153 | + fwrite( STDERR, "Heading too small to transform: {$matches[0]}.\n" ); |
| 154 | + exit( __LINE__ ); |
| 155 | + } |
| 156 | + |
| 157 | + return sprintf( |
| 158 | + '%1$s %2$s %1$s', |
| 159 | + str_repeat( '=', $txt_heading_level ), |
| 160 | + $heading_text |
| 161 | + ); |
| 162 | + }, |
| 163 | + $readme_txt, |
| 164 | + -1, |
| 165 | + $replace_count |
| 166 | +); |
| 167 | +if ( 0 === $replace_count ) { |
| 168 | + fwrite( STDERR, "Unable to transform headings.\n" ); |
| 169 | + exit( __LINE__ ); |
| 170 | +} |
| 171 | + |
| 172 | +if ( ! file_put_contents( __DIR__ . '/../readme.txt', $readme_txt ) ) { |
| 173 | + fwrite( STDERR, "Failed to write readme.txt.\n" ); |
| 174 | + exit( __LINE__ ); |
| 175 | +} |
| 176 | + |
| 177 | +fwrite( STDOUT, "Validated README.md and generated readme.txt\n" ); |
0 commit comments