-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgenerate-manifest.php
More file actions
71 lines (60 loc) · 1.94 KB
/
generate-manifest.php
File metadata and controls
71 lines (60 loc) · 1.94 KB
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
#!/usr/bin/env php
<?php
/**
* Generate a manifest of all documentation files.
*
* @package wordpress/secure-custom-fields
*/
// phpcs:disable WordPress.WP.AlternativeFunctions -- Using native PHP functions as this is a CLI script.
$root = dirname( __DIR__ ); // docs directory
$repo = 'wordpress/secure-custom-fields';
$manifest = array();
$paths = array(
$root . '/*.md',
$root . '/*/*.md',
$root . '/*/*/*.md',
$root . '/*/*/*/*.md', // For deeper nesting like features/fields/accordion/
);
// Files to exclude from manifest
$excludes = array(
$root . '/README.md',
$root . '/bin/README.md',
);
foreach ( $paths as $path_pattern ) {
foreach ( glob( $path_pattern ) as $file ) {
// Skip specified README.md files and all META.md files.
if ( in_array( $file, $excludes, true ) || basename( $file ) === 'META.md' ) {
continue;
}
$slug = basename( $file, '.md' );
// Get relative path from docs directory
$key = str_replace( array( $root . '/', '.md' ), '', $file );
// Handle index.md files specially
if ( 'index' === $slug ) {
$bits = explode( '/', $key );
array_pop( $bits ); // Remove 'index'
$slug = end( $bits ); // Use parent directory name as slug
$key = implode( '/', $bits ); // Remove /index from key
}
$parent = null;
if ( stripos( $key, '/' ) ) {
$bits = explode( '/', $key );
array_pop( $bits );
$parent = implode( '/', $bits );
}
$manifest[ $key ] = array(
'slug' => $slug,
'parent' => $parent,
'markdown_source' => sprintf(
'https://github.com/%s/blob/trunk/docs/%s%s',
$repo,
$key,
basename( $file ) === 'index.md' ? '/index.md' : '.md'
),
);
}
}
file_put_contents( $root . '/bin/manifest.json', json_encode( (object) $manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
$count = count( $manifest );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
printf( 'Generated manifest.json of %d pages%s', $count, PHP_EOL );