From 30deeec5dfbe09d3988da88390938839ab7be2e0 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Fri, 19 Jan 2018 00:38:05 +0800 Subject: [PATCH 1/2] Manually implemented PartialEq for pulldown_cmark types --- Cargo.toml | 2 -- src/book/summary.rs | 53 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27cc0f1e9b..b499eebcff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,5 +66,3 @@ doc = false name = "mdbook" path = "src/bin/mdbook.rs" -[patch.crates-io] -pulldown-cmark = { git = "https://github.com/google/pulldown-cmark" } diff --git a/src/book/summary.rs b/src/book/summary.rs index 4f2d1300e0..facdca4c6e 100644 --- a/src/book/summary.rs +++ b/src/book/summary.rs @@ -3,7 +3,7 @@ use std::iter::FromIterator; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; use memchr::{self, Memchr}; -use pulldown_cmark::{self, Event, Tag}; +use pulldown_cmark::{self, Alignment, Event, Tag}; use errors::*; @@ -310,7 +310,9 @@ impl<'a> SummaryParser<'a> { break; } Some(Event::Start(other_tag)) => { - if Tag::Rule == other_tag { + // FIXME: Remove this when google/pulldown_cmark#120 lands (new patch release) + // replace with `other_tag == Tag::Rule` + if tag_eq(&other_tag, &Tag::Rule) { items.push(SummaryItem::Separator); } trace!("Skipping contents of {:?}", other_tag); @@ -318,9 +320,15 @@ impl<'a> SummaryParser<'a> { // Skip over the contents of this tag loop { let next = self.next_event(); - - if next.is_none() || next == Some(Event::End(other_tag.clone())) { - break; + // FIXME: Remove this when google/pulldown_cmark#120 lands (new patch release) + // and replace it with: + // if next.is_none() || next == Some(Event::End(other_tag.clone())) { + // break; + // } + match next { + Some(Event::Start(ref t)) if tag_eq(t, &other_tag) => break, + None => break, + _ => {} } } @@ -474,6 +482,41 @@ fn stringify_events(events: Vec) -> String { .collect() } +// FIXME: Remove this when google/pulldown_cmark#120 lands (new patch release) +fn tag_eq(left: &Tag, right: &Tag) -> bool { + match (left, right) { + (&Tag::Paragraph, &Tag::Paragraph) => true, + (&Tag::Rule, &Tag::Rule) => true, + (&Tag::Header(a), &Tag::Header(b)) => a == b, + (&Tag::BlockQuote, &Tag::BlockQuote) => true, + (&Tag::CodeBlock(ref a), &Tag::CodeBlock(ref b)) => a == b, + (&Tag::List(ref a), &Tag::List(ref b)) => a == b, + (&Tag::Item, &Tag::Item) => true, + (&Tag::FootnoteDefinition(ref a), &Tag::FootnoteDefinition(ref b)) => a == b, + (&Tag::Table(ref a), &Tag::Table(ref b)) => a.iter().zip(b.iter()).all(|(l, r)| alignment_eq(*l, *r)), + (&Tag::TableHead, &Tag::TableHead) => true, + (&Tag::TableRow, &Tag::TableRow) => true, + (&Tag::TableCell, &Tag::TableCell) => true, + (&Tag::Emphasis, &Tag::Emphasis) => true, + (&Tag::Strong, &Tag::Strong) => true, + (&Tag::Code, &Tag::Code) => true, + (&Tag::Link(ref a_1, ref a_2), &Tag::Link(ref b_1, ref b_2)) => a_1 == b_1 && a_2 == b_2, + (&Tag::Image(ref a_1, ref a_2), &Tag::Image(ref b_1, ref b_2)) => a_1 == b_1 && a_2 == b_2, + _ => false, + } +} + +// FIXME: Remove this when google/pulldown_cmark#120 lands (new patch release) +fn alignment_eq(left: Alignment, right: Alignment) -> bool { + match (left, right) { + (Alignment::None, Alignment::None) => true, + (Alignment::Left, Alignment::Left) => true, + (Alignment::Center, Alignment::Center) => true, + (Alignment::Right, Alignment::Right) => true, + _ => false + } +} + /// A section number like "1.2.3", basically just a newtype'd `Vec` with /// a pretty `Display` impl. #[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)] From 3bc72811777aa2219a8d7cdd135fd53a52749726 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Fri, 19 Jan 2018 00:44:33 +0800 Subject: [PATCH 2/2] Fixed an issue where we wouldn't skip a tag properly --- src/book/summary.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/book/summary.rs b/src/book/summary.rs index facdca4c6e..16b67a7c7e 100644 --- a/src/book/summary.rs +++ b/src/book/summary.rs @@ -318,17 +318,16 @@ impl<'a> SummaryParser<'a> { trace!("Skipping contents of {:?}", other_tag); // Skip over the contents of this tag - loop { - let next = self.next_event(); + while let Some(event) = self.next_event() { // FIXME: Remove this when google/pulldown_cmark#120 lands (new patch release) - // and replace it with: - // if next.is_none() || next == Some(Event::End(other_tag.clone())) { + // and replace the nested if-let with: + // if next == Event::End(other_tag.clone()) { // break; // } - match next { - Some(Event::Start(ref t)) if tag_eq(t, &other_tag) => break, - None => break, - _ => {} + if let Event::End(tag) = event { + if tag_eq(&tag, &other_tag) { + break; + } } }