@@ -525,14 +525,19 @@ impl<'a> SummaryParser<'a> {
525
525
526
526
/// Try to parse the title line.
527
527
fn parse_title ( & mut self ) -> Option < String > {
528
- match self . next_event ( ) {
529
- Some ( Event :: Start ( Tag :: Heading ( 1 ) ) ) => {
530
- debug ! ( "Found a h1 in the SUMMARY" ) ;
528
+ loop {
529
+ match self . next_event ( ) {
530
+ Some ( Event :: Start ( Tag :: Heading ( 1 ) ) ) => {
531
+ debug ! ( "Found a h1 in the SUMMARY" ) ;
531
532
532
- let tags = collect_events ! ( self . stream, end Tag :: Heading ( 1 ) ) ;
533
- Some ( stringify_events ( tags) )
533
+ let tags = collect_events ! ( self . stream, end Tag :: Heading ( 1 ) ) ;
534
+ return Some ( stringify_events ( tags) ) ;
535
+ }
536
+ // Skip a HTML element such as a comment line.
537
+ Some ( Event :: Html ( _) ) => { }
538
+ // Otherwise, no title.
539
+ _ => return None ,
534
540
}
535
- _ => None ,
536
541
}
537
542
}
538
543
}
@@ -973,4 +978,103 @@ mod tests {
973
978
974
979
assert_eq ! ( got, should_be) ;
975
980
}
981
+
982
+ #[ test]
983
+ fn skip_html_comments ( ) {
984
+ let src = r#"<!--
985
+ # Title - En
986
+ -->
987
+ # Title - Local
988
+
989
+ <!--
990
+ [Prefix 00-01 - En](ch00-01.md)
991
+ [Prefix 00-02 - En](ch00-02.md)
992
+ -->
993
+ [Prefix 00-01 - Local](ch00-01.md)
994
+ [Prefix 00-02 - Local](ch00-02.md)
995
+
996
+ <!--
997
+ ## Section Title - En
998
+ -->
999
+ ## Section Title - Localized
1000
+
1001
+ <!--
1002
+ - [Ch 01-00 - En](ch01-00.md)
1003
+ - [Ch 01-01 - En](ch01-01.md)
1004
+ - [Ch 01-02 - En](ch01-02.md)
1005
+ -->
1006
+ - [Ch 01-00 - Local](ch01-00.md)
1007
+ - [Ch 01-01 - Local](ch01-01.md)
1008
+ - [Ch 01-02 - Local](ch01-02.md)
1009
+
1010
+ <!--
1011
+ - [Ch 02-00 - En](ch02-00.md)
1012
+ -->
1013
+ - [Ch 02-00 - Local](ch02-00.md)
1014
+
1015
+ <!--
1016
+ [Appendix A - En](appendix-01.md)
1017
+ [Appendix B - En](appendix-02.md)
1018
+ -->`
1019
+ [Appendix A - Local](appendix-01.md)
1020
+ [Appendix B - Local](appendix-02.md)
1021
+ "# ;
1022
+
1023
+ let mut parser = SummaryParser :: new ( src) ;
1024
+
1025
+ // ---- Title ----
1026
+ let title = parser. parse_title ( ) ;
1027
+ assert_eq ! ( title, Some ( String :: from( "Title - Local" ) ) ) ;
1028
+
1029
+ // ---- Prefix Chapters ----
1030
+
1031
+ let new_affix_item = |name, location| {
1032
+ SummaryItem :: Link ( Link {
1033
+ name : String :: from ( name) ,
1034
+ location : Some ( PathBuf :: from ( location) ) ,
1035
+ ..Default :: default ( )
1036
+ } )
1037
+ } ;
1038
+
1039
+ let should_be = vec ! [
1040
+ new_affix_item( "Prefix 00-01 - Local" , "ch00-01.md" ) ,
1041
+ new_affix_item( "Prefix 00-02 - Local" , "ch00-02.md" ) ,
1042
+ ] ;
1043
+
1044
+ let got = parser. parse_affix ( true ) . unwrap ( ) ;
1045
+ assert_eq ! ( got, should_be) ;
1046
+
1047
+ // ---- Numbered Chapters ----
1048
+
1049
+ let new_numbered_item = |name, location, numbers : & [ u32 ] , nested_items| {
1050
+ SummaryItem :: Link ( Link {
1051
+ name : String :: from ( name) ,
1052
+ location : Some ( PathBuf :: from ( location) ) ,
1053
+ number : Some ( SectionNumber ( numbers. to_vec ( ) ) ) ,
1054
+ nested_items,
1055
+ } )
1056
+ } ;
1057
+
1058
+ let ch01_nested = vec ! [
1059
+ new_numbered_item( "Ch 01-01 - Local" , "ch01-01.md" , & [ 1 , 1 ] , vec![ ] ) ,
1060
+ new_numbered_item( "Ch 01-02 - Local" , "ch01-02.md" , & [ 1 , 2 ] , vec![ ] ) ,
1061
+ ] ;
1062
+
1063
+ let should_be = vec ! [
1064
+ new_numbered_item( "Ch 01-00 - Local" , "ch01-00.md" , & [ 1 ] , ch01_nested) ,
1065
+ new_numbered_item( "Ch 02-00 - Local" , "ch02-00.md" , & [ 2 ] , vec![ ] ) ,
1066
+ ] ;
1067
+ let got = parser. parse_parts ( ) . unwrap ( ) ;
1068
+ assert_eq ! ( got, should_be) ;
1069
+
1070
+ // ---- Suffix Chapters ----
1071
+
1072
+ let should_be = vec ! [
1073
+ new_affix_item( "Appendix A - Local" , "appendix-01.md" ) ,
1074
+ new_affix_item( "Appendix B - Local" , "appendix-02.md" ) ,
1075
+ ] ;
1076
+
1077
+ let got = parser. parse_affix ( false ) . unwrap ( ) ;
1078
+ assert_eq ! ( got, should_be) ;
1079
+ }
976
1080
}
0 commit comments