mirror of
https://github.com/rust-lang/mdBook
synced 2024-12-15 07:12:35 +00:00
parent
0bc3544c81
commit
0d146ffa82
2 changed files with 86 additions and 31 deletions
|
@ -199,7 +199,8 @@ fn load_chapter<P: AsRef<Path>>(link: &Link, src_dir: P) -> Result<Chapter> {
|
||||||
.chain_err(|| format!("Chapter file not found, {}", link.location.display()))?;
|
.chain_err(|| format!("Chapter file not found, {}", link.location.display()))?;
|
||||||
|
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
f.read_to_string(&mut content)?;
|
f.read_to_string(&mut content)
|
||||||
|
.chain_err(|| format!("Unable to read \"{}\" ({})", link.name, location.display()))?;
|
||||||
|
|
||||||
let stripped = location
|
let stripped = location
|
||||||
.strip_prefix(&src_dir)
|
.strip_prefix(&src_dir)
|
||||||
|
@ -476,4 +477,43 @@ And here is some \
|
||||||
|
|
||||||
assert_eq!(visited, num_items);
|
assert_eq!(visited, num_items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cant_load_chapters_with_an_empty_path() {
|
||||||
|
let (_, temp) = dummy_link();
|
||||||
|
let summary = Summary {
|
||||||
|
numbered_chapters: vec![
|
||||||
|
SummaryItem::Link(Link {
|
||||||
|
name: String::from("Empty"),
|
||||||
|
location: PathBuf::from(""),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let got = load_book_from_disk(&summary, temp.path());
|
||||||
|
assert!(got.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cant_load_chapters_when_the_link_is_a_directory() {
|
||||||
|
let (_, temp) = dummy_link();
|
||||||
|
let dir = temp.path().join("nested");
|
||||||
|
fs::create_dir(&dir).unwrap();
|
||||||
|
|
||||||
|
let summary = Summary {
|
||||||
|
numbered_chapters: vec![
|
||||||
|
SummaryItem::Link(Link {
|
||||||
|
name: String::from("nested"),
|
||||||
|
location: dir,
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let got = load_book_from_disk(&summary, temp.path());
|
||||||
|
assert!(got.is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ use memchr::{self, Memchr};
|
||||||
use pulldown_cmark::{self, Alignment, Event, Tag};
|
use pulldown_cmark::{self, Alignment, Event, Tag};
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
|
|
||||||
/// Parse the text from a `SUMMARY.md` file into a sort of "recipe" to be
|
/// Parse the text from a `SUMMARY.md` file into a sort of "recipe" to be
|
||||||
/// used when loading a book from disk.
|
/// used when loading a book from disk.
|
||||||
///
|
///
|
||||||
|
@ -273,6 +272,9 @@ impl<'a> SummaryParser<'a> {
|
||||||
let link_content = collect_events!(self.stream, end Tag::Link(..));
|
let link_content = collect_events!(self.stream, end Tag::Link(..));
|
||||||
let name = stringify_events(link_content);
|
let name = stringify_events(link_content);
|
||||||
|
|
||||||
|
if href.is_empty() {
|
||||||
|
Err(self.parse_error("You can't have an empty link."))
|
||||||
|
} else {
|
||||||
Ok(Link {
|
Ok(Link {
|
||||||
name: name,
|
name: name,
|
||||||
location: PathBuf::from(href.to_string()),
|
location: PathBuf::from(href.to_string()),
|
||||||
|
@ -280,6 +282,7 @@ impl<'a> SummaryParser<'a> {
|
||||||
nested_items: Vec::new(),
|
nested_items: Vec::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse the numbered chapters. This assumes the opening list tag has
|
/// Parse the numbered chapters. This assumes the opening list tag has
|
||||||
/// already been consumed by a previous parser.
|
/// already been consumed by a previous parser.
|
||||||
|
@ -492,7 +495,9 @@ fn tag_eq(left: &Tag, right: &Tag) -> bool {
|
||||||
(&Tag::List(ref a), &Tag::List(ref b)) => a == b,
|
(&Tag::List(ref a), &Tag::List(ref b)) => a == b,
|
||||||
(&Tag::Item, &Tag::Item) => true,
|
(&Tag::Item, &Tag::Item) => true,
|
||||||
(&Tag::FootnoteDefinition(ref a), &Tag::FootnoteDefinition(ref b)) => a == b,
|
(&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::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::TableHead, &Tag::TableHead) => true,
|
||||||
(&Tag::TableRow, &Tag::TableRow) => true,
|
(&Tag::TableRow, &Tag::TableRow) => true,
|
||||||
(&Tag::TableCell, &Tag::TableCell) => true,
|
(&Tag::TableCell, &Tag::TableCell) => true,
|
||||||
|
@ -512,7 +517,7 @@ fn alignment_eq(left: Alignment, right: Alignment) -> bool {
|
||||||
(Alignment::Left, Alignment::Left) => true,
|
(Alignment::Left, Alignment::Left) => true,
|
||||||
(Alignment::Center, Alignment::Center) => true,
|
(Alignment::Center, Alignment::Center) => true,
|
||||||
(Alignment::Right, Alignment::Right) => true,
|
(Alignment::Right, Alignment::Right) => true,
|
||||||
_ => false
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -754,4 +759,14 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(got, should_be);
|
assert_eq!(got, should_be);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_empty_link_location_is_an_error() {
|
||||||
|
let src = "- [Empty]()\n";
|
||||||
|
let mut parser = SummaryParser::new(src);
|
||||||
|
parser.stream.next();
|
||||||
|
|
||||||
|
let got = parser.parse_numbered();
|
||||||
|
assert!(got.is_err());
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue