@@ -23,7 +23,7 @@ class DocGenerator {
23
23
/**
24
24
* PHP Parser instance.
25
25
*
26
- * @var ParserFactory
26
+ * @var \PhpParser\Parser
27
27
*/
28
28
private $ parser ;
29
29
@@ -96,7 +96,8 @@ class DocGenerator {
96
96
* @param string $output_dir Directory where documentation will be generated.
97
97
*/
98
98
public function __construct ( $ output_dir ) {
99
- $ this ->parser = ( new ParserFactory () )->create ( ParserFactory::PREFER_PHP7 );
99
+ $ parser_factory = new ParserFactory ();
100
+ $ this ->parser = $ parser_factory ->create ( ParserFactory::PREFER_PHP7 );
100
101
$ this ->traverser = new NodeTraverser ();
101
102
$ this ->traverser ->addVisitor ( new NameResolver () );
102
103
$ this ->output_dir = rtrim ( $ output_dir , '/ ' );
@@ -415,13 +416,16 @@ private function generate_index_files() {
415
416
if ( '' === $ dir ) {
416
417
$ index_content = '# Code Reference ' . "\n\n" ;
417
418
} else {
418
- $ index_content = '# ' . ( '. ' === $ dir ? ' Code Reference ' : ' ' . ucwords ( str_replace ( '/ ' , ' ' , $ dir ) ) ) . "\n\n" ;
419
+ // Format directory name for title
420
+ $ dir_title = $ this ->format_directory_title ( $ dir );
421
+ $ index_content = '# ' . $ dir_title . "\n\n" ;
419
422
}
423
+
420
424
$ index_content .= '## Files ' . "\n\n" ;
421
425
422
426
foreach ( $ files as $ file ) {
423
427
$ basename = basename ( $ file , '.md ' );
424
- $ title = ucwords ( str_replace ( ' - ' , ' ' , $ basename ) );
428
+ $ title = $ this -> format_file_title ( $ basename );
425
429
$ index_content .= '- [ ' . $ title . ']( ' . $ basename . ') ' . "\n" ;
426
430
}
427
431
@@ -438,6 +442,49 @@ private function generate_index_files() {
438
442
}
439
443
}
440
444
445
+ /**
446
+ * Format directory title with special cases.
447
+ *
448
+ * @param string $dir Directory name.
449
+ * @return string Formatted title.
450
+ */
451
+ private function format_directory_title ( $ dir ) {
452
+ if ( 'rest-api ' === $ dir ) {
453
+ return 'REST API ' ;
454
+ }
455
+ return '. ' === $ dir ? 'Code Reference ' : ucwords ( str_replace ( '/ ' , ' ' , $ dir ) );
456
+ }
457
+
458
+ /**
459
+ * Format file title with special cases.
460
+ *
461
+ * @param string $basename File basename.
462
+ * @return string Formatted title.
463
+ */
464
+ private function format_file_title ( $ basename ) {
465
+ // Replace common patterns
466
+ $ title = str_replace (
467
+ array (
468
+ 'rest-api ' ,
469
+ 'acf-rest-api ' ,
470
+ 'class-acf-rest ' ,
471
+ '-file ' ,
472
+ ),
473
+ array (
474
+ 'REST API ' ,
475
+ 'ACF REST API ' ,
476
+ 'Class ACF REST ' ,
477
+ '' ,
478
+ ),
479
+ $ basename
480
+ );
481
+
482
+ // Convert to title case and handle special words
483
+ $ title = ucwords ( str_replace ( '- ' , ' ' , $ title ) );
484
+
485
+ return $ title ;
486
+ }
487
+
441
488
/**
442
489
* Generate the META.md file containing undocumented elements and duplicate hooks.
443
490
*/
@@ -611,7 +658,7 @@ private function save_markdown( $file, $docs ) {
611
658
mkdir ( $ output_dir , 0755 , true );
612
659
}
613
660
614
- $ markdown = $ this ->generate_markdown ( $ docs );
661
+ $ markdown = $ this ->generate_markdown ( $ docs, $ relative_path );
615
662
file_put_contents ( $ output_path , $ markdown );
616
663
617
664
// Track files by directory for index generation
@@ -626,24 +673,33 @@ private function save_markdown( $file, $docs ) {
626
673
/**
627
674
* Generate markdown content from documentation array.
628
675
*
629
- * @param array $docs Documentation organized by type.
676
+ * @param array $docs Documentation organized by type.
677
+ * @param string $source_path The relative path to the source file.
630
678
* @return string Generated markdown content.
631
679
*/
632
- private function generate_markdown ( $ docs ) {
680
+ private function generate_markdown ( $ docs, $ source_path ) {
633
681
$ markdown = '' ;
634
682
635
683
// Generate standalone functions documentation
636
684
if ( ! empty ( $ docs ['functions ' ] ) ) {
637
- $ markdown .= '# Global Functions ' . "\n\n" ;
685
+ // Convert file path to title
686
+ // e.g., "api/api-helpers.php" becomes "API Helpers"
687
+ $ file_title = basename ( $ source_path , self ::PHP_EXT );
688
+ $ file_title = str_replace ( '- ' , ' ' , $ file_title );
689
+ $ file_title = ucwords ( $ file_title );
690
+ // Special handling for "api" to become "API"
691
+ $ file_title = preg_replace ( '/\b[Aa]pi\b/ ' , 'API ' , $ file_title );
692
+ $ markdown .= $ this ->format_title ( $ file_title . ' Global Functions ' ) . "\n\n" ;
638
693
foreach ( $ docs ['functions ' ] as $ name => $ doc ) {
639
694
$ markdown .= '## ` ' . $ name . '()` ' . "\n\n" ;
640
695
$ markdown .= trim ( $ doc ) . "\n\n\n" ; // Add extra newline after each function
641
696
}
642
697
$ markdown .= "--- \n\n" ;
643
698
}
644
699
700
+ // Handle class documentation (unchanged)
645
701
if ( isset ( $ docs ['class ' ] ) ) {
646
- $ markdown .= ' # ' . $ docs ['class ' ]['name ' ] . "\n\n" ;
702
+ $ markdown .= $ this -> format_title ( $ docs ['class ' ]['name ' ] ) . "\n\n" ;
647
703
$ markdown .= trim ( $ docs ['class ' ]['doc ' ] ) . "\n\n" ;
648
704
649
705
// Add properties section
@@ -668,6 +724,18 @@ private function generate_markdown( $docs ) {
668
724
$ markdown = rtrim ( rtrim ( $ markdown ), "\n" ) . "\n" ;
669
725
return $ markdown ;
670
726
}
727
+
728
+ /**
729
+ * Format a title for markdown documentation.
730
+ *
731
+ * @param string $title The title to format.
732
+ * @return string Formatted title.
733
+ */
734
+ private function format_title ( $ title ) {
735
+ // Convert "Api" or "api" to "API"
736
+ $ title = preg_replace ( '/\b[Aa]pi\b/ ' , 'API ' , $ title );
737
+ return '# ' . $ title ;
738
+ }
671
739
}
672
740
673
741
// Parse command line arguments
0 commit comments