/
class-wpcom-liveblog-wp-cli.php
60 lines (54 loc) · 1.93 KB
/
class-wpcom-liveblog-wp-cli.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
<?php
WP_CLI::add_command( 'liveblog', 'WPCOM_Liveblog_WP_CLI' );
class WPCOM_Liveblog_WP_CLI extends WP_CLI_Command {
public function readme_for_github() {
$readme_path = dirname( __FILE__ ) . '/../readme.txt';
$readme = file_get_contents( $readme_path );
$readme = $this->listify_meta( $readme );
$readme = $this->add_contributors_wp_org_profile_links( $readme );
$readme = $this->add_screenshot_links( $readme );
$readme = $this->markdownify_headings( $readme );
echo $readme;
}
private function markdownify_headings( $readme ) {
return preg_replace_callback( '/^\s*(=+)\s*(.*?)\s*=+\s*$/m',
function( $matches ) {
return "\n" . str_repeat( '#', 4 - strlen( $matches[1] ) ) . ' ' . $matches[2] . "\n";
},
$readme );
}
private function listify_meta( $readme ) {
return preg_replace_callback( '/===\s*\n+(.*?)\n\n/s',
function ( $matches ) {
$meta = $matches[1];
if ( !$meta ) return $matches[0];
return "===\n" . preg_replace( '/^/m', "* ", $meta ) . "\n\n";
},
$readme );
}
private function add_contributors_wp_org_profile_links( $readme ) {
return preg_replace_callback( '/Contributors: (.*)/',
function( $matches ) {
$links = array_filter( array_map(
function( $username ) {
return "[$username](http://profiles.wordpress.org/$username)";
}, preg_split( '/\s*,\s*/', $matches[1] ) ) );
return "Contributors: " . implode( ', ', $links );
},
$readme );
}
private function add_screenshot_links( $readme ) {
return preg_replace_callback( '/==\s*Screenshots\s*==\n(.*?)==/ms',
function ( $matches ) {
return "== Screenshots ==\n" . preg_replace( '/^\s*(\d+)\.\s*(.*?)$/m', '![\2](https://raw.github.com/Automattic/liveblog/master/screenshot-\1.png)', $matches[1] ) . "\n==";
},
$readme );
}
static function help() {
WP_CLI::line( <<<HELP
usage: wp liveblog readme_for_github
Converts the readme.txt to real markdown to be used as a README.md
HELP
);
}
}