Skip to content

Commit 84b3b72

Browse files
authored
Merge pull request #1437 from rust-lang-ja/summary-with-html-comments
Skip HTML nodes in SUMMARY.md
2 parents 71ba6c9 + 30ce7e7 commit 84b3b72

File tree

1 file changed

+110
-6
lines changed

1 file changed

+110
-6
lines changed

src/book/summary.rs

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,19 @@ impl<'a> SummaryParser<'a> {
525525

526526
/// Try to parse the title line.
527527
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");
531532

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,
534540
}
535-
_ => None,
536541
}
537542
}
538543
}
@@ -973,4 +978,103 @@ mod tests {
973978

974979
assert_eq!(got, should_be);
975980
}
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+
}
9761080
}

0 commit comments

Comments
 (0)