Skip to content

Commit d597141

Browse files
committed
Use README.md as canonical and generate readme.txt from it at build time
1 parent 91ae9d7 commit d597141

File tree

6 files changed

+199
-16
lines changed

6 files changed

+199
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
/vendor
66
/node_modules
77
/dist
8+
9+
# Generated via bin/transform-readme.php
10+
/readme.txt

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = function (grunt) {
5151
'language-names.php',
5252
'editor-styles.css',
5353
'style.css',
54-
'README.md',
54+
'readme.txt',
5555
'LICENSE',
5656
'build/*',
5757
];

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# Syntax-highlighting Code Block (with Server-side Rendering)
22

3-
Contributors: westonruter, allejo
4-
Tags: block, code, code syntax, syntax highlight, code highlighting
5-
Requires at least: 5.5
6-
Tested up to: 5.6
7-
Stable tag: 1.3.1
8-
License: GPLv2 or later
9-
License URI: http://www.gnu.org/licenses/gpl-2.0.html
10-
Requires PHP: 5.6
11-
123
![Banner](.wordpress-org/banner-1544x500.png)
134

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

7+
**Contributors:** [westonruter](https://profiles.wordpress.org/westonruter), [allejo](https://profiles.wordpress.org/allejo)
8+
**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)
9+
**Requires at least:** 5.5
10+
**Tested up to:** 5.6
11+
**Stable tag:** 1.3.1
12+
**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)
13+
**Requires PHP:** 5.6
14+
1615
[![Build Status](https://travis-ci.org/westonruter/syntax-highlighting-code-block.svg?branch=develop)](https://travis-ci.org/westonruter/syntax-highlighting-code-block)
1716
[![Built with Grunt](https://gruntjs.com/cdn/builtwith.svg)](http://gruntjs.com)
1817

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

3837
## Screenshots
3938

40-
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)
39+
### 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.
40+
41+
![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)
42+
43+
### The Code block renders with syntax highlighting on the frontend without any JavaScript enqueued. Stylesheets are added only when block is on the page.
4144

42-
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)
45+
![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)
4346

4447
## Changelog
4548

bin/transform-readme.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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" );

bin/verify-version-consistency.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$versions = [];
1616

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
"build:js": "wp-scripts build src/index.js src/customize-controls.js --output-path=build",
4444
"build:dist": "grunt dist",
4545
"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\"",
46-
"deploy": "npm-run-all build deploy:*",
47-
"deploy:verify-versions": "php bin/verify-version-consistency.php",
48-
"deploy:wporg": "grunt wp_deploy",
46+
"verify-matching-versions": "php bin/verify-version-consistency.php",
47+
"transform-readme": "php bin/transform-readme.php",
48+
"deploy": "npm-run-all transform-readme verify-matching-versions build && grunt wp_deploy",
4949
"generate-language-names": "php ./bin/generate-language-names.php",
5050
"check-engines": "wp-scripts check-engines",
5151
"check-licenses": "wp-scripts check-licenses",

0 commit comments

Comments
 (0)