2019-05-26 02:50:41 +08:00
|
|
|
use crate::errors::*;
|
2018-07-24 01:45:01 +08:00
|
|
|
use memchr::{self, Memchr};
|
|
|
|
use pulldown_cmark::{self, Event, Tag};
|
2017-12-11 09:26:11 +08:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2017-12-11 12:17:20 +08:00
|
|
|
use std::iter::FromIterator;
|
2017-11-18 19:50:47 +08:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
/// Parse the text from a `SUMMARY.md` file into a sort of "recipe" to be
|
|
|
|
/// used when loading a book from disk.
|
|
|
|
///
|
|
|
|
/// # Summary Format
|
|
|
|
///
|
|
|
|
/// **Title:** It's common practice to begin with a title, generally
|
2017-12-11 15:50:31 +08:00
|
|
|
/// "# Summary". It's not mandatory and the parser (currently) ignores it, so
|
|
|
|
/// you can too if you feel like it.
|
2017-11-18 19:50:47 +08:00
|
|
|
///
|
|
|
|
/// **Prefix Chapter:** Before the main numbered chapters you can add a couple
|
|
|
|
/// of elements that will not be numbered. This is useful for forewords,
|
|
|
|
/// introductions, etc. There are however some constraints. You can not nest
|
|
|
|
/// prefix chapters, they should all be on the root level. And you can not add
|
|
|
|
/// prefix chapters once you have added numbered chapters.
|
|
|
|
///
|
|
|
|
/// ```markdown
|
|
|
|
/// [Title of prefix element](relative/path/to/markdown.md)
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// **Numbered Chapter:** Numbered chapters are the main content of the book,
|
|
|
|
/// they
|
|
|
|
/// will be numbered and can be nested, resulting in a nice hierarchy (chapters,
|
|
|
|
/// sub-chapters, etc.)
|
|
|
|
///
|
|
|
|
/// ```markdown
|
|
|
|
/// - [Title of the Chapter](relative/path/to/markdown.md)
|
|
|
|
/// ```
|
|
|
|
///
|
2017-12-11 15:50:31 +08:00
|
|
|
/// You can either use - or * to indicate a numbered chapter, the parser doesn't
|
|
|
|
/// care but you'll probably want to stay consistent.
|
2017-11-18 19:50:47 +08:00
|
|
|
///
|
|
|
|
/// **Suffix Chapter:** After the numbered chapters you can add a couple of
|
|
|
|
/// non-numbered chapters. They are the same as prefix chapters but come after
|
|
|
|
/// the numbered chapters instead of before.
|
|
|
|
///
|
|
|
|
/// All other elements are unsupported and will be ignored at best or result in
|
|
|
|
/// an error.
|
|
|
|
pub fn parse_summary(summary: &str) -> Result<Summary> {
|
|
|
|
let parser = SummaryParser::new(summary);
|
|
|
|
parser.parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The parsed `SUMMARY.md`, specifying how the book should be laid out.
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Summary {
|
|
|
|
/// An optional title for the `SUMMARY.md`, currently just ignored.
|
|
|
|
pub title: Option<String>,
|
|
|
|
/// Chapters before the main text (e.g. an introduction).
|
|
|
|
pub prefix_chapters: Vec<SummaryItem>,
|
|
|
|
/// The main chapters in the document.
|
|
|
|
pub numbered_chapters: Vec<SummaryItem>,
|
|
|
|
/// Items which come after the main document (e.g. a conclusion).
|
|
|
|
pub suffix_chapters: Vec<SummaryItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A struct representing an entry in the `SUMMARY.md`, possibly with nested
|
|
|
|
/// entries.
|
|
|
|
///
|
|
|
|
/// This is roughly the equivalent of `[Some section](./path/to/file.md)`.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Link {
|
|
|
|
/// The name of the chapter.
|
|
|
|
pub name: String,
|
|
|
|
/// The location of the chapter's source file, taking the book's `src`
|
|
|
|
/// directory as the root.
|
|
|
|
pub location: PathBuf,
|
|
|
|
/// The section number, if this chapter is in the numbered section.
|
|
|
|
pub number: Option<SectionNumber>,
|
|
|
|
/// Any nested items this chapter may contain.
|
|
|
|
pub nested_items: Vec<SummaryItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Link {
|
|
|
|
/// Create a new link with no nested items.
|
2020-03-25 06:52:24 +08:00
|
|
|
pub fn new<S: Into<String>, P: AsRef<Path>>(name: S, location: P) -> Link {
|
2017-11-18 19:50:47 +08:00
|
|
|
Link {
|
|
|
|
name: name.into(),
|
|
|
|
location: location.as_ref().to_path_buf(),
|
|
|
|
number: None,
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Link {
|
|
|
|
fn default() -> Self {
|
|
|
|
Link {
|
|
|
|
name: String::new(),
|
|
|
|
location: PathBuf::new(),
|
|
|
|
number: None,
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An item in `SUMMARY.md` which could be either a separator or a `Link`.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum SummaryItem {
|
|
|
|
/// A link to a chapter.
|
|
|
|
Link(Link),
|
|
|
|
/// A separator (`---`).
|
|
|
|
Separator,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SummaryItem {
|
|
|
|
fn maybe_link_mut(&mut self) -> Option<&mut Link> {
|
|
|
|
match *self {
|
|
|
|
SummaryItem::Link(ref mut l) => Some(l),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Link> for SummaryItem {
|
|
|
|
fn from(other: Link) -> SummaryItem {
|
|
|
|
SummaryItem::Link(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:50:31 +08:00
|
|
|
/// A recursive descent (-ish) parser for a `SUMMARY.md`.
|
2017-11-18 19:50:47 +08:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Grammar
|
|
|
|
///
|
|
|
|
/// The `SUMMARY.md` file has a grammar which looks something like this:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// summary ::= title prefix_chapters numbered_chapters
|
|
|
|
/// suffix_chapters
|
|
|
|
/// title ::= "# " TEXT
|
|
|
|
/// | EPSILON
|
|
|
|
/// prefix_chapters ::= item*
|
|
|
|
/// suffix_chapters ::= item*
|
|
|
|
/// numbered_chapters ::= dotted_item+
|
|
|
|
/// dotted_item ::= INDENT* DOT_POINT item
|
|
|
|
/// item ::= link
|
|
|
|
/// | separator
|
|
|
|
/// separator ::= "---"
|
|
|
|
/// link ::= "[" TEXT "]" "(" TEXT ")"
|
|
|
|
/// DOT_POINT ::= "-"
|
|
|
|
/// | "*"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// > **Note:** the `TEXT` terminal is "normal" text, and should (roughly)
|
|
|
|
/// > match the following regex: "[^<>\n[]]+".
|
|
|
|
struct SummaryParser<'a> {
|
2017-12-11 12:17:20 +08:00
|
|
|
src: &'a str,
|
2019-11-12 03:25:38 +08:00
|
|
|
stream: pulldown_cmark::OffsetIter<'a>,
|
|
|
|
offset: usize,
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads `Events` from the provided stream until the corresponding
|
|
|
|
/// `Event::End` is encountered which matches the `$delimiter` pattern.
|
|
|
|
///
|
|
|
|
/// This is the equivalent of doing
|
|
|
|
/// `$stream.take_while(|e| e != $delimeter).collect()` but it allows you to
|
|
|
|
/// use pattern matching and you won't get errors because `take_while()`
|
|
|
|
/// moves `$stream` out of self.
|
|
|
|
macro_rules! collect_events {
|
2018-07-24 01:45:01 +08:00
|
|
|
($stream:expr,start $delimiter:pat) => {
|
2017-12-11 12:17:20 +08:00
|
|
|
collect_events!($stream, Event::Start($delimiter))
|
|
|
|
};
|
2018-07-24 01:45:01 +08:00
|
|
|
($stream:expr,end $delimiter:pat) => {
|
2017-12-11 12:17:20 +08:00
|
|
|
collect_events!($stream, Event::End($delimiter))
|
|
|
|
};
|
2018-07-24 01:45:01 +08:00
|
|
|
($stream:expr, $delimiter:pat) => {{
|
|
|
|
let mut events = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
2019-11-12 03:25:38 +08:00
|
|
|
let event = $stream.next().map(|(ev, _range)| ev);
|
2018-07-24 01:45:01 +08:00
|
|
|
trace!("Next event: {:?}", event);
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Some($delimiter) => break,
|
|
|
|
Some(other) => events.push(other),
|
|
|
|
None => {
|
|
|
|
debug!(
|
|
|
|
"Reached end of stream without finding the closing pattern, {}",
|
|
|
|
stringify!($delimiter)
|
|
|
|
);
|
|
|
|
break;
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 01:45:01 +08:00
|
|
|
|
|
|
|
events
|
|
|
|
}};
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SummaryParser<'a> {
|
2019-05-07 04:50:34 +08:00
|
|
|
fn new(text: &str) -> SummaryParser<'_> {
|
2019-11-12 03:25:38 +08:00
|
|
|
let pulldown_parser = pulldown_cmark::Parser::new(text).into_offset_iter();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
SummaryParser {
|
2017-12-11 12:17:20 +08:00
|
|
|
src: text,
|
2017-11-18 19:50:47 +08:00
|
|
|
stream: pulldown_parser,
|
2019-11-12 03:25:38 +08:00
|
|
|
offset: 0,
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:47:29 +08:00
|
|
|
/// Get the current line and column to give the user more useful error
|
2017-12-11 15:50:31 +08:00
|
|
|
/// messages.
|
2017-12-11 12:17:20 +08:00
|
|
|
fn current_location(&self) -> (usize, usize) {
|
2019-11-12 03:25:38 +08:00
|
|
|
let previous_text = self.src[..self.offset].as_bytes();
|
2017-12-11 14:20:05 +08:00
|
|
|
let line = Memchr::new(b'\n', previous_text).count() + 1;
|
|
|
|
let start_of_line = memchr::memrchr(b'\n', previous_text).unwrap_or(0);
|
2019-11-12 03:25:38 +08:00
|
|
|
let col = self.src[start_of_line..self.offset].chars().count();
|
2017-12-11 14:20:05 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
(line, col)
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
/// Parse the text the `SummaryParser` was created with.
|
|
|
|
fn parse(mut self) -> Result<Summary> {
|
2017-12-11 12:17:20 +08:00
|
|
|
let title = self.parse_title();
|
|
|
|
|
2018-08-03 09:22:49 +08:00
|
|
|
let prefix_chapters = self
|
|
|
|
.parse_affix(true)
|
2017-12-11 12:17:20 +08:00
|
|
|
.chain_err(|| "There was an error parsing the prefix chapters")?;
|
2018-08-03 09:22:49 +08:00
|
|
|
let numbered_chapters = self
|
|
|
|
.parse_numbered()
|
2017-12-11 12:17:20 +08:00
|
|
|
.chain_err(|| "There was an error parsing the numbered chapters")?;
|
2018-08-03 09:22:49 +08:00
|
|
|
let suffix_chapters = self
|
|
|
|
.parse_affix(false)
|
2017-12-11 12:17:20 +08:00
|
|
|
.chain_err(|| "There was an error parsing the suffix chapters")?;
|
|
|
|
|
|
|
|
Ok(Summary {
|
|
|
|
title,
|
|
|
|
prefix_chapters,
|
|
|
|
numbered_chapters,
|
|
|
|
suffix_chapters,
|
|
|
|
})
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
/// Parse the affix chapters. This expects the first event (start of
|
|
|
|
/// paragraph) to have already been consumed by the previous parser.
|
|
|
|
fn parse_affix(&mut self, is_prefix: bool) -> Result<Vec<SummaryItem>> {
|
|
|
|
let mut items = Vec::new();
|
|
|
|
debug!(
|
2018-01-23 01:28:37 +08:00
|
|
|
"Parsing {} items",
|
2017-12-11 12:17:20 +08:00
|
|
|
if is_prefix { "prefix" } else { "suffix" }
|
|
|
|
);
|
|
|
|
|
|
|
|
loop {
|
2017-12-11 14:20:05 +08:00
|
|
|
match self.next_event() {
|
2017-12-11 12:17:20 +08:00
|
|
|
Some(Event::Start(Tag::List(..))) => {
|
|
|
|
if is_prefix {
|
|
|
|
// we've finished prefix chapters and are at the start
|
|
|
|
// of the numbered section.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
bail!(self.parse_error("Suffix chapters cannot be followed by a list"));
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 00:26:24 +08:00
|
|
|
Some(Event::Start(Tag::Link(_type, href, _title))) => {
|
2017-12-11 12:17:20 +08:00
|
|
|
let link = self.parse_link(href.to_string())?;
|
|
|
|
items.push(SummaryItem::Link(link));
|
|
|
|
}
|
2019-11-12 03:25:38 +08:00
|
|
|
Some(Event::Rule) => items.push(SummaryItem::Separator),
|
2017-12-11 12:17:20 +08:00
|
|
|
Some(_) => {}
|
|
|
|
None => break,
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
Ok(items)
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_link(&mut self, href: String) -> Result<Link> {
|
|
|
|
let link_content = collect_events!(self.stream, end Tag::Link(..));
|
|
|
|
let name = stringify_events(link_content);
|
|
|
|
|
2020-03-25 06:52:24 +08:00
|
|
|
if href.is_empty() {
|
|
|
|
Err(self.parse_error("You can't have an empty link."))
|
|
|
|
} else {
|
|
|
|
Ok(Link {
|
2018-12-04 07:10:09 +08:00
|
|
|
name,
|
2020-05-10 23:29:50 +08:00
|
|
|
location: PathBuf::from(href),
|
2018-01-22 20:47:29 +08:00
|
|
|
number: None,
|
|
|
|
nested_items: Vec::new(),
|
2020-03-25 06:52:24 +08:00
|
|
|
})
|
2018-01-22 20:47:29 +08:00
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
/// Parse the numbered chapters. This assumes the opening list tag has
|
|
|
|
/// already been consumed by a previous parser.
|
|
|
|
fn parse_numbered(&mut self) -> Result<Vec<SummaryItem>> {
|
|
|
|
let mut items = Vec::new();
|
2018-09-19 23:33:28 +08:00
|
|
|
let mut root_items = 0;
|
2017-12-11 12:17:20 +08:00
|
|
|
let root_number = SectionNumber::default();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
// we need to do this funny loop-match-if-let dance because a rule will
|
|
|
|
// close off any currently running list. Therefore we try to read the
|
|
|
|
// list items before the rule, then if we encounter a rule we'll add a
|
|
|
|
// separator and try to resume parsing numbered chapters if we start a
|
|
|
|
// list immediately afterwards.
|
|
|
|
//
|
|
|
|
// If you can think of a better way to do this then please make a PR :)
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
loop {
|
2017-12-11 14:20:05 +08:00
|
|
|
let mut bunch_of_items = self.parse_nested_numbered(&root_number)?;
|
|
|
|
|
|
|
|
// if we've resumed after something like a rule the root sections
|
|
|
|
// will be numbered from 1. We need to manually go back and update
|
|
|
|
// them
|
2018-09-19 23:33:28 +08:00
|
|
|
update_section_numbers(&mut bunch_of_items, 0, root_items);
|
|
|
|
root_items += bunch_of_items.len() as u32;
|
2017-12-11 12:17:20 +08:00
|
|
|
items.extend(bunch_of_items);
|
|
|
|
|
2017-12-11 14:20:05 +08:00
|
|
|
match self.next_event() {
|
|
|
|
Some(Event::Start(Tag::Paragraph)) => {
|
|
|
|
// we're starting the suffix chapters
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Some(Event::Start(other_tag)) => {
|
|
|
|
trace!("Skipping contents of {:?}", other_tag);
|
|
|
|
|
|
|
|
// Skip over the contents of this tag
|
2018-01-19 01:21:04 +08:00
|
|
|
while let Some(event) = self.next_event() {
|
2018-03-11 22:17:38 +08:00
|
|
|
if event == Event::End(other_tag.clone()) {
|
|
|
|
break;
|
2017-12-11 14:20:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(Event::Start(Tag::List(..))) = self.next_event() {
|
2017-12-11 12:17:20 +08:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 03:25:38 +08:00
|
|
|
Some(Event::Rule) => {
|
|
|
|
items.push(SummaryItem::Separator);
|
|
|
|
if let Some(Event::Start(Tag::List(..))) = self.next_event() {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 14:20:05 +08:00
|
|
|
Some(_) => {
|
|
|
|
// something else... ignore
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// EOF, bail...
|
2017-12-11 12:17:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
Ok(items)
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:20:05 +08:00
|
|
|
fn next_event(&mut self) -> Option<Event<'a>> {
|
2019-11-12 03:25:38 +08:00
|
|
|
let next = self.stream.next().map(|(ev, range)| {
|
|
|
|
self.offset = range.start;
|
|
|
|
ev
|
|
|
|
});
|
2017-12-11 14:20:05 +08:00
|
|
|
trace!("Next event: {:?}", next);
|
|
|
|
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_nested_numbered(&mut self, parent: &SectionNumber) -> Result<Vec<SummaryItem>> {
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Parsing numbered chapters at level {}", parent);
|
2017-12-11 12:17:20 +08:00
|
|
|
let mut items = Vec::new();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
loop {
|
2017-12-11 14:20:05 +08:00
|
|
|
match self.next_event() {
|
|
|
|
Some(Event::Start(Tag::Item)) => {
|
|
|
|
let item = self.parse_nested_item(parent, items.len())?;
|
|
|
|
items.push(item);
|
|
|
|
}
|
2017-12-11 12:17:20 +08:00
|
|
|
Some(Event::Start(Tag::List(..))) => {
|
2020-03-17 16:11:12 +08:00
|
|
|
// Skip this tag after comment bacause it is not nested.
|
|
|
|
if items.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-11 14:20:05 +08:00
|
|
|
// recurse to parse the nested list
|
2017-12-11 12:17:20 +08:00
|
|
|
let (_, last_item) = get_last_link(&mut items)?;
|
|
|
|
let last_item_number = last_item
|
|
|
|
.number
|
|
|
|
.as_ref()
|
|
|
|
.expect("All numbered chapters have numbers");
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let sub_items = self.parse_nested_numbered(last_item_number)?;
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
last_item.nested_items = sub_items;
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
2017-12-11 12:17:20 +08:00
|
|
|
Some(Event::End(Tag::List(..))) => break,
|
|
|
|
Some(_) => {}
|
|
|
|
None => break,
|
|
|
|
}
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
Ok(items)
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 14:20:05 +08:00
|
|
|
fn parse_nested_item(
|
|
|
|
&mut self,
|
|
|
|
parent: &SectionNumber,
|
|
|
|
num_existing_items: usize,
|
|
|
|
) -> Result<SummaryItem> {
|
|
|
|
loop {
|
|
|
|
match self.next_event() {
|
|
|
|
Some(Event::Start(Tag::Paragraph)) => continue,
|
2019-06-12 00:26:24 +08:00
|
|
|
Some(Event::Start(Tag::Link(_type, href, _title))) => {
|
2017-12-11 14:20:05 +08:00
|
|
|
let mut link = self.parse_link(href.to_string())?;
|
|
|
|
|
|
|
|
let mut number = parent.clone();
|
|
|
|
number.0.push(num_existing_items as u32 + 1);
|
|
|
|
trace!(
|
2018-01-23 01:28:37 +08:00
|
|
|
"Found chapter: {} {} ({})",
|
2017-12-11 14:20:05 +08:00
|
|
|
number,
|
|
|
|
link.name,
|
|
|
|
link.location.display()
|
|
|
|
);
|
|
|
|
|
|
|
|
link.number = Some(number);
|
|
|
|
|
|
|
|
return Ok(SummaryItem::Link(link));
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
warn!("Expected a start of a link, actually got {:?}", other);
|
|
|
|
bail!(self.parse_error(
|
|
|
|
"The link items for nested chapters must only contain a hyperlink"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_error<D: Display>(&self, msg: D) -> Error {
|
|
|
|
let (line, col) = self.current_location();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
ErrorKind::ParseError(line, col, msg.to_string()).into()
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to parse the title line.
|
|
|
|
fn parse_title(&mut self) -> Option<String> {
|
2019-11-12 03:25:38 +08:00
|
|
|
if let Some(Event::Start(Tag::Heading(1))) = self.next_event() {
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Found a h1 in the SUMMARY");
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2019-11-12 03:25:38 +08:00
|
|
|
let tags = collect_events!(self.stream, end Tag::Heading(1));
|
2017-11-18 19:50:47 +08:00
|
|
|
Some(stringify_events(tags))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 14:20:05 +08:00
|
|
|
fn update_section_numbers(sections: &mut [SummaryItem], level: usize, by: u32) {
|
|
|
|
for section in sections {
|
|
|
|
if let SummaryItem::Link(ref mut link) = *section {
|
|
|
|
if let Some(ref mut number) = link.number {
|
|
|
|
number.0[level] += by;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_section_numbers(&mut link.nested_items, level, by);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
/// Gets a pointer to the last `Link` in a list of `SummaryItem`s, and its
|
|
|
|
/// index.
|
|
|
|
fn get_last_link(links: &mut [SummaryItem]) -> Result<(usize, &mut Link)> {
|
|
|
|
links
|
|
|
|
.iter_mut()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(i, item)| item.maybe_link_mut().map(|l| (i, l)))
|
|
|
|
.rev()
|
|
|
|
.next()
|
2017-12-11 14:20:05 +08:00
|
|
|
.ok_or_else(|| {
|
|
|
|
"Unable to get last link because the list of SummaryItems doesn't contain any Links"
|
|
|
|
.into()
|
|
|
|
})
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the styling from a list of Markdown events and returns just the
|
|
|
|
/// plain text.
|
2019-05-07 04:50:34 +08:00
|
|
|
fn stringify_events(events: Vec<Event<'_>>) -> String {
|
2017-11-18 19:50:47 +08:00
|
|
|
events
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|t| match t {
|
2019-06-12 00:26:24 +08:00
|
|
|
Event::Text(text) | Event::Code(text) => Some(text.into_string()),
|
2017-11-18 19:50:47 +08:00
|
|
|
_ => None,
|
2019-05-05 22:57:43 +08:00
|
|
|
})
|
|
|
|
.collect()
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A section number like "1.2.3", basically just a newtype'd `Vec<u32>` with
|
|
|
|
/// a pretty `Display` impl.
|
|
|
|
#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)]
|
|
|
|
pub struct SectionNumber(pub Vec<u32>);
|
|
|
|
|
|
|
|
impl Display for SectionNumber {
|
2019-05-07 04:50:34 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2017-12-11 12:17:20 +08:00
|
|
|
if self.0.is_empty() {
|
|
|
|
write!(f, "0")
|
|
|
|
} else {
|
|
|
|
for item in &self.0 {
|
|
|
|
write!(f, "{}.", item)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SectionNumber {
|
|
|
|
type Target = Vec<u32>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for SectionNumber {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
impl FromIterator<u32> for SectionNumber {
|
|
|
|
fn from_iter<I: IntoIterator<Item = u32>>(it: I) -> Self {
|
|
|
|
SectionNumber(it.into_iter().collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn section_number_has_correct_dotted_representation() {
|
|
|
|
let inputs = vec![
|
|
|
|
(vec![0], "0."),
|
|
|
|
(vec![1, 3], "1.3."),
|
|
|
|
(vec![1, 2, 3], "1.2.3."),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, should_be) in inputs {
|
|
|
|
let section_number = SectionNumber(input).to_string();
|
|
|
|
assert_eq!(section_number, should_be);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_initial_title() {
|
|
|
|
let src = "# Summary";
|
|
|
|
let should_be = String::from("Summary");
|
|
|
|
|
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let got = parser.parse_title().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_title_with_styling() {
|
|
|
|
let src = "# My **Awesome** Summary";
|
|
|
|
let should_be = String::from("My Awesome Summary");
|
|
|
|
|
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let got = parser.parse_title().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn convert_markdown_events_to_a_string() {
|
|
|
|
let src = "Hello *World*, `this` is some text [and a link](./path/to/link)";
|
|
|
|
let should_be = "Hello World, this is some text and a link";
|
|
|
|
|
|
|
|
let events = pulldown_cmark::Parser::new(src).collect();
|
|
|
|
let got = stringify_events(events);
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_some_prefix_items() {
|
|
|
|
let src = "[First](./first.md)\n[Second](./second.md)\n";
|
2017-11-18 19:50:47 +08:00
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let should_be = vec![
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("Second"),
|
|
|
|
location: PathBuf::from("./second.md"),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
];
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let _ = parser.stream.next(); // step past first event
|
|
|
|
let got = parser.parse_affix(true).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_prefix_items_with_a_separator() {
|
|
|
|
let src = "[First](./first.md)\n\n---\n\n[Second](./second.md)\n";
|
2017-11-18 19:50:47 +08:00
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let _ = parser.stream.next(); // step past first event
|
|
|
|
let got = parser.parse_affix(true).unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
assert_eq!(got.len(), 3);
|
|
|
|
assert_eq!(got[1], SummaryItem::Separator);
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-11 12:17:20 +08:00
|
|
|
fn suffix_items_cannot_be_followed_by_a_list() {
|
|
|
|
let src = "[First](./first.md)\n- [Second](./second.md)\n";
|
2017-11-18 19:50:47 +08:00
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let _ = parser.stream.next(); // step past first event
|
|
|
|
let got = parser.parse_affix(false);
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
assert!(got.is_err());
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_a_link() {
|
|
|
|
let src = "[First](./first.md)";
|
|
|
|
let should_be = Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2017-11-18 19:50:47 +08:00
|
|
|
|
|
|
|
let mut parser = SummaryParser::new(src);
|
2017-12-11 12:17:20 +08:00
|
|
|
let _ = parser.stream.next(); // skip past start of paragraph
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let href = match parser.stream.next() {
|
2019-11-12 03:25:38 +08:00
|
|
|
Some((Event::Start(Tag::Link(_type, href, _title)), _range)) => href.to_string(),
|
2017-12-11 12:17:20 +08:00
|
|
|
other => panic!("Unreachable, {:?}", other),
|
|
|
|
};
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let got = parser.parse_link(href).unwrap();
|
|
|
|
assert_eq!(got, should_be);
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_a_numbered_chapter() {
|
|
|
|
let src = "- [First](./first.md)\n";
|
|
|
|
let link = Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
number: Some(SectionNumber(vec![1])),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let should_be = vec![SummaryItem::Link(link)];
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let _ = parser.stream.next();
|
|
|
|
|
|
|
|
let got = parser.parse_numbered().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-11 12:17:20 +08:00
|
|
|
fn parse_nested_numbered_chapters() {
|
|
|
|
let src = "- [First](./first.md)\n - [Nested](./nested.md)\n- [Second](./second.md)";
|
|
|
|
|
|
|
|
let should_be = vec![
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
number: Some(SectionNumber(vec![1])),
|
2018-07-24 01:45:01 +08:00
|
|
|
nested_items: vec![SummaryItem::Link(Link {
|
|
|
|
name: String::from("Nested"),
|
|
|
|
location: PathBuf::from("./nested.md"),
|
|
|
|
number: Some(SectionNumber(vec![1, 1])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
})],
|
2017-12-11 12:17:20 +08:00
|
|
|
}),
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("Second"),
|
|
|
|
location: PathBuf::from("./second.md"),
|
|
|
|
number: Some(SectionNumber(vec![2])),
|
2020-03-17 16:11:12 +08:00
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let _ = parser.stream.next();
|
|
|
|
|
|
|
|
let got = parser.parse_numbered().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_numbered_chapters_separated_by_comment() {
|
|
|
|
let src = "- [First](./first.md)\n<!-- this is a comment -->\n- [Second](./second.md)";
|
|
|
|
|
|
|
|
let should_be = vec![
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
number: Some(SectionNumber(vec![1])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("Second"),
|
|
|
|
location: PathBuf::from("./second.md"),
|
|
|
|
number: Some(SectionNumber(vec![2])),
|
2017-12-11 12:17:20 +08:00
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
2017-11-18 19:50:47 +08:00
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let _ = parser.stream.next();
|
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
let got = parser.parse_numbered().unwrap();
|
2017-11-18 19:50:47 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
assert_eq!(got, should_be);
|
2017-11-18 19:50:47 +08:00
|
|
|
}
|
2017-12-11 14:20:05 +08:00
|
|
|
|
|
|
|
/// This test ensures the book will continue to pass because it breaks the
|
|
|
|
/// `SUMMARY.md` up using level 2 headers ([example]).
|
|
|
|
///
|
|
|
|
/// [example]: https://github.com/rust-lang/book/blob/2c942dc094f4ddcdc7aba7564f80782801197c99/second-edition/src/SUMMARY.md#basic-rust-literacy
|
|
|
|
#[test]
|
|
|
|
fn can_have_a_subheader_between_nested_items() {
|
|
|
|
let src = "- [First](./first.md)\n\n## Subheading\n\n- [Second](./second.md)\n";
|
|
|
|
let should_be = vec![
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
number: Some(SectionNumber(vec![1])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("Second"),
|
|
|
|
location: PathBuf::from("./second.md"),
|
|
|
|
number: Some(SectionNumber(vec![2])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let _ = parser.stream.next();
|
|
|
|
|
|
|
|
let got = parser.parse_numbered().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
2018-01-22 20:47:29 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn an_empty_link_location_is_an_error() {
|
2020-03-25 06:52:24 +08:00
|
|
|
let src = "- [Empty]()\n";
|
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
parser.stream.next();
|
2018-01-22 20:47:29 +08:00
|
|
|
|
2020-03-25 06:52:24 +08:00
|
|
|
let got = parser.parse_numbered();
|
|
|
|
assert!(got.is_err());
|
2018-01-22 20:47:29 +08:00
|
|
|
}
|
2018-09-19 23:33:28 +08:00
|
|
|
|
2019-10-29 21:04:16 +08:00
|
|
|
/// Regression test for https://github.com/rust-lang/mdBook/issues/779
|
2018-09-19 23:33:28 +08:00
|
|
|
/// Ensure section numbers are correctly incremented after a horizontal separator.
|
|
|
|
#[test]
|
|
|
|
fn keep_numbering_after_separator() {
|
2018-12-04 07:10:09 +08:00
|
|
|
let src =
|
|
|
|
"- [First](./first.md)\n---\n- [Second](./second.md)\n---\n- [Third](./third.md)\n";
|
2018-09-19 23:33:28 +08:00
|
|
|
let should_be = vec![
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("First"),
|
|
|
|
location: PathBuf::from("./first.md"),
|
|
|
|
number: Some(SectionNumber(vec![1])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
SummaryItem::Separator,
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("Second"),
|
|
|
|
location: PathBuf::from("./second.md"),
|
|
|
|
number: Some(SectionNumber(vec![2])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
SummaryItem::Separator,
|
|
|
|
SummaryItem::Link(Link {
|
|
|
|
name: String::from("Third"),
|
|
|
|
location: PathBuf::from("./third.md"),
|
|
|
|
number: Some(SectionNumber(vec![3])),
|
|
|
|
nested_items: Vec::new(),
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut parser = SummaryParser::new(src);
|
|
|
|
let _ = parser.stream.next();
|
|
|
|
|
|
|
|
let got = parser.parse_numbered().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
2018-01-22 20:47:29 +08:00
|
|
|
}
|