Skip to content

Commit

Permalink
Merge pull request #5815 from ampproject/add/readme-txt-build-step
Browse files Browse the repository at this point in the history
Add readme.txt transformation from README.md
  • Loading branch information
westonruter authored Jan 26, 2021
2 parents e605767 + 6222a32 commit c641127
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 21 deletions.
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 ) {
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

0 comments on commit c641127

Please sign in to comment.