Skip to content
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 4 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ built
/phpcs.xml
/phpunit.xml
/*.sql

# Generated via bin/transform-readme.php
/readme.txt
7 changes: 6 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = function( grunt ) {
'assets',
'back-compat',
'includes',
'README.md',
'src',
'templates',
'vendor',
Expand Down Expand Up @@ -71,6 +70,9 @@ module.exports = function( grunt ) {
stdout: true,
stderr: true,
},
transform_readme: {
command: 'php bin/transform-readme.php',
},
verify_matching_versions: {
command: 'php bin/verify-version-consistency.php',
},
Expand Down Expand Up @@ -147,6 +149,9 @@ module.exports = function( grunt ) {
return true;
} );

grunt.task.run( 'shell:transform_readme' );
paths.push( 'readme.txt' );

paths.push( 'composer.*' ); // Copy in order to be able to do run composer_install.
paths.push( 'assets/js/**/*.js' );
paths.push( 'assets/js/**/*.asset.php' );
Expand Down
49 changes: 30 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# AMP
# AMP Plugin for WordPress

Contributors: google, xwp, automattic, westonruter, albertomedina, schlessera, swissspidy, pierlo, johnwatkins0, joshuawold, ryankienstra
Tags: amp, mobile, optimization, accelerated mobile pages, framework, components, blocks, performance, ux, seo, official
Requires at least: 4.9
Tested up to: 5.6
Stable tag: 2.0.10
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Requires PHP: 5.6

![The Official AMP Plugin for WordPress](.wordpress-org/banner-1544x500.png)
![Banner](.wordpress-org/banner-1544x500.png)

The official AMP Plugin, supported by the AMP team. Formerly Accelerated Mobile Pages, AMP enables great experiences across both mobile and desktop.

**Contributors:** [google](https://profiles.wordpress.org/google), [xwp](https://profiles.wordpress.org/xwp), [automattic](https://profiles.wordpress.org/automattic), [westonruter](https://profiles.wordpress.org/westonruter), [albertomedina](https://profiles.wordpress.org/albertomedina), [schlessera](https://profiles.wordpress.org/schlessera), [swissspidy](https://profiles.wordpress.org/swissspidy), [pierlo](https://profiles.wordpress.org/pierlo), [johnwatkins0](https://profiles.wordpress.org/johnwatkins0), [joshuawold](https://profiles.wordpress.org/joshuawold), [ryankienstra](https://profiles.wordpress.org/ryankienstra)
**Tags:** [amp](https://wordpress.org/plugins/tags/amp), [mobile](https://wordpress.org/plugins/tags/mobile), [optimization](https://wordpress.org/plugins/tags/optimization), [accelerated mobile pages](https://wordpress.org/plugins/tags/accelerated-mobile-pages), [framework](https://wordpress.org/plugins/tags/framework), [components](https://wordpress.org/plugins/tags/components), [blocks](https://wordpress.org/plugins/tags/blocks), [performance](https://wordpress.org/plugins/tags/performance), [ux](https://wordpress.org/plugins/tags/ux), [seo](https://wordpress.org/plugins/tags/seo), [official](https://wordpress.org/plugins/tags/official)
**Requires at least:** 4.9
**Tested up to:** 5.6
**Stable tag:** 2.0.10
**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)
**Requires PHP:** 5.6

[![Build Status](https://github.com/ampproject/amp-wp/workflows/Build,%20test%20&%20measure/badge.svg)](https://github.com/ampproject/amp-wp/actions?query=branch%3Adevelop+workflow%3A%22Build%2C+test+%26+measure%22)
[![Coverage Status](https://img.shields.io/codecov/c/github/ampproject/amp-wp/develop.svg)](https://codecov.io/gh/ampproject/amp-wp)
[![Built with Grunt](https://gruntjs.com/cdn/builtwith.svg)](http://gruntjs.com)
Expand All @@ -21,11 +20,11 @@ The official AMP Plugin, supported by the AMP team. Formerly Accelerated Mobile

This official plugin from the AMP project enables AMP content publishing with WordPress in a way that is fully and seamlessly integrated with the standard mechanisms of the platform.

https://www.youtube.com/watch?v=s52JNMT59s8&list=PLXTOW_XMsIDRGRr5QDffrvND8Qh1RndFb
[![Play video on YouTube](https://i1.ytimg.com/vi/s52JNMT59s8/hqdefault.jpg)](https://www.youtube.com/watch?v=s52JNMT59s8&list=PLXTOW_XMsIDRGRr5QDffrvND8Qh1RndFb)

For more videos like this, check out the ongoing [AMP for WordPress video series](https://www.youtube.com/playlist?list=PLXTOW_XMsIDRGRr5QDffrvND8Qh1RndFb).

The key features are the following:
The plugin's key features include:

1. **Automate the process of generating AMP-valid markup as much as possible**, letting users follow the standard workflows they are used to in WordPress.
2. **Provide effective validation tools** to help users deal with AMP incompatibilities when they happen, including mechanisms for **identifying**, **contextualizing**, and **resolving issues caused by validation errors**.
Expand Down Expand Up @@ -88,17 +87,29 @@ Please see the [FAQs on amp-wp.org](https://amp-wp.org/documentation/frequently-

## Screenshots

1. New onboarding wizard to help you get started.<br>![New onboarding wizard to help you get started.](.wordpress-org/screenshot-1.png)
### New onboarding wizard to help you get started.

![New onboarding wizard to help you get started.](.wordpress-org/screenshot-1.png)

### Built for developers and non-technical content creators alike.

![Built for developers and non-technical content creators alike.](.wordpress-org/screenshot-2.png)

### Theme selection to enhance the Reader mode experience.

![Theme selection to enhance the Reader mode experience.](.wordpress-org/screenshot-3.png)

### Preview how your site looks across desktop and mobile before finalising changes.

2. Built for developers and non-technical content creators alike.<br>![Built for developers and non-technical content creators alike.](.wordpress-org/screenshot-2.png)
![Preview how your site looks across desktop and mobile before finalising changes.](.wordpress-org/screenshot-4.png)

3. Theme selection to enhance the Reader mode experience.<br>![Theme selection to enhance the Reader mode experience.](.wordpress-org/screenshot-3.png)
### Customize the design of AMP pages in the Customizer.

4. Preview how your site looks across desktop and mobile before finalising changes.<br>![Preview how your site looks across desktop and mobile before finalising changes.](.wordpress-org/screenshot-4.png)
![Customize the design of AMP pages in the Customizer.](.wordpress-org/screenshot-5.png)

5. Customize the design of AMP pages in the Customizer.<br>![Customize the design of AMP pages in the Customizer.](.wordpress-org/screenshot-5.png)
### Reopen the onboarding wizard, change individual options, or manage advanced settings.

6. Reopen the onboarding wizard, change individual options, or manage advanced settings.<br>![Reopen the onboarding wizard, change individual options, or manage advanced settings.](.wordpress-org/screenshot-6.png)
![Reopen the onboarding wizard, change individual options, or manage advanced settings.](.wordpress-org/screenshot-6.png)

## Changelog

Expand Down
178 changes: 178 additions & 0 deletions bin/transform-readme.php
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 ) {
Copy link
Contributor

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, and preg_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.

Copy link
Member Author

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

<?php
$str = 'abcba';

$str = preg_replace( '/b/', 'a', $str, -1, $replace_count );
var_dump( $replace_count );

$str = preg_replace( '/c/', 'a', $str, -1, $replace_count );
var_dump( $replace_count );

var_dump( $str );

The result is:

int(2)
int(1)
string(5) "aaaaa"

In other words, $replace_count is reset with each preg_replace_callback(). It doesn't increment on top of an existing variable. If it did, then the int(3) would be here instead of int(1).

Copy link
Contributor

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.

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" );
2 changes: 1 addition & 1 deletion bin/verify-version-consistency.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$versions = [];

$readme_md = file_get_contents( dirname( __FILE__ ) . '/../README.md' );
if ( ! preg_match( '/Stable tag:\s+(?P<version>\S+)/i', $readme_md, $matches ) ) {
if ( ! preg_match( '/\*\*Stable tag:\*\*\s+(?P<version>\S+)/i', $readme_md, $matches ) ) {
echo "Could not find stable tag in readme\n";
exit( 1 );
}
Expand Down