-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add readme.txt transformation from README.md #5815
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ built | |
/phpcs.xml | ||
/phpunit.xml | ||
/*.sql | ||
|
||
# Generated via bin/transform-readme.php | ||
/readme.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
/** | ||
* Rewrite README.md into WordPress's readme.txt | ||
* | ||
* @codeCoverageIgnore | ||
* @package AMP | ||
*/ | ||
|
||
if ( 'cli' !== php_sapi_name() ) { | ||
fwrite( STDERR, "Must run from CLI.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
$readme_md = file_get_contents( __DIR__ . '/../README.md' ); | ||
|
||
$readme_txt = $readme_md; | ||
|
||
// Transform the sections above the description. | ||
$readme_txt = preg_replace_callback( | ||
'/^.+?(?=## Description)/s', | ||
static function ( $matches ) { | ||
// Delete lines with images. | ||
$input = trim( preg_replace( '/\[?!\[.+/', '', $matches[0] ) ); | ||
|
||
$parts = preg_split( '/\n\n+/', $input ); | ||
|
||
if ( 3 !== count( $parts ) ) { | ||
fwrite( STDERR, "Too many sections in header found.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
// Replace verbose name on GitHub with short name for plugin directory. | ||
$header = '# AMP'; | ||
|
||
$description = $parts[1]; | ||
if ( strlen( $description ) > 150 ) { | ||
fwrite( STDERR, "The short description is too long: $description\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
$metadata = []; | ||
foreach ( explode( "\n", $parts[2] ) as $meta ) { | ||
$meta = trim( $meta ); | ||
if ( ! preg_match( '/^\*\*(?P<key>.+?):\*\* (?P<value>.+)/', $meta, $matches ) ) { | ||
fwrite( STDERR, "Parse error for meta line: $meta.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
$unlinked_value = preg_replace( '/\[(.+?)]\(.+?\)/', '$1', $matches['value'] ); | ||
|
||
$metadata[ $matches['key'] ] = $unlinked_value; | ||
|
||
// Extract License URI from link. | ||
if ( 'License' === $matches['key'] ) { | ||
$license_uri = preg_replace( '/\[.+?]\((.+?)\)/', '$1', $matches['value'] ); | ||
|
||
if ( 0 !== strpos( $license_uri, 'http' ) ) { | ||
fwrite( STDERR, "Unable to extract License URI from: $meta.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
$metadata['License URI'] = $license_uri; | ||
} | ||
} | ||
|
||
$expected_metadata = [ | ||
'Contributors', | ||
'Tags', | ||
'Requires at least', | ||
'Tested up to', | ||
'Stable tag', | ||
'License', | ||
'License URI', | ||
'Requires PHP', | ||
]; | ||
foreach ( $expected_metadata as $key ) { | ||
if ( empty( $metadata[ $key ] ) ) { | ||
fwrite( STDERR, "Failed to parse metadata. Missing: $key\n" ); | ||
exit( __LINE__ ); | ||
} | ||
} | ||
|
||
$replaced = "$header\n"; | ||
foreach ( $metadata as $key => $value ) { | ||
$replaced .= "$key: $value\n"; | ||
} | ||
$replaced .= "\n$description\n\n"; | ||
|
||
return $replaced; | ||
}, | ||
$readme_txt | ||
); | ||
|
||
// Replace image-linked YouTube videos with bare URLs. | ||
$readme_txt = preg_replace( | ||
'#\[!\[.+?]\(.+?\)]\((https://www\.youtube\.com/.+?)\)#', | ||
'$1', | ||
$readme_txt | ||
); | ||
|
||
// Fix up the screenshots. | ||
$screenshots_captioned = 0; | ||
$readme_txt = preg_replace_callback( | ||
'/(?<=## Screenshots\n\n)(.+?)(?=## Changelog)/s', | ||
static function ( $matches ) use ( &$screenshots_captioned ) { | ||
if ( ! preg_match_all( '/### (.+)/', $matches[0], $screenshot_matches ) ) { | ||
fwrite( STDERR, "Unable to parse screenshot headings.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
$screenshot_txt = ''; | ||
foreach ( $screenshot_matches[1] as $i => $screenshot_caption ) { | ||
$screenshot_txt .= sprintf( "%d. %s\n", $i + 1, $screenshot_caption ); | ||
$screenshots_captioned++; | ||
} | ||
$screenshot_txt .= "\n"; | ||
|
||
return $screenshot_txt; | ||
}, | ||
$readme_txt, | ||
1, | ||
$replace_count | ||
); | ||
if ( 0 === $replace_count ) { | ||
fwrite( STDERR, "Unable to transform screenshots.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
$screenshot_files = glob( __DIR__ . '/../.wordpress-org/screenshot-*' ); | ||
if ( count( $screenshot_files ) !== $screenshots_captioned ) { | ||
fwrite( STDERR, "Number of screenshot files does not match number of screenshot captions.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
foreach ( $screenshot_files as $i => $screenshot_file ) { | ||
if ( 0 !== strpos( basename( $screenshot_file ), sprintf( 'screenshot-%d.', $i + 1 ) ) ) { | ||
fwrite( STDERR, "Screenshot filename is not sequential: $screenshot_file.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
} | ||
|
||
// Convert markdown headings into WP readme headings for good measure. | ||
$readme_txt = preg_replace_callback( | ||
'/^(#+)\s(.+)/m', | ||
static function ( $matches ) { | ||
$md_heading_level = strlen( $matches[1] ); | ||
$heading_text = $matches[2]; | ||
|
||
// #: === | ||
// ##: == | ||
// ###: = | ||
$txt_heading_level = 4 - $md_heading_level; | ||
if ( $txt_heading_level <= 0 ) { | ||
fwrite( STDERR, "Heading too small to transform: {$matches[0]}.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
return sprintf( | ||
'%1$s %2$s %1$s', | ||
str_repeat( '=', $txt_heading_level ), | ||
$heading_text | ||
); | ||
}, | ||
$readme_txt, | ||
-1, | ||
$replace_count | ||
); | ||
if ( 0 === $replace_count ) { | ||
fwrite( STDERR, "Unable to transform headings.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
if ( ! file_put_contents( __DIR__ . '/../readme.txt', $readme_txt ) ) { | ||
fwrite( STDERR, "Failed to write readme.txt.\n" ); | ||
exit( __LINE__ ); | ||
} | ||
|
||
fwrite( STDOUT, "Validated README.md and generated readme.txt\n" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition will never be true as
$replace_count
will always have a value > 0 after the screenshot transformations above, andpreg_replace_callback()
will increase that previous value by the number of replacements done here. So, the condition should either be$replace_count === $replace_count
, or use an unset variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to be the case: https://3v4l.org/0CGoT
The result is:
In other words,
$replace_count
is reset with eachpreg_replace_callback()
. It doesn't increment on top of an existing variable. If it did, then theint(3)
would be here instead ofint(1)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I've been terribly mistaken here. I was operating against changes I made locally 🤦. This is all good.