-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathgenerate-manifest.php
More file actions
85 lines (73 loc) · 2.58 KB
/
generate-manifest.php
File metadata and controls
85 lines (73 loc) · 2.58 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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();
$slug_map = array(); // Track slugs and their sources
$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 );
// Special handling for tutorial files
if ( 'tutorial' === $slug ) {
$bits = explode( '/', $key );
array_pop( $bits ); // Remove 'tutorial'
$slug = end( $bits ) . '-tutorial'; // Use parent directory name plus -tutorial
} elseif ( 'index' === $slug ) { // Handle index.md files specially.
$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'
),
);
// Check for slug conflicts
if ( isset( $slug_map[ $slug ] ) ) {
printf( "\nError: Slug conflict detected!\n\n" );
printf( "Original entry:\n%s\n\n", json_encode( $manifest[ $slug_map[ $slug ] ], JSON_PRETTY_PRINT ) );
printf( "Conflicting entry:\n%s\n\n", json_encode( $manifest[ $key ], JSON_PRETTY_PRINT ) );
exit( 1 );
}
$slug_map[ $slug ] = $key;
}
}
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 );