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

Use README.md as canonical and generate readme.txt from it at build time #265

Merged
merged 3 commits into from
Mar 9, 2023
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 @@ -5,3 +5,6 @@
/vendor
/node_modules
/dist

# Generated via bin/transform-readme.php
/readme.txt
1 change: 1 addition & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/dist/*</exclude-pattern>
<exclude-pattern>*/bin/*</exclude-pattern>
<exclude-pattern>phpstan-baseline.php</exclude-pattern>
</ruleset>
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = function (grunt) {
'language-names.php',
'editor-styles.css',
'style.css',
'README.md',
'readme.txt',
'LICENSE',
'build/*',
];
Expand Down
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Syntax-highlighting Code Block (with Server-side Rendering)

Contributors: westonruter, allejo
Tags: block, code, code syntax, syntax highlight, code highlighting
Requires at least: 5.6
Tested up to: 6.2
Stable tag: 1.4.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Requires PHP: 5.6

![Banner](.wordpress-org/banner-1544x500.png)

Extending the Code block with syntax highlighting rendered on the server, thus being AMP-compatible and having faster frontend performance.

**Contributors:** [westonruter](https://profiles.wordpress.org/westonruter), [allejo](https://profiles.wordpress.org/allejo)
**Tags:** [block](https://wordpress.org/plugins/tags/block), [code](https://wordpress.org/plugins/tags/code), [code syntax](https://wordpress.org/plugins/tags/code-syntax), [syntax highlight](https://wordpress.org/plugins/tags/syntax-highlight), [code highlighting](https://wordpress.org/plugins/tags/code-highlighting)
**Requires at least:** 5.6
**Tested up to:** 6.2
**Stable tag:** 1.4.0
**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)
**Requires PHP:** 5.6

[![Build Status](https://travis-ci.org/westonruter/syntax-highlighting-code-block.svg?branch=develop)](https://travis-ci.org/westonruter/syntax-highlighting-code-block)
[![Built with Grunt](https://gruntjs.com/cdn/builtwith.svg)](http://gruntjs.com)

Expand All @@ -37,9 +36,13 @@ This is a fork of [Code Syntax Block](https://github.com/mkaz/code-syntax-block)

## Screenshots

1. Code blocks can be added as normal, optionally overriding the auto-detected language. Also specify any lines to be highlighted, whether to show line numbers, and if the lines should wrap.<br>![Code blocks can be added as normal, optionally overriding the auto-detected language. Also specify any lines to be highlighted, whether to show line numbers, and if the lines should wrap.](.wordpress-org/screenshot-1.png)
### Code blocks can be added as normal, optionally overriding the auto-detected language. Also specify any lines to be highlighted, whether to show line numbers, and if the lines should wrap.

![Code blocks can be added as normal, optionally overriding the auto-detected language. Also specify any lines to be highlighted, whether to show line numbers, and if the lines should wrap.](.wordpress-org/screenshot-1.png)

### The Code block renders with syntax highlighting on the frontend without any JavaScript enqueued. Stylesheets are added only when block is on the page.

2. The Code block renders with syntax highlighting on the frontend without any JavaScript enqueued. Stylesheets are added only when block is on the page.<br>![The Code block renders with syntax highlighting on the frontend without any JavaScript enqueued. Stylesheets are added only when block is on the page.](.wordpress-org/screenshot-2.png)
![The Code block renders with syntax highlighting on the frontend without any JavaScript enqueued. Stylesheets are added only when block is on the page.](.wordpress-org/screenshot-2.png)

## Changelog

Expand Down
177 changes: 177 additions & 0 deletions bin/transform-readme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/usr/bin/env php
<?php
/**
* Rewrite README.md into WordPress's readme.txt
*
* @codeCoverageIgnore
* @package Syntax_Highlighting_Code_Block
*/

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__ );
}

$header = $parts[0];

$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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
"scripts": {
"update": "bin/update-highlight-libs.sh",
"build": "npm-run-all build:*",
"build:transform-readme": "php ./bin/transform-readme.php",
"build:clean": "if [ -e dist ]; then rm -r dist; fi; if [ -e build ]; then rm -r build; fi",
"build:js": "wp-scripts build src/index.js src/customize-controls.js --output-path=build",
"build:dist": "grunt dist",
"build:zip": "if [ -e syntax-highlighting-code-block.zip ]; then rm syntax-highlighting-code-block.zip; fi; cd dist; zip -r ../syntax-highlighting-code-block.zip .; cd ..; echo; echo \"ZIP of build: $(pwd)/syntax-highlighting-code-block.zip\"",
"deploy": "npm-run-all build deploy:*",
"deploy:verify-versions": "php ./bin/verify-version-consistency.php",
"deploy:wporg": "grunt wp_deploy",
"verify-matching-versions": "php ./bin/verify-version-consistency.php",
"deploy": "npm-run-all verify-matching-versions build && grunt wp_deploy",
"generate-language-names": "php ./bin/generate-language-names.php",
"check-engines": "wp-scripts check-engines",
"check-licenses": "wp-scripts check-licenses",
Expand Down