-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathtransform-readme.php
executable file
·176 lines (149 loc) · 4.54 KB
/
transform-readme.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/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',
'Tested up to',
'Stable tag',
'License',
'License URI',
];
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" );