@@ -387,66 +387,73 @@ public function gen_commands_manifest() {
387387 */
388388 public function gen_hb_manifest () {
389389 $ manifest = [];
390- // Top-level pages
391- foreach ( glob ( WP_CLI_HANDBOOK_PATH . '/*.md ' ) as $ file ) {
392- $ slug = basename ( $ file , '.md ' );
393- if ( 'README ' === $ slug ) {
390+
391+ $ ignored_dirs = [
392+ '.git ' ,
393+ '.github ' ,
394+ 'bin ' ,
395+ 'commands ' ,
396+ 'vendor ' ,
397+ ];
398+
399+ $ files = new \RecursiveIteratorIterator (
400+ new \RecursiveCallbackFilterIterator (
401+ new \RecursiveDirectoryIterator ( WP_CLI_HANDBOOK_PATH , \RecursiveDirectoryIterator::SKIP_DOTS ),
402+ static function ( $ file ) use ( $ ignored_dirs ) {
403+ /** @var SplFileInfo $file */
404+
405+ if ( $ file ->isDir () && in_array ( $ file ->getBasename (), $ ignored_dirs , true ) ) {
406+ return false ;
407+ }
408+
409+ if ( $ file ->isFile () && $ file ->getExtension () !== 'md ' ) {
410+ return false ;
411+ }
412+
413+ if ( 'README.md ' === $ file ->getBasename () ) {
414+ return false ;
415+ }
416+
417+ return true ;
418+ }
419+ ),
420+ \RecursiveIteratorIterator::CHILD_FIRST
421+ );
422+
423+ foreach ( $ files as $ file ) {
424+ if ( $ file ->isDir () ) {
394425 continue ;
395426 }
396- $ title = '' ;
397- $ contents = file_get_contents ( $ file );
398- if ( preg_match ( '/^#\s(.+)/ ' , $ contents , $ matches ) ) {
399- $ title = $ matches [1 ];
400- }
401- $ manifest [ $ slug ] = [
402- 'title ' => $ title ,
403- 'slug ' => 'index ' === $ slug ? 'handbook ' : $ slug ,
404- 'markdown_source ' => sprintf (
405- 'https://github.com/wp-cli/handbook/blob/main/%s.md ' ,
406- $ slug
407- ),
408- 'parent ' => null ,
409- ];
410- }
411427
412- // Internal API pages.
413- foreach ( glob ( WP_CLI_HANDBOOK_PATH . '/internal-api/*.md ' ) as $ file ) {
414- $ slug = basename ( $ file , '.md ' );
415- $ title = '' ;
416- $ contents = file_get_contents ( $ file );
417- if ( preg_match ( '/^#\s(.+)/ ' , $ contents , $ matches ) ) {
418- $ title = $ matches [1 ];
419- }
420- $ manifest [ $ slug ] = [
421- 'title ' => $ title ,
422- 'slug ' => $ slug ,
423- 'markdown_source ' => sprintf (
424- 'https://github.com/wp-cli/handbook/blob/main/internal-api/%s.md ' ,
425- $ slug
426- ),
427- 'parent ' => 'internal-api ' ,
428- ];
429- }
428+ $ rel_path = str_replace ( WP_CLI_HANDBOOK_PATH . '/ ' , '' , $ file ->getPathname () );
429+
430+ $ path = explode ( '/ ' , $ rel_path );
431+ array_pop ( $ path );
432+
433+ $ parent = ! empty ( $ path ) ? end ( $ path ) : null ;
434+
435+ $ path = implode ( '/ ' , $ path );
436+
437+ $ slug = $ file ->getBasename ( '.md ' );
430438
431- // Behat steps pages.
432- foreach ( glob ( WP_CLI_HANDBOOK_PATH . '/behat-steps/*.md ' ) as $ file ) {
433- $ slug = basename ( $ file , '.md ' );
434439 $ title = '' ;
435- $ contents = file_get_contents ( $ file );
440+ $ contents = file_get_contents ( $ file-> getPathname () );
436441 if ( preg_match ( '/^#\s(.+)/ ' , $ contents , $ matches ) ) {
437442 $ title = $ matches [1 ];
438443 }
439444 $ manifest [ $ slug ] = [
440445 'title ' => $ title ,
441- 'slug ' => $ slug ,
446+ 'slug ' => ' index ' === $ slug ? ' handbook ' : $ slug ,
442447 'markdown_source ' => sprintf (
443- 'https://github.com/wp-cli/handbook/blob/main/behat-steps/%s.md ' ,
444- $ slug
448+ 'https://github.com/wp-cli/handbook/blob/main/%s ' ,
449+ $ rel_path
445450 ),
446- 'parent ' => ' behat-steps ' ,
451+ 'parent ' => $ parent ,
447452 ];
448453 }
449454
455+ ksort ( $ manifest );
456+
450457 file_put_contents ( WP_CLI_HANDBOOK_PATH . '/bin/handbook-manifest.json ' , json_encode ( $ manifest , JSON_PRETTY_PRINT ) );
451458 WP_CLI ::success ( 'Generated bin/handbook-manifest.json ' );
452459 }
0 commit comments