11<?php
22
3+ declare (strict_types=1 );
4+
35// Handle toctree functionality from RST
4- function after_filter_toctree ($ data , $ folder ) {
6+ function after_filter_toctree (string $ data , string $ folder ): ? string {
57 // Look for HTML toctree divs created by pandoc with --wrap=none
68 $ pattern = '/<div class="toctree"[^>]*>\s*(.*?)\s*<\/div>/s ' ;
79
8- return preg_replace_callback ($ pattern , function ($ matches ) use ($ folder ) {
10+ return preg_replace_callback ($ pattern , function (array $ matches ) use ($ folder ): string {
911 $ content = trim ($ matches [1 ]);
1012
1113 // Parse the toctree content which can be in two formats:
@@ -104,16 +106,12 @@ function after_filter_toctree($data, $folder) {
104106 }, $ data );
105107}
106108
107- function getSubdirectoryToctree ($ subdirPath , $ folder ) {
108- // Convert the target folder (docs) back to source folder (docs-rst)
109- $ targetPath = realpath ($ folder );
110-
111- // Handle both '/docs/' (middle of path) and '/docs' (end of path)
112- if (str_ends_with ($ targetPath , '/docs ' )) {
113- $ sourcePath = substr ($ targetPath , 0 , -5 ) . '/docs-rst ' ;
114- } else {
115- $ sourcePath = str_replace ('/docs/ ' , '/docs-rst/ ' , $ targetPath );
116- }
109+ /**
110+ * @return list<string>|null
111+ */
112+ function getSubdirectoryToctree (string $ subdirPath , string $ folder ): ?array {
113+ // Convert the target folder (docs) back to source folder
114+ $ sourcePath = rtrim (SOURCE_DIR , '/ ' ) . substr (realpath ($ folder ), strlen (realpath (TARGET_DIR )));
117115
118116 // Handle relative paths properly
119117 if (strpos ($ subdirPath , '../ ' ) === 0 ) {
@@ -132,15 +130,15 @@ function getSubdirectoryToctree($subdirPath, $folder) {
132130 $ content = file_get_contents ($ subdirRstFile );
133131
134132 // Look for toctree sections - improved regex to handle multiple toctree blocks
135- if (preg_match ('/\.\. toctree::\s*\n((?:\s +:[^:]+:[^\n]*\n)*)\s*\n((?:\s +[^\s][^\n]*\n?)*)/m ' , $ content , $ matches )) {
133+ if (preg_match ('/\.\. toctree::\s*\n((?:[ \t] +:[^:]+:[^\n]*\n)*)\s*\n((?:[ \t] +[^\s][^\n]*\n?)*)/m ' , $ content , $ matches )) {
136134 $ toctreeContent = $ matches [2 ];
137135
138136 // Extract individual entries (lines that are indented)
139137 $ lines = explode ("\n" , $ toctreeContent );
140138 $ entries = [];
141139
142140 // Get the main title of this index file to include as the first entry
143- if (preg_match ('/^#+\s*(.+) $/m ' , $ content , $ titleMatch )) {
141+ if (preg_match ('/^([^\s#*=.][^\n]*)\n[#*=]{3,}[ \t]* $/m ' , $ content , $ titleMatch )) {
144142 $ mainTitle = trim ($ titleMatch [1 ]);
145143
146144 // Determine the link path for the main index file
@@ -237,7 +235,7 @@ function getSubdirectoryToctree($subdirPath, $folder) {
237235 if (!empty ($ displayName )) {
238236 $ finalDisplayName = $ displayName ;
239237 } else {
240- $ headerDisplayName = getFirstHeaderFromFile ($ subdirFile , dirname ( $ folder ) );
238+ $ headerDisplayName = getFirstHeaderFromFile ($ subdirFile , TARGET_DIR );
241239 $ finalDisplayName = $ headerDisplayName ?: ucwords (str_replace (['_ ' , '- ' ], ' ' , basename ($ filename , '.md ' )));
242240 }
243241 }
@@ -251,7 +249,7 @@ function getSubdirectoryToctree($subdirPath, $folder) {
251249 return null ;
252250}
253251
254- function getFirstHeaderFromFile ($ filename , $ folder ) {
252+ function getFirstHeaderFromFile (string $ filename , string $ folder ): ? string {
255253 // Construct the full path to the target markdown file
256254 $ targetFile = '' ;
257255
0 commit comments