@@ -328,42 +328,83 @@ fn generate_table_of_contents(
328
328
web_prefix : Option < & str > ,
329
329
) -> String {
330
330
let mut table_of_contents_html = "<ul>" . to_string ( ) ;
331
+ log:: debug!( "<ul>" ) ;
331
332
let mut prev_depth = 0 ;
333
+ let mut prev_file_depth = 0 ;
334
+ let mut prev_folders = Vec :: new ( ) ;
332
335
for result in results {
333
336
match result {
334
337
FirstPassResult :: Dir {
335
338
depth,
336
339
relative_path,
337
340
} => {
341
+ log:: trace!( "Dir: {}" , & relative_path. to_string_lossy( ) ) ;
338
342
let mut depth_diff = * depth as i32 - prev_depth as i32 ;
339
343
while depth_diff < 0 {
340
- table_of_contents_html. push_str ( "</ul>" ) ;
344
+ if prev_folders. pop ( ) . is_none ( ) {
345
+ let format_string = format ! ( "</ul>" ) ;
346
+ log:: debug!( "{} (Dir, depth_diff={})" , & format_string, depth_diff) ;
347
+ table_of_contents_html. push_str ( & format_string) ;
348
+ }
341
349
depth_diff += 1 ;
342
350
}
343
351
prev_depth = * depth;
344
352
if * depth > 0 {
345
- table_of_contents_html . push_str ( & format ! (
346
- "<li><b><u>{}:</u></b></li><ul> " ,
353
+ log :: trace !(
354
+ "Adding {} to the folders stack (at depth {}) " ,
347
355
& relative_path. to_string_lossy( ) ,
348
- ) ) ;
356
+ * depth
357
+ ) ;
358
+ prev_folders. push (
359
+ relative_path
360
+ . file_name ( )
361
+ . unwrap ( )
362
+ . to_string_lossy ( )
363
+ . to_string ( ) ,
364
+ ) ;
349
365
}
350
366
}
351
367
FirstPassResult :: HtmlOutput {
352
368
relative_path,
353
369
depth,
354
370
..
355
371
} => {
372
+ log:: trace!( "File: {}" , & relative_path. to_string_lossy( ) ) ;
356
373
let mut depth_diff = * depth as i32 - prev_depth as i32 ;
357
374
while depth_diff < 0 {
358
- table_of_contents_html. push_str ( "</ul>" ) ;
375
+ if prev_folders. pop ( ) . is_none ( ) {
376
+ let format_string = format ! ( "</ul>" ) ;
377
+ log:: debug!( "{} (File, depth_diff={})" , & format_string, depth_diff) ;
378
+ table_of_contents_html. push_str ( & format_string) ;
379
+ }
359
380
depth_diff += 1 ;
360
381
}
382
+ let mut pos_depth_diff = prev_folders. len ( ) ;
383
+ while pos_depth_diff > 0 {
384
+ let folder_name = prev_folders. remove ( 0 ) ;
385
+ let format_string = format ! ( "<li><b><u>{}:</u></b></li>" , & folder_name, ) ;
386
+ log:: debug!(
387
+ "{} (folder, depth={})" ,
388
+ & format_string,
389
+ ( * depth - pos_depth_diff)
390
+ ) ;
391
+ table_of_contents_html. push_str ( & format_string) ;
392
+ let format_string = format ! ( "<ul>" ) ;
393
+ log:: debug!( "{}, prev_folders-={}" , & format_string, & folder_name) ;
394
+ table_of_contents_html. push_str ( & format_string) ;
395
+ pos_depth_diff -= 1 ;
396
+ }
361
397
prev_depth = * depth;
398
+ prev_file_depth = * depth;
362
399
if relative_path == my_result {
363
- table_of_contents_html
364
- . push_str ( & format ! ( "<li><b>{}</b></li>" , & relative_path. to_string_lossy( ) ) )
400
+ let format_string = format ! (
401
+ "<li><b>{}</b></li>" ,
402
+ & relative_path. file_stem( ) . unwrap( ) . to_string_lossy( )
403
+ ) ;
404
+ log:: debug!( "{} (file, depth={})" , & format_string, * depth) ;
405
+ table_of_contents_html. push_str ( & format_string) ;
365
406
} else {
366
- table_of_contents_html . push_str ( & format ! (
407
+ let format_string = format ! (
367
408
"<li><a href=\" {}{}{}\" >{}</a></li>" ,
368
409
if my_depth > 1 {
369
410
"../" . repeat( my_depth - 1 )
@@ -372,19 +413,24 @@ fn generate_table_of_contents(
372
413
} ,
373
414
& web_prefix. unwrap_or( "" ) , // "./" if "" doesn't work
374
415
& relative_path. to_string_lossy( ) ,
375
- & relative_path. to_string_lossy( )
376
- ) )
416
+ & relative_path. file_stem( ) . unwrap( ) . to_string_lossy( )
417
+ ) ;
418
+ log:: debug!( "{} (file, depth={})" , & format_string, * depth) ;
419
+ table_of_contents_html. push_str ( & format_string) ;
377
420
}
378
421
}
379
422
}
380
423
}
381
-
382
- let mut depth_diff = 0 - prev_depth as i32 ;
424
+ prev_depth -= prev_folders. len ( ) ;
425
+ log:: trace!( "prev_depth - {} = {}" , prev_folders. len( ) , prev_depth) ;
426
+ log:: trace!( "prev_file_depth = {}" , prev_file_depth) ;
427
+ let mut depth_diff = 0 - prev_file_depth as i32 ;
383
428
while depth_diff < 0 {
384
- table_of_contents_html. push_str ( "</ul>" ) ;
429
+ let format_string = format ! ( "</ul>" ) ;
430
+ log:: debug!( "{} (end, depth_diff={})" , & format_string, depth_diff) ;
431
+ table_of_contents_html. push_str ( & format_string) ;
385
432
depth_diff += 1 ;
386
433
}
387
434
// log::debug!("Table of contents: {}", &table_of_contents_html);
388
-
389
435
table_of_contents_html
390
436
}
0 commit comments