-
Notifications
You must be signed in to change notification settings - Fork 6
/
wp-readme.php
132 lines (122 loc) · 3.41 KB
/
wp-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
<?php
/**
* Generate readme.txt from GitHub's README.md
*
* @author fumikito
* @version 2.0.0
* @see https://github.com/fumikito/wp-readme
*/
/**
* Find readme and return file path.
*
* @param string $target_dir
*
* @return string
*/
function wp_readme_find( $target_dir = '.' ) {
$target_dir = rtrim( $target_dir, DIRECTORY_SEPARATOR );
if ( is_dir( $target_dir ) ) {
foreach ( scandir( $target_dir ) as $file ) {
if ( in_array( $file, [ 'readme.md', 'README.md' ] ) ) {
return $target_dir . DIRECTORY_SEPARATOR . $file;
}
}
}
return '';
}
/**
* @param $target_file
*
* @return bool
* @throws Exception
*/
function wp_readme_replace( $target_file ) {
if ( ! file_exists( $target_file ) ) {
throw new Exception( 'File not found.', 404 );
}
// Check if file is wriable.
$target_dir = dirname( realpath( $target_file ) );
if ( ! is_writable( $target_dir ) ) {
throw new Exception( 'Target directory is not writable.', 403 );
}
$new_file = dirname( $target_file ) . DIRECTORY_SEPARATOR . strtolower( preg_replace( '/\.md$/u', '.txt', basename( $target_file ) ) );
if ( file_exists( $new_file ) && ! is_writable( $new_file ) ) {
throw new Exception( 'readme.txt already exists and is not writable.', 403 );
}
$string = file_get_contents( $target_file );
$string = wp_readme_convert_string( $string );
// Save file.
if ( ! @file_put_contents( $new_file, $string ) ) {
throw new Exception( 'Failed to save readme.txt' );
}
return true;
}
/**
* Convert readme string.
*
* @param string $string
*
* @return string
*/
function wp_readme_convert_string( $string ) {
// Control visibility.
$string = wp_readme_visibility( $string );
// Replace headers.
$string = preg_replace_callback( '/^(#+)\s+(.*)/mu', function ( $match ) {
$length = strlen( $match[ 1 ] );
$sep = '';
for ( $i = 1, $l = 3 - ( $length - 1 ); $i <= $l; $i ++ ) {
$sep .= '=';
}
return "{$sep} {$match[2]} {$sep}";
}, $string );
// Format code.
$string = preg_replace( '/```([^\n`]*?)\n(.*?)\n```/us', '<pre>$2</pre>', $string );
return $string;
}
/**
* Convert visibility.
*
* @param $string
*
* @return string
*/
function wp_readme_visibility( $string ) {
// Remove github comment
$string = preg_replace( '#<!-- only:github/ -->(.*?)<!-- /only:github -->#us', '', $string );
// Display WordPress comment.
$string = preg_replace_callback( '#<!-- only:wp>(.*?)</only:wp -->#us', function( $matches ) {
return trim( $matches[1] );
}, $string );
// Handle env variable.
if ( $var = getenv( 'WP_README_ENV' ) ) {
$string = preg_replace_callback( sprintf( '#<!-- only:%1$s>(.*?)</only:%1$s -->#us', $var ), function( $matches ) {
return trim( $matches[1] );
}, $string );
$string = preg_replace_callback( '#<!-- not:([^/]+)/ -->(.*?)<!-- /not:[^ ]+ -->#us', function( $matches ) use ( $var ) {
if ( $var === $matches[1] ) {
return '';
} else {
return trim( $matches[2] );
}
}, $string );
}
return $string;
}
// This file is executed as main routine.
if ( ! debug_backtrace() ) {
$file = wp_readme_find( getenv( 'WP_README_DIR' ) ?: '.' );
try {
// File not found.
if ( ! $file ) {
throw new Exception( 'No README.md in current directory.' );
}
echo 'readme.md found...' . PHP_EOL;
if ( wp_readme_replace( $file ) ) {
echo 'readme.txt generated successfully!' . PHP_EOL;
}
} catch ( Exception $e ) {
echo '[ERROR]' . $e->getMessage() . PHP_EOL;
exit( 1 );
}
}