@@ -23,7 +23,7 @@ class DocGenerator {
2323 /**
2424 * PHP Parser instance.
2525 *
26- * @var ParserFactory
26+ * @var \PhpParser\Parser
2727 */
2828 private $ parser ;
2929
@@ -96,7 +96,8 @@ class DocGenerator {
9696 * @param string $output_dir Directory where documentation will be generated.
9797 */
9898 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 );
100101 $ this ->traverser = new NodeTraverser ();
101102 $ this ->traverser ->addVisitor ( new NameResolver () );
102103 $ this ->output_dir = rtrim ( $ output_dir , '/ ' );
@@ -415,13 +416,16 @@ private function generate_index_files() {
415416 if ( '' === $ dir ) {
416417 $ index_content = '# Code Reference ' . "\n\n" ;
417418 } 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" ;
419422 }
423+
420424 $ index_content .= '## Files ' . "\n\n" ;
421425
422426 foreach ( $ files as $ file ) {
423427 $ basename = basename ( $ file , '.md ' );
424- $ title = ucwords ( str_replace ( ' - ' , ' ' , $ basename ) );
428+ $ title = $ this -> format_file_title ( $ basename );
425429 $ index_content .= '- [ ' . $ title . ']( ' . $ basename . ') ' . "\n" ;
426430 }
427431
@@ -438,6 +442,49 @@ private function generate_index_files() {
438442 }
439443 }
440444
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+
441488 /**
442489 * Generate the META.md file containing undocumented elements and duplicate hooks.
443490 */
@@ -611,7 +658,7 @@ private function save_markdown( $file, $docs ) {
611658 mkdir ( $ output_dir , 0755 , true );
612659 }
613660
614- $ markdown = $ this ->generate_markdown ( $ docs );
661+ $ markdown = $ this ->generate_markdown ( $ docs, $ relative_path );
615662 file_put_contents ( $ output_path , $ markdown );
616663
617664 // Track files by directory for index generation
@@ -626,24 +673,33 @@ private function save_markdown( $file, $docs ) {
626673 /**
627674 * Generate markdown content from documentation array.
628675 *
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.
630678 * @return string Generated markdown content.
631679 */
632- private function generate_markdown ( $ docs ) {
680+ private function generate_markdown ( $ docs, $ source_path ) {
633681 $ markdown = '' ;
634682
635683 // Generate standalone functions documentation
636684 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" ;
638693 foreach ( $ docs ['functions ' ] as $ name => $ doc ) {
639694 $ markdown .= '## ` ' . $ name . '()` ' . "\n\n" ;
640695 $ markdown .= trim ( $ doc ) . "\n\n\n" ; // Add extra newline after each function
641696 }
642697 $ markdown .= "--- \n\n" ;
643698 }
644699
700+ // Handle class documentation (unchanged)
645701 if ( isset ( $ docs ['class ' ] ) ) {
646- $ markdown .= ' # ' . $ docs ['class ' ]['name ' ] . "\n\n" ;
702+ $ markdown .= $ this -> format_title ( $ docs ['class ' ]['name ' ] ) . "\n\n" ;
647703 $ markdown .= trim ( $ docs ['class ' ]['doc ' ] ) . "\n\n" ;
648704
649705 // Add properties section
@@ -668,6 +724,18 @@ private function generate_markdown( $docs ) {
668724 $ markdown = rtrim ( rtrim ( $ markdown ), "\n" ) . "\n" ;
669725 return $ markdown ;
670726 }
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+ }
671739}
672740
673741// Parse command line arguments
0 commit comments