2023-02-04 14:07:20 -05:00
|
|
|
//! A pull parser for [Djot](https://djot.net).
|
|
|
|
//!
|
|
|
|
//! The main entry is through [`Parser`] which implements an [`Iterator`] of [`Event`]s. The events
|
|
|
|
//! can then be used to traverse the document structure in order to e.g. construct an AST or
|
|
|
|
//! directly generate to some output format. This crate provides an [`html`] module that can be
|
|
|
|
//! used to render the events to HTML.
|
|
|
|
//!
|
2023-02-05 14:07:20 -05:00
|
|
|
//! # Feature flags
|
|
|
|
//!
|
|
|
|
//! - `html` (default): build the html module and a binary that converts djot to HTML.
|
|
|
|
//!
|
2023-02-04 14:07:20 -05:00
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! Generate HTML from Djot input:
|
|
|
|
//!
|
|
|
|
//! ```
|
2023-02-05 14:07:20 -05:00
|
|
|
//! # #[cfg(feature = "html")]
|
|
|
|
//! # {
|
2023-02-10 03:46:18 -05:00
|
|
|
//! use jotdown::Render;
|
2023-02-04 14:07:20 -05:00
|
|
|
//! let djot_input = "hello *world*!";
|
|
|
|
//! let events = jotdown::Parser::new(djot_input);
|
|
|
|
//! let mut html = String::new();
|
2023-02-10 03:46:18 -05:00
|
|
|
//! jotdown::html::Renderer.push(events, &mut html);
|
2023-02-04 14:07:20 -05:00
|
|
|
//! assert_eq!(html, "<p>hello <strong>world</strong>!</p>\n");
|
2023-02-05 14:07:20 -05:00
|
|
|
//! # }
|
2023-02-04 14:07:20 -05:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Apply some filter to a specific type of element:
|
|
|
|
//!
|
|
|
|
//! ```
|
2023-02-05 14:07:20 -05:00
|
|
|
//! # #[cfg(feature = "html")]
|
|
|
|
//! # {
|
2023-02-04 14:07:20 -05:00
|
|
|
//! # use jotdown::Event;
|
|
|
|
//! # use jotdown::Container::Link;
|
2023-02-10 03:46:18 -05:00
|
|
|
//! # use jotdown::Render;
|
2023-02-04 14:07:20 -05:00
|
|
|
//! let events =
|
|
|
|
//! jotdown::Parser::new("a [link](https://example.com)").map(|e| match e {
|
|
|
|
//! Event::Start(Link(dst, ty), attrs) => {
|
|
|
|
//! Event::Start(Link(dst.replace(".com", ".net").into(), ty), attrs)
|
|
|
|
//! }
|
|
|
|
//! e => e,
|
|
|
|
//! });
|
|
|
|
//! let mut html = String::new();
|
2023-02-10 03:46:18 -05:00
|
|
|
//! jotdown::html::Renderer.push(events, &mut html);
|
2023-02-04 14:07:20 -05:00
|
|
|
//! assert_eq!(html, "<p>a <a href=\"https://example.net\">link</a></p>\n");
|
2023-02-05 14:07:20 -05:00
|
|
|
//! # }
|
2023-02-04 14:07:20 -05:00
|
|
|
//! ```
|
|
|
|
|
2023-02-10 03:45:43 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
use std::io;
|
2023-01-29 09:10:01 -05:00
|
|
|
|
2023-02-05 14:07:20 -05:00
|
|
|
#[cfg(feature = "html")]
|
2022-11-29 12:34:13 -05:00
|
|
|
pub mod html;
|
|
|
|
|
2022-12-18 12:05:39 -05:00
|
|
|
mod attr;
|
2022-11-12 12:45:17 -05:00
|
|
|
mod block;
|
2022-11-16 16:11:55 -05:00
|
|
|
mod inline;
|
2022-11-20 13:13:48 -05:00
|
|
|
mod lex;
|
2022-11-12 12:45:17 -05:00
|
|
|
mod span;
|
|
|
|
mod tree;
|
|
|
|
|
2023-01-15 10:12:05 -05:00
|
|
|
use span::DiscontinuousString;
|
2022-11-28 14:12:49 -05:00
|
|
|
use span::Span;
|
|
|
|
|
2022-12-18 12:05:39 -05:00
|
|
|
pub use attr::Attributes;
|
|
|
|
|
2022-12-13 15:19:16 -05:00
|
|
|
type CowStr<'s> = std::borrow::Cow<'s, str>;
|
|
|
|
|
2023-02-10 03:45:43 -05:00
|
|
|
pub trait Render {
|
|
|
|
/// Push [`Event`]s to a unicode-accepting buffer or stream.
|
|
|
|
fn push<'s, I: Iterator<Item = Event<'s>>, W: fmt::Write>(
|
|
|
|
&self,
|
|
|
|
events: I,
|
|
|
|
out: W,
|
|
|
|
) -> fmt::Result;
|
|
|
|
|
|
|
|
/// Write [`Event`]s to a byte sink, encoded as UTF-8.
|
|
|
|
///
|
|
|
|
/// NOTE: This performs many small writes, so IO writes should be buffered with e.g.
|
|
|
|
/// [`std::io::BufWriter`].
|
|
|
|
fn write<'s, I: Iterator<Item = Event<'s>>, W: io::Write>(
|
|
|
|
&self,
|
|
|
|
events: I,
|
|
|
|
out: W,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
struct Adapter<T: io::Write> {
|
|
|
|
inner: T,
|
|
|
|
error: io::Result<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: io::Write> fmt::Write for Adapter<T> {
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
match self.inner.write_all(s.as_bytes()) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(e) => {
|
|
|
|
self.error = Err(e);
|
|
|
|
Err(fmt::Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut out = Adapter {
|
|
|
|
inner: out,
|
|
|
|
error: Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.push(events, &mut out) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => match out.error {
|
|
|
|
Err(_) => out.error,
|
|
|
|
_ => Err(io::Error::new(io::ErrorKind::Other, "formatter error")),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// A Djot event.
|
|
|
|
///
|
|
|
|
/// A Djot document is represented by a sequence of events. An element may consist of one or
|
|
|
|
/// multiple events. [`Container`] elements are represented by a [`Event::Start`] followed by
|
|
|
|
/// events representing its content, and finally a [`Event::End`]. Atomic elements without any
|
|
|
|
/// inside elements are represented by a single event.
|
2022-11-28 14:12:49 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2022-11-28 18:33:43 -05:00
|
|
|
pub enum Event<'s> {
|
2022-11-29 12:34:13 -05:00
|
|
|
/// Start of a container.
|
|
|
|
Start(Container<'s>, Attributes<'s>),
|
|
|
|
/// End of a container.
|
|
|
|
End(Container<'s>),
|
2022-11-28 15:52:09 -05:00
|
|
|
/// A string object, text only.
|
2022-12-13 15:19:16 -05:00
|
|
|
Str(CowStr<'s>),
|
2023-02-04 15:59:01 -05:00
|
|
|
/// A footnote reference.
|
|
|
|
FootnoteReference(&'s str, usize),
|
|
|
|
/// A symbol, by default rendered literally but may be treated specially.
|
|
|
|
Symbol(CowStr<'s>),
|
|
|
|
/// Left single quotation mark.
|
|
|
|
LeftSingleQuote,
|
|
|
|
/// Right double quotation mark.
|
|
|
|
RightSingleQuote,
|
|
|
|
/// Left single quotation mark.
|
|
|
|
LeftDoubleQuote,
|
|
|
|
/// Right double quotation mark.
|
|
|
|
RightDoubleQuote,
|
|
|
|
/// A horizontal ellipsis, i.e. a set of three periods.
|
|
|
|
Ellipsis,
|
|
|
|
/// An en dash.
|
|
|
|
EnDash,
|
|
|
|
/// An em dash.
|
|
|
|
EmDash,
|
|
|
|
/// A space that must not break a line.
|
|
|
|
NonBreakingSpace,
|
|
|
|
/// A newline that may or may not break a line in the output.
|
|
|
|
Softbreak,
|
|
|
|
/// A newline that must break a line in the output.
|
|
|
|
Hardbreak,
|
|
|
|
/// An escape character, not visible in output.
|
|
|
|
Escape,
|
|
|
|
/// A blank line, not visible in output.
|
|
|
|
Blankline,
|
2023-02-01 16:32:22 -05:00
|
|
|
/// A thematic break, typically a horizontal rule.
|
|
|
|
ThematicBreak(Attributes<'s>),
|
2022-11-27 15:59:54 -05:00
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// A container that may contain other elements.
|
|
|
|
///
|
|
|
|
/// There are three types of containers:
|
|
|
|
///
|
|
|
|
/// - inline, may only contain inline elements,
|
|
|
|
/// - block leaf, may only contain inline elements,
|
|
|
|
/// - block container, may contain any block-level elements.
|
2022-11-28 14:12:49 -05:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2022-11-29 12:34:13 -05:00
|
|
|
pub enum Container<'s> {
|
|
|
|
/// A blockquote element.
|
|
|
|
Blockquote,
|
|
|
|
/// A list.
|
2023-01-22 06:39:04 -05:00
|
|
|
List { kind: ListKind, tight: bool },
|
2022-11-29 12:34:13 -05:00
|
|
|
/// An item of a list
|
|
|
|
ListItem,
|
2023-01-21 15:21:43 -05:00
|
|
|
/// An item of a task list, either checked or unchecked.
|
|
|
|
TaskListItem { checked: bool },
|
2023-01-22 04:49:24 -05:00
|
|
|
/// A description list element.
|
|
|
|
DescriptionList,
|
2022-11-29 12:34:13 -05:00
|
|
|
/// Details describing a term within a description list.
|
|
|
|
DescriptionDetails,
|
|
|
|
/// A footnote definition.
|
2023-01-18 16:30:24 -05:00
|
|
|
Footnote { tag: &'s str, number: usize },
|
2022-11-29 12:34:13 -05:00
|
|
|
/// A table element.
|
|
|
|
Table,
|
|
|
|
/// A row element of a table.
|
2023-01-25 13:27:12 -05:00
|
|
|
TableRow { head: bool },
|
2023-01-28 12:12:45 -05:00
|
|
|
/// A section belonging to a top level heading.
|
2023-01-29 09:10:01 -05:00
|
|
|
Section { id: CowStr<'s> },
|
2022-11-29 12:34:13 -05:00
|
|
|
/// A block-level divider element.
|
2022-12-07 13:32:42 -05:00
|
|
|
Div { class: Option<&'s str> },
|
2022-11-28 15:52:09 -05:00
|
|
|
/// A paragraph.
|
2022-11-27 15:59:54 -05:00
|
|
|
Paragraph,
|
2022-11-28 15:52:09 -05:00
|
|
|
/// A heading.
|
2023-01-29 09:10:01 -05:00
|
|
|
Heading {
|
|
|
|
level: u16,
|
|
|
|
has_section: bool,
|
|
|
|
id: CowStr<'s>,
|
|
|
|
},
|
2022-11-28 15:52:09 -05:00
|
|
|
/// A cell element of row within a table.
|
2023-01-25 13:27:12 -05:00
|
|
|
TableCell { alignment: Alignment, head: bool },
|
2023-01-26 14:16:20 -05:00
|
|
|
/// A caption within a table.
|
|
|
|
Caption,
|
2022-12-01 14:34:23 -05:00
|
|
|
/// A term within a description list.
|
|
|
|
DescriptionTerm,
|
2022-11-28 15:52:09 -05:00
|
|
|
/// A block with raw markup for a specific output format.
|
2022-11-27 15:59:54 -05:00
|
|
|
RawBlock { format: &'s str },
|
2022-11-28 15:52:09 -05:00
|
|
|
/// A block with code in a specific language.
|
2022-12-04 11:56:49 -05:00
|
|
|
CodeBlock { lang: Option<&'s str> },
|
2022-12-01 14:46:53 -05:00
|
|
|
/// An inline divider element.
|
|
|
|
Span,
|
|
|
|
/// An inline link with a destination URL.
|
2022-12-13 15:19:16 -05:00
|
|
|
Link(CowStr<'s>, LinkType),
|
2022-12-18 01:59:11 -05:00
|
|
|
/// An inline image with a source URL. Inner Str objects compose the alternative text.
|
2022-12-17 12:03:06 -05:00
|
|
|
Image(CowStr<'s>, SpanLinkType),
|
2022-12-08 11:42:54 -05:00
|
|
|
/// An inline verbatim string.
|
|
|
|
Verbatim,
|
|
|
|
/// An inline or display math element.
|
|
|
|
Math { display: bool },
|
|
|
|
/// Inline raw markup for a specific output format.
|
|
|
|
RawInline { format: &'s str },
|
2022-11-28 18:33:43 -05:00
|
|
|
/// A subscripted element.
|
|
|
|
Subscript,
|
|
|
|
/// A superscripted element.
|
|
|
|
Superscript,
|
2022-12-01 14:46:53 -05:00
|
|
|
/// An inserted inline element.
|
2022-11-28 18:33:43 -05:00
|
|
|
Insert,
|
2022-12-01 14:46:53 -05:00
|
|
|
/// A deleted inline element.
|
2022-11-28 18:33:43 -05:00
|
|
|
Delete,
|
2022-12-01 14:46:53 -05:00
|
|
|
/// An inline element emphasized with a bold typeface.
|
2022-11-28 18:33:43 -05:00
|
|
|
Strong,
|
2022-12-01 14:46:53 -05:00
|
|
|
/// An emphasized inline element.
|
2022-11-28 18:33:43 -05:00
|
|
|
Emphasis,
|
|
|
|
/// A highlighted inline element.
|
|
|
|
Mark,
|
2022-11-27 15:59:54 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 13:56:08 -05:00
|
|
|
impl<'s> Container<'s> {
|
|
|
|
/// Is a block element.
|
2023-02-11 18:57:11 -05:00
|
|
|
#[must_use]
|
|
|
|
pub fn is_block(&self) -> bool {
|
2022-11-30 13:56:08 -05:00
|
|
|
match self {
|
|
|
|
Self::Blockquote
|
2023-01-22 06:39:04 -05:00
|
|
|
| Self::List { .. }
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::ListItem
|
2023-01-21 15:21:43 -05:00
|
|
|
| Self::TaskListItem { .. }
|
2023-01-22 04:49:24 -05:00
|
|
|
| Self::DescriptionList
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::DescriptionDetails
|
|
|
|
| Self::Footnote { .. }
|
|
|
|
| Self::Table
|
2023-01-25 13:27:12 -05:00
|
|
|
| Self::TableRow { .. }
|
2023-01-29 09:10:01 -05:00
|
|
|
| Self::Section { .. }
|
2022-12-07 13:32:42 -05:00
|
|
|
| Self::Div { .. }
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::Paragraph
|
|
|
|
| Self::Heading { .. }
|
2023-01-25 13:27:12 -05:00
|
|
|
| Self::TableCell { .. }
|
2023-01-26 14:16:20 -05:00
|
|
|
| Self::Caption
|
2022-12-01 14:34:23 -05:00
|
|
|
| Self::DescriptionTerm
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::RawBlock { .. }
|
|
|
|
| Self::CodeBlock { .. } => true,
|
|
|
|
Self::Span
|
|
|
|
| Self::Link(..)
|
|
|
|
| Self::Image(..)
|
2022-12-08 11:42:54 -05:00
|
|
|
| Self::Verbatim
|
|
|
|
| Self::Math { .. }
|
|
|
|
| Self::RawInline { .. }
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::Subscript
|
|
|
|
| Self::Superscript
|
|
|
|
| Self::Insert
|
|
|
|
| Self::Delete
|
|
|
|
| Self::Strong
|
|
|
|
| Self::Emphasis
|
2023-01-27 13:04:01 -05:00
|
|
|
| Self::Mark => false,
|
2022-11-30 13:56:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is a block element that may contain children blocks.
|
2023-02-11 18:57:11 -05:00
|
|
|
#[must_use]
|
|
|
|
pub fn is_block_container(&self) -> bool {
|
2022-11-30 13:56:08 -05:00
|
|
|
match self {
|
|
|
|
Self::Blockquote
|
2023-01-22 06:39:04 -05:00
|
|
|
| Self::List { .. }
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::ListItem
|
2023-01-21 15:21:43 -05:00
|
|
|
| Self::TaskListItem { .. }
|
2023-01-22 04:49:24 -05:00
|
|
|
| Self::DescriptionList
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::DescriptionDetails
|
|
|
|
| Self::Footnote { .. }
|
|
|
|
| Self::Table
|
2023-01-25 13:27:12 -05:00
|
|
|
| Self::TableRow { .. }
|
2023-01-29 09:10:01 -05:00
|
|
|
| Self::Section { .. }
|
2022-12-07 13:32:42 -05:00
|
|
|
| Self::Div { .. } => true,
|
2022-11-30 13:56:08 -05:00
|
|
|
Self::Paragraph
|
|
|
|
| Self::Heading { .. }
|
2023-01-25 13:27:12 -05:00
|
|
|
| Self::TableCell { .. }
|
2023-01-26 14:16:20 -05:00
|
|
|
| Self::Caption
|
2022-12-01 14:34:23 -05:00
|
|
|
| Self::DescriptionTerm
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::RawBlock { .. }
|
|
|
|
| Self::CodeBlock { .. }
|
|
|
|
| Self::Span
|
|
|
|
| Self::Link(..)
|
|
|
|
| Self::Image(..)
|
2022-12-08 11:42:54 -05:00
|
|
|
| Self::Verbatim
|
|
|
|
| Self::Math { .. }
|
|
|
|
| Self::RawInline { .. }
|
2022-11-30 13:56:08 -05:00
|
|
|
| Self::Subscript
|
|
|
|
| Self::Superscript
|
|
|
|
| Self::Insert
|
|
|
|
| Self::Delete
|
|
|
|
| Self::Strong
|
|
|
|
| Self::Emphasis
|
2023-01-27 13:04:01 -05:00
|
|
|
| Self::Mark => false,
|
2022-11-30 13:56:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// Alignment of a table column.
|
2023-01-25 13:27:12 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum Alignment {
|
|
|
|
Unspecified,
|
|
|
|
Left,
|
|
|
|
Center,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// The type of an inline span link.
|
2023-02-04 14:10:56 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-12-17 12:03:06 -05:00
|
|
|
pub enum SpanLinkType {
|
2023-02-04 14:07:20 -05:00
|
|
|
/// E.g. `[text](url)`
|
2022-11-28 15:52:09 -05:00
|
|
|
Inline,
|
2023-02-04 14:07:20 -05:00
|
|
|
/// In the form `[text][tag]` or `[tag][]`.
|
2022-11-28 15:52:09 -05:00
|
|
|
Reference,
|
2022-12-17 12:03:06 -05:00
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// The type of an inline link.
|
2023-02-04 14:10:56 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-12-17 12:03:06 -05:00
|
|
|
pub enum LinkType {
|
2023-02-04 14:07:20 -05:00
|
|
|
/// E.g. `[text](url)`.
|
2022-12-17 12:03:06 -05:00
|
|
|
Span(SpanLinkType),
|
2023-02-04 14:07:20 -05:00
|
|
|
/// In the form `<url>`.
|
2022-12-11 03:26:55 -05:00
|
|
|
AutoLink,
|
2023-02-04 14:07:20 -05:00
|
|
|
/// In the form `<address>`.
|
2022-11-28 15:52:09 -05:00
|
|
|
Email,
|
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// The type of a list.
|
2022-11-28 15:52:09 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2023-01-22 06:39:04 -05:00
|
|
|
pub enum ListKind {
|
2023-02-04 14:07:20 -05:00
|
|
|
/// A bullet list.
|
2022-11-28 15:52:09 -05:00
|
|
|
Unordered,
|
2023-02-04 14:07:20 -05:00
|
|
|
/// An enumerated list.
|
2023-01-21 05:14:00 -05:00
|
|
|
Ordered {
|
2023-01-21 06:33:41 -05:00
|
|
|
numbering: OrderedListNumbering,
|
|
|
|
style: OrderedListStyle,
|
2023-01-31 13:58:00 -05:00
|
|
|
start: u64,
|
2023-01-21 05:14:00 -05:00
|
|
|
},
|
2023-02-04 14:07:20 -05:00
|
|
|
/// A task list.
|
2022-12-06 15:31:08 -05:00
|
|
|
Task,
|
2022-11-28 15:52:09 -05:00
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// Numbering type of an ordered list.
|
2022-11-28 15:52:09 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2023-01-21 05:13:24 -05:00
|
|
|
pub enum OrderedListNumbering {
|
2022-11-28 15:52:09 -05:00
|
|
|
/// Decimal numbering, e.g. `1)`.
|
|
|
|
Decimal,
|
|
|
|
/// Lowercase alphabetic numbering, e.g. `a)`.
|
|
|
|
AlphaLower,
|
|
|
|
/// Uppercase alphabetic numbering, e.g. `A)`.
|
|
|
|
AlphaUpper,
|
2023-01-22 04:49:40 -05:00
|
|
|
/// Lowercase roman or alphabetic numbering, e.g. `iv)`.
|
2022-11-28 15:52:09 -05:00
|
|
|
RomanLower,
|
2023-01-22 04:49:40 -05:00
|
|
|
/// Uppercase roman or alphabetic numbering, e.g. `IV)`.
|
2022-11-28 15:52:09 -05:00
|
|
|
RomanUpper,
|
|
|
|
}
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// Style of an ordered list.
|
2022-11-28 15:52:09 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-12-01 14:46:53 -05:00
|
|
|
pub enum OrderedListStyle {
|
2022-11-28 15:52:09 -05:00
|
|
|
/// Number is followed by a period, e.g. `1.`.
|
|
|
|
Period,
|
|
|
|
/// Number is followed by a closing parenthesis, e.g. `1)`.
|
|
|
|
Paren,
|
|
|
|
/// Number is enclosed by parentheses, e.g. `(1)`.
|
|
|
|
ParenParen,
|
|
|
|
}
|
|
|
|
|
2023-01-21 15:21:43 -05:00
|
|
|
impl OrderedListNumbering {
|
2023-01-31 13:58:00 -05:00
|
|
|
fn parse_number(self, n: &str) -> u64 {
|
2023-01-21 15:21:43 -05:00
|
|
|
match self {
|
|
|
|
Self::Decimal => n.parse().unwrap(),
|
2023-01-23 15:41:04 -05:00
|
|
|
Self::AlphaLower | Self::AlphaUpper => {
|
2023-01-31 13:58:00 -05:00
|
|
|
let d0 = u64::from(if matches!(self, Self::AlphaLower) {
|
2023-01-23 15:41:04 -05:00
|
|
|
b'a'
|
|
|
|
} else {
|
|
|
|
b'A'
|
|
|
|
});
|
|
|
|
let weights = (1..=n.len()).scan(1, |a, _| {
|
|
|
|
let prev = *a;
|
|
|
|
*a *= 26;
|
|
|
|
Some(prev)
|
|
|
|
});
|
|
|
|
n.as_bytes()
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.copied()
|
2023-01-31 13:58:00 -05:00
|
|
|
.map(u64::from)
|
2023-01-23 15:41:04 -05:00
|
|
|
.zip(weights)
|
|
|
|
.map(|(d, w)| w * (d - d0 + 1))
|
|
|
|
.sum()
|
|
|
|
}
|
2023-01-23 16:22:23 -05:00
|
|
|
Self::RomanLower | Self::RomanUpper => {
|
2023-01-31 13:58:00 -05:00
|
|
|
fn value(d: char) -> u64 {
|
2023-01-23 16:22:23 -05:00
|
|
|
match d {
|
|
|
|
'i' | 'I' => 1,
|
|
|
|
'v' | 'V' => 5,
|
|
|
|
'x' | 'X' => 10,
|
|
|
|
'l' | 'L' => 50,
|
|
|
|
'c' | 'C' => 100,
|
|
|
|
'd' | 'D' => 500,
|
|
|
|
'm' | 'M' => 1000,
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut prev = 0;
|
|
|
|
let mut sum = 0;
|
|
|
|
for d in n.chars().rev() {
|
|
|
|
let v = value(d);
|
|
|
|
if v < prev {
|
|
|
|
sum -= v;
|
|
|
|
} else {
|
|
|
|
sum += v;
|
|
|
|
}
|
|
|
|
prev = v;
|
|
|
|
}
|
|
|
|
sum
|
|
|
|
}
|
2023-01-21 15:21:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OrderedListStyle {
|
|
|
|
fn number(self, marker: &str) -> &str {
|
|
|
|
&marker[usize::from(matches!(self, Self::ParenParen))..marker.len() - 1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:06:57 -05:00
|
|
|
#[cfg(not(feature = "deterministic"))]
|
|
|
|
type Map<K, V> = std::collections::HashMap<K, V>;
|
|
|
|
#[cfg(feature = "deterministic")]
|
|
|
|
type Map<K, V> = std::collections::BTreeMap<K, V>;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "deterministic"))]
|
|
|
|
type Set<T> = std::collections::HashSet<T>;
|
|
|
|
#[cfg(feature = "deterministic")]
|
|
|
|
type Set<T> = std::collections::BTreeSet<T>;
|
|
|
|
|
2023-02-04 14:07:20 -05:00
|
|
|
/// A parser that generates [`Event`]s from a Djot document.
|
|
|
|
///
|
|
|
|
/// When created, it will perform an initial pass and build up a tree of the document's block
|
|
|
|
/// structure that will be kept for the duration of the parser's lifetime. Then, when the iterator
|
|
|
|
/// is advanced, the parser will start from the beginning of the document and parse inline elements
|
|
|
|
/// and emit [`Event`]s.
|
2022-11-28 14:19:22 -05:00
|
|
|
pub struct Parser<'s> {
|
2022-11-20 13:13:48 -05:00
|
|
|
src: &'s str,
|
2023-01-18 16:22:59 -05:00
|
|
|
|
2023-01-29 09:10:01 -05:00
|
|
|
/// Block tree parsed at first.
|
2023-01-21 07:36:21 -05:00
|
|
|
tree: block::Tree,
|
2023-01-29 09:10:01 -05:00
|
|
|
|
|
|
|
/// Contents obtained by the prepass.
|
|
|
|
pre_pass: PrePass<'s>,
|
|
|
|
|
2023-01-29 09:47:39 -05:00
|
|
|
/// Last parsed block attributes
|
|
|
|
block_attributes: Attributes<'s>,
|
2023-01-21 05:36:05 -05:00
|
|
|
|
2023-01-29 05:40:55 -05:00
|
|
|
/// Current table row is a head row.
|
2023-01-25 13:27:12 -05:00
|
|
|
table_head_row: bool,
|
|
|
|
|
2023-01-18 16:30:24 -05:00
|
|
|
/// Footnote references in the order they were encountered, without duplicates.
|
|
|
|
footnote_references: Vec<&'s str>,
|
|
|
|
/// Cache of footnotes to emit at the end.
|
2023-02-02 10:06:57 -05:00
|
|
|
footnotes: Map<&'s str, block::Tree>,
|
2023-01-18 16:30:24 -05:00
|
|
|
/// Next or current footnote being parsed and emitted.
|
|
|
|
footnote_index: usize,
|
|
|
|
/// Currently within a footnote.
|
|
|
|
footnote_active: bool,
|
2023-01-29 09:10:01 -05:00
|
|
|
|
|
|
|
/// Spans to the inlines in the leaf block currently being parsed.
|
|
|
|
inlines: span::InlineSpans<'s>,
|
|
|
|
/// Inline parser, recreated for each new inline.
|
|
|
|
inline_parser: Option<inline::Parser<span::InlineCharsIter<'s>>>,
|
2022-11-20 13:13:48 -05:00
|
|
|
}
|
|
|
|
|
2023-01-29 09:10:01 -05:00
|
|
|
struct Heading {
|
|
|
|
/// Location of heading in src.
|
|
|
|
location: usize,
|
|
|
|
/// Automatically generated id from heading text.
|
|
|
|
id_auto: String,
|
|
|
|
/// Overriding id from an explicit attribute on the heading.
|
|
|
|
id_override: Option<String>,
|
|
|
|
}
|
2023-01-16 17:22:44 -05:00
|
|
|
|
2023-01-29 09:10:01 -05:00
|
|
|
/// Because of potential future references, an initial pass is required to obtain all definitions.
|
|
|
|
struct PrePass<'s> {
|
|
|
|
/// Link definitions and their attributes.
|
2023-02-02 10:06:57 -05:00
|
|
|
link_definitions: Map<&'s str, (CowStr<'s>, attr::Attributes<'s>)>,
|
2023-01-29 09:10:01 -05:00
|
|
|
/// Cache of all heading ids.
|
|
|
|
headings: Vec<Heading>,
|
|
|
|
/// Indices to headings sorted lexicographically.
|
|
|
|
headings_lex: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> PrePass<'s> {
|
|
|
|
#[must_use]
|
|
|
|
fn new(src: &'s str, mut tree: block::Tree) -> Self {
|
2023-02-02 10:06:57 -05:00
|
|
|
let mut link_definitions = Map::new();
|
2023-01-29 09:10:01 -05:00
|
|
|
let mut headings: Vec<Heading> = Vec::new();
|
2023-02-02 10:06:57 -05:00
|
|
|
let mut used_ids: Set<&str> = Set::new();
|
2023-01-29 09:10:01 -05:00
|
|
|
|
|
|
|
let mut inlines = span::InlineSpans::new(src);
|
|
|
|
|
|
|
|
let mut attr_prev: Option<Span> = None;
|
|
|
|
while let Some(e) = tree.next() {
|
|
|
|
match e.kind {
|
|
|
|
tree::EventKind::Enter(block::Node::Leaf(block::Leaf::LinkDefinition)) => {
|
|
|
|
// All link definition tags have to be obtained initially, as references can
|
|
|
|
// appear before the definition.
|
2023-01-16 17:22:44 -05:00
|
|
|
let tag = e.span.of(src);
|
2023-01-28 10:03:01 -05:00
|
|
|
let attrs =
|
|
|
|
attr_prev.map_or_else(Attributes::new, |sp| attr::parse(sp.of(src)));
|
2023-01-29 09:10:01 -05:00
|
|
|
let url = match tree.count_children() {
|
2023-01-18 15:44:58 -05:00
|
|
|
0 => "".into(),
|
2023-01-29 09:10:01 -05:00
|
|
|
1 => tree.take_inlines().next().unwrap().of(src).trim().into(),
|
|
|
|
_ => tree.take_inlines().map(|sp| sp.of(src).trim()).collect(),
|
2023-01-18 15:44:58 -05:00
|
|
|
};
|
2023-01-29 09:10:01 -05:00
|
|
|
link_definitions.insert(tag, (url, attrs));
|
|
|
|
}
|
|
|
|
tree::EventKind::Enter(block::Node::Leaf(block::Leaf::Heading { .. })) => {
|
|
|
|
// All headings ids have to be obtained initially, as references can appear
|
|
|
|
// before the heading. Additionally, determining the id requires inline parsing
|
|
|
|
// as formatting must be removed.
|
|
|
|
//
|
|
|
|
// We choose to parse all headers twice instead of caching them.
|
|
|
|
let attrs = attr_prev.map(|sp| attr::parse(sp.of(src)));
|
|
|
|
let id_override = attrs
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|attrs| attrs.get("id"))
|
|
|
|
.map(ToString::to_string);
|
|
|
|
|
|
|
|
inlines.set_spans(tree.take_inlines());
|
|
|
|
let mut id_auto = String::new();
|
2023-02-01 12:25:52 -05:00
|
|
|
let mut last_whitespace = true;
|
2023-01-29 09:10:01 -05:00
|
|
|
inline::Parser::new(inlines.chars()).for_each(|ev| match ev.kind {
|
|
|
|
inline::EventKind::Str => {
|
|
|
|
let mut chars = inlines.slice(ev.span).chars().peekable();
|
|
|
|
while let Some(c) = chars.next() {
|
|
|
|
if c.is_whitespace() {
|
|
|
|
while chars.peek().map_or(false, |c| c.is_whitespace()) {
|
|
|
|
chars.next();
|
|
|
|
}
|
2023-02-01 12:25:52 -05:00
|
|
|
if !last_whitespace {
|
|
|
|
last_whitespace = true;
|
2023-01-29 09:10:01 -05:00
|
|
|
id_auto.push('-');
|
|
|
|
}
|
|
|
|
} else if !c.is_ascii_punctuation() || matches!(c, '-' | '_') {
|
|
|
|
id_auto.push(c);
|
2023-02-01 12:25:52 -05:00
|
|
|
last_whitespace = false;
|
2023-01-29 09:10:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline::EventKind::Atom(inline::Atom::Softbreak) => {
|
|
|
|
id_auto.push('-');
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
});
|
|
|
|
id_auto.drain(id_auto.trim_end_matches('-').len()..);
|
|
|
|
|
|
|
|
// ensure id unique
|
2023-01-31 16:49:10 -05:00
|
|
|
if used_ids.contains::<str>(&id_auto) || id_auto.is_empty() {
|
2023-01-29 09:10:01 -05:00
|
|
|
if id_auto.is_empty() {
|
|
|
|
id_auto.push('s');
|
|
|
|
}
|
|
|
|
let mut num = 1;
|
|
|
|
id_auto.push('-');
|
|
|
|
let i_num = id_auto.len();
|
|
|
|
write!(id_auto, "{}", num).unwrap();
|
2023-01-31 16:49:10 -05:00
|
|
|
while used_ids.contains::<str>(&id_auto) {
|
2023-01-29 09:10:01 -05:00
|
|
|
num += 1;
|
|
|
|
id_auto.drain(i_num..);
|
|
|
|
write!(id_auto, "{}", num).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 16:49:10 -05:00
|
|
|
// SAFETY: used_ids is dropped before the id_auto strings in headings. even if
|
|
|
|
// the strings move due to headings reallocating, the string data on the heap
|
|
|
|
// will not move
|
|
|
|
used_ids.insert(unsafe {
|
|
|
|
std::mem::transmute::<&str, &'static str>(id_auto.as_ref())
|
|
|
|
});
|
2023-01-29 09:10:01 -05:00
|
|
|
headings.push(Heading {
|
|
|
|
location: e.span.start(),
|
|
|
|
id_auto,
|
|
|
|
id_override,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
tree::EventKind::Atom(block::Atom::Attributes) => {
|
2023-01-28 10:03:01 -05:00
|
|
|
attr_prev = Some(e.span);
|
2023-01-29 09:10:01 -05:00
|
|
|
}
|
|
|
|
tree::EventKind::Enter(..)
|
|
|
|
| tree::EventKind::Exit(block::Node::Container(block::Container::Section {
|
|
|
|
..
|
|
|
|
})) => {}
|
|
|
|
_ => {
|
2023-01-28 10:03:01 -05:00
|
|
|
attr_prev = None;
|
2023-01-16 17:22:44 -05:00
|
|
|
}
|
|
|
|
}
|
2023-01-29 09:10:01 -05:00
|
|
|
}
|
2023-01-16 17:22:44 -05:00
|
|
|
|
2023-01-29 09:10:01 -05:00
|
|
|
let mut headings_lex = (0..headings.len()).collect::<Vec<_>>();
|
|
|
|
headings_lex.sort_by_key(|i| &headings[*i].id_auto);
|
2023-01-18 15:33:55 -05:00
|
|
|
|
2022-11-28 14:19:22 -05:00
|
|
|
Self {
|
2023-01-18 16:23:58 -05:00
|
|
|
link_definitions,
|
2023-01-29 09:10:01 -05:00
|
|
|
headings,
|
|
|
|
headings_lex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn heading_id(&self, i: usize) -> &str {
|
|
|
|
let h = &self.headings[i];
|
|
|
|
h.id_override.as_ref().unwrap_or(&h.id_auto)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn heading_id_by_location(&self, location: usize) -> Option<&str> {
|
|
|
|
self.headings
|
|
|
|
.binary_search_by_key(&location, |h| h.location)
|
|
|
|
.ok()
|
|
|
|
.map(|i| self.heading_id(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn heading_id_by_tag(&self, tag: &str) -> Option<&str> {
|
|
|
|
self.headings_lex
|
|
|
|
.binary_search_by_key(&tag, |i| &self.headings[*i].id_auto)
|
|
|
|
.ok()
|
|
|
|
.map(|i| self.heading_id(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s> Parser<'s> {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(src: &'s str) -> Self {
|
|
|
|
let tree = block::parse(src);
|
|
|
|
let pre_pass = PrePass::new(src, tree.clone());
|
|
|
|
|
|
|
|
Self {
|
|
|
|
src,
|
|
|
|
tree,
|
|
|
|
pre_pass,
|
2023-01-29 09:47:39 -05:00
|
|
|
block_attributes: Attributes::new(),
|
2023-01-25 13:27:12 -05:00
|
|
|
table_head_row: false,
|
2023-01-18 16:30:24 -05:00
|
|
|
footnote_references: Vec::new(),
|
2023-02-02 10:06:57 -05:00
|
|
|
footnotes: Map::new(),
|
2023-01-18 16:30:24 -05:00
|
|
|
footnote_index: 0,
|
|
|
|
footnote_active: false,
|
2023-01-15 10:12:05 -05:00
|
|
|
inlines: span::InlineSpans::new(src),
|
2022-12-11 12:47:00 -05:00
|
|
|
inline_parser: None,
|
2022-11-28 14:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 09:47:28 -05:00
|
|
|
fn inline(&mut self) -> Option<Event<'s>> {
|
|
|
|
self.inline_parser.as_mut().and_then(|parser| {
|
|
|
|
let mut inline = parser.next();
|
|
|
|
|
|
|
|
let mut first_is_attr = false;
|
2023-01-28 10:03:01 -05:00
|
|
|
let mut attributes = inline.as_ref().map_or_else(Attributes::new, |inl| {
|
2023-01-15 09:47:28 -05:00
|
|
|
if let inline::EventKind::Attributes = inl.kind {
|
|
|
|
first_is_attr = true;
|
|
|
|
attr::parse(self.inlines.slice(inl.span))
|
2022-12-17 06:21:15 -05:00
|
|
|
} else {
|
2023-01-15 09:47:28 -05:00
|
|
|
Attributes::new()
|
2022-12-17 06:21:15 -05:00
|
|
|
}
|
2023-01-15 09:47:28 -05:00
|
|
|
});
|
2022-11-20 13:13:48 -05:00
|
|
|
|
2023-01-15 09:47:28 -05:00
|
|
|
if first_is_attr {
|
|
|
|
inline = parser.next();
|
2022-12-11 12:47:00 -05:00
|
|
|
}
|
2022-11-22 13:19:21 -05:00
|
|
|
|
2023-01-15 09:47:28 -05:00
|
|
|
inline.map(|inline| match inline.kind {
|
|
|
|
inline::EventKind::Enter(c) | inline::EventKind::Exit(c) => {
|
|
|
|
let t = match c {
|
|
|
|
inline::Container::Span => Container::Span,
|
|
|
|
inline::Container::Verbatim => Container::Verbatim,
|
|
|
|
inline::Container::InlineMath => Container::Math { display: false },
|
|
|
|
inline::Container::DisplayMath => Container::Math { display: true },
|
|
|
|
inline::Container::RawFormat => Container::RawInline {
|
|
|
|
format: match self.inlines.src(inline.span) {
|
|
|
|
CowStr::Owned(_) => panic!(),
|
|
|
|
CowStr::Borrowed(s) => s,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
inline::Container::Subscript => Container::Subscript,
|
|
|
|
inline::Container::Superscript => Container::Superscript,
|
|
|
|
inline::Container::Insert => Container::Insert,
|
|
|
|
inline::Container::Delete => Container::Delete,
|
|
|
|
inline::Container::Emphasis => Container::Emphasis,
|
|
|
|
inline::Container::Strong => Container::Strong,
|
|
|
|
inline::Container::Mark => Container::Mark,
|
|
|
|
inline::Container::InlineLink => Container::Link(
|
|
|
|
match self.inlines.src(inline.span) {
|
|
|
|
CowStr::Owned(s) => s.replace('\n', "").into(),
|
|
|
|
s @ CowStr::Borrowed(_) => s,
|
|
|
|
},
|
|
|
|
LinkType::Span(SpanLinkType::Inline),
|
|
|
|
),
|
|
|
|
inline::Container::InlineImage => Container::Image(
|
|
|
|
match self.inlines.src(inline.span) {
|
|
|
|
CowStr::Owned(s) => s.replace('\n', "").into(),
|
|
|
|
s @ CowStr::Borrowed(_) => s,
|
|
|
|
},
|
|
|
|
SpanLinkType::Inline,
|
|
|
|
),
|
2023-01-28 10:03:01 -05:00
|
|
|
inline::Container::ReferenceLink | inline::Container::ReferenceImage => {
|
2023-01-28 10:38:17 -05:00
|
|
|
let tag = match self.inlines.src(inline.span) {
|
|
|
|
CowStr::Owned(s) => s.replace('\n', " ").into(),
|
|
|
|
s @ CowStr::Borrowed(_) => s,
|
|
|
|
};
|
2023-01-29 09:10:01 -05:00
|
|
|
let link_def =
|
|
|
|
self.pre_pass.link_definitions.get(tag.as_ref()).cloned();
|
|
|
|
|
|
|
|
let url = if let Some((url, attrs_def)) = link_def {
|
|
|
|
attributes.union(attrs_def);
|
|
|
|
url
|
|
|
|
} else {
|
|
|
|
self.pre_pass
|
|
|
|
.heading_id_by_tag(tag.as_ref())
|
|
|
|
.map_or_else(|| "".into(), |id| format!("#{}", id).into())
|
|
|
|
};
|
|
|
|
|
2023-01-28 10:03:01 -05:00
|
|
|
if matches!(c, inline::Container::ReferenceLink) {
|
|
|
|
Container::Link(url, LinkType::Span(SpanLinkType::Reference))
|
|
|
|
} else {
|
|
|
|
Container::Image(url, SpanLinkType::Reference)
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 10:38:19 -05:00
|
|
|
inline::Container::Autolink => {
|
|
|
|
let url = self.inlines.src(inline.span);
|
|
|
|
let url = if url.contains('@') {
|
|
|
|
format!("mailto:{}", url).into()
|
|
|
|
} else {
|
|
|
|
url
|
|
|
|
};
|
|
|
|
Container::Link(url, LinkType::AutoLink)
|
|
|
|
}
|
2023-01-15 09:47:28 -05:00
|
|
|
};
|
|
|
|
if matches!(inline.kind, inline::EventKind::Enter(_)) {
|
|
|
|
Event::Start(t, attributes)
|
|
|
|
} else {
|
|
|
|
Event::End(t)
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 15:59:01 -05:00
|
|
|
inline::EventKind::Atom(a) => match a {
|
2023-01-18 16:30:24 -05:00
|
|
|
inline::Atom::FootnoteReference => {
|
|
|
|
let tag = match self.inlines.src(inline.span) {
|
|
|
|
CowStr::Borrowed(s) => s,
|
|
|
|
CowStr::Owned(..) => panic!(),
|
|
|
|
};
|
|
|
|
let number = self
|
|
|
|
.footnote_references
|
|
|
|
.iter()
|
|
|
|
.position(|t| *t == tag)
|
|
|
|
.map_or_else(
|
|
|
|
|| {
|
|
|
|
self.footnote_references.push(tag);
|
|
|
|
self.footnote_references.len()
|
|
|
|
},
|
|
|
|
|i| i + 1,
|
|
|
|
);
|
2023-02-04 15:59:01 -05:00
|
|
|
Event::FootnoteReference(
|
2023-01-18 16:30:24 -05:00
|
|
|
match self.inlines.src(inline.span) {
|
|
|
|
CowStr::Borrowed(s) => s,
|
|
|
|
CowStr::Owned(..) => panic!(),
|
|
|
|
},
|
|
|
|
number,
|
|
|
|
)
|
|
|
|
}
|
2023-02-04 15:59:01 -05:00
|
|
|
inline::Atom::Symbol => Event::Symbol(self.inlines.src(inline.span)),
|
2023-01-27 13:04:01 -05:00
|
|
|
inline::Atom::Quote { ty, left } => match (ty, left) {
|
2023-02-04 15:59:01 -05:00
|
|
|
(inline::QuoteType::Single, true) => Event::LeftSingleQuote,
|
|
|
|
(inline::QuoteType::Single, false) => Event::RightSingleQuote,
|
|
|
|
(inline::QuoteType::Double, true) => Event::LeftDoubleQuote,
|
|
|
|
(inline::QuoteType::Double, false) => Event::RightDoubleQuote,
|
2023-01-27 13:04:01 -05:00
|
|
|
},
|
2023-02-04 15:59:01 -05:00
|
|
|
inline::Atom::Ellipsis => Event::Ellipsis,
|
|
|
|
inline::Atom::EnDash => Event::EnDash,
|
|
|
|
inline::Atom::EmDash => Event::EmDash,
|
|
|
|
inline::Atom::Nbsp => Event::NonBreakingSpace,
|
|
|
|
inline::Atom::Softbreak => Event::Softbreak,
|
|
|
|
inline::Atom::Hardbreak => Event::Hardbreak,
|
|
|
|
inline::Atom::Escape => Event::Escape,
|
|
|
|
},
|
2023-01-15 09:47:28 -05:00
|
|
|
inline::EventKind::Str => Event::Str(self.inlines.src(inline.span)),
|
2023-01-15 17:48:55 -05:00
|
|
|
inline::EventKind::Whitespace
|
|
|
|
| inline::EventKind::Attributes
|
2023-01-16 11:25:09 -05:00
|
|
|
| inline::EventKind::Placeholder => {
|
2023-01-15 09:47:28 -05:00
|
|
|
panic!("{:?}", inline)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block(&mut self) -> Option<Event<'s>> {
|
2023-01-16 17:22:44 -05:00
|
|
|
while let Some(ev) = &mut self.tree.next() {
|
2022-12-07 13:32:42 -05:00
|
|
|
let content = ev.span.of(self.src);
|
|
|
|
let event = match ev.kind {
|
2022-12-10 04:26:06 -05:00
|
|
|
tree::EventKind::Atom(a) => match a {
|
2023-02-04 15:59:01 -05:00
|
|
|
block::Atom::Blankline => Event::Blankline,
|
2023-02-01 16:32:22 -05:00
|
|
|
block::Atom::ThematicBreak => {
|
|
|
|
Event::ThematicBreak(self.block_attributes.take())
|
|
|
|
}
|
2022-12-07 13:32:42 -05:00
|
|
|
block::Atom::Attributes => {
|
2023-01-29 09:47:39 -05:00
|
|
|
self.block_attributes.parse(content);
|
2022-12-07 13:32:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
2023-01-21 15:07:42 -05:00
|
|
|
tree::EventKind::Enter(c) | tree::EventKind::Exit(c) => {
|
|
|
|
let enter = matches!(ev.kind, tree::EventKind::Enter(..));
|
|
|
|
let cont = match c {
|
|
|
|
block::Node::Leaf(l) => {
|
|
|
|
if matches!(l, block::Leaf::LinkDefinition) {
|
|
|
|
// ignore link definitions
|
2023-01-28 05:05:16 -05:00
|
|
|
if enter {
|
|
|
|
self.tree.take_inlines().last();
|
|
|
|
}
|
2023-01-29 09:47:39 -05:00
|
|
|
self.block_attributes = Attributes::new();
|
2023-01-21 15:07:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-30 12:28:32 -05:00
|
|
|
if enter && !matches!(l, block::Leaf::CodeBlock) {
|
2023-01-21 15:07:42 -05:00
|
|
|
self.inlines.set_spans(self.tree.take_inlines());
|
|
|
|
self.inline_parser =
|
|
|
|
Some(inline::Parser::new(self.inlines.chars()));
|
|
|
|
}
|
2023-01-25 13:27:12 -05:00
|
|
|
match l {
|
|
|
|
block::Leaf::Paragraph => Container::Paragraph,
|
2023-01-29 09:10:01 -05:00
|
|
|
block::Leaf::Heading { has_section } => Container::Heading {
|
2023-01-29 07:34:10 -05:00
|
|
|
level: content.len().try_into().unwrap(),
|
2023-01-29 09:10:01 -05:00
|
|
|
has_section,
|
|
|
|
id: self
|
|
|
|
.pre_pass
|
|
|
|
.heading_id_by_location(ev.span.start())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string()
|
|
|
|
.into(),
|
2023-01-25 13:27:12 -05:00
|
|
|
},
|
2023-02-04 04:20:05 -05:00
|
|
|
block::Leaf::DescriptionTerm => Container::DescriptionTerm,
|
2023-01-25 13:27:12 -05:00
|
|
|
block::Leaf::CodeBlock => {
|
|
|
|
if let Some(format) = content.strip_prefix('=') {
|
|
|
|
Container::RawBlock { format }
|
|
|
|
} else {
|
|
|
|
Container::CodeBlock {
|
|
|
|
lang: (!content.is_empty()).then(|| content),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block::Leaf::TableCell(alignment) => Container::TableCell {
|
|
|
|
alignment,
|
|
|
|
head: self.table_head_row,
|
|
|
|
},
|
2023-01-26 14:16:20 -05:00
|
|
|
block::Leaf::Caption => Container::Caption,
|
2023-01-25 13:27:12 -05:00
|
|
|
block::Leaf::LinkDefinition => unreachable!(),
|
|
|
|
}
|
2023-01-21 15:07:42 -05:00
|
|
|
}
|
|
|
|
block::Node::Container(c) => match c {
|
2023-01-19 16:16:01 -05:00
|
|
|
block::Container::Blockquote => Container::Blockquote,
|
2022-12-12 12:22:13 -05:00
|
|
|
block::Container::Div { .. } => Container::Div {
|
|
|
|
class: (!ev.span.is_empty()).then(|| content),
|
|
|
|
},
|
2023-01-18 16:30:24 -05:00
|
|
|
block::Container::Footnote => {
|
2023-01-21 15:07:42 -05:00
|
|
|
assert!(enter);
|
2023-01-18 16:30:24 -05:00
|
|
|
self.footnotes.insert(content, self.tree.take_branch());
|
2023-01-29 09:47:39 -05:00
|
|
|
self.block_attributes = Attributes::new();
|
2023-01-18 16:30:24 -05:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-06 17:08:35 -05:00
|
|
|
block::Container::List(block::ListKind { ty, tight }) => {
|
2023-01-29 03:24:46 -05:00
|
|
|
if matches!(ty, block::ListType::Description) {
|
|
|
|
Container::DescriptionList
|
|
|
|
} else {
|
|
|
|
let kind = match ty {
|
|
|
|
block::ListType::Unordered(..) => ListKind::Unordered,
|
|
|
|
block::ListType::Task => ListKind::Task,
|
|
|
|
block::ListType::Ordered(numbering, style) => {
|
|
|
|
let start = numbering
|
|
|
|
.parse_number(style.number(content))
|
|
|
|
.max(1);
|
|
|
|
ListKind::Ordered {
|
|
|
|
numbering,
|
|
|
|
style,
|
|
|
|
start,
|
|
|
|
}
|
2023-01-22 06:39:04 -05:00
|
|
|
}
|
2023-01-29 03:24:46 -05:00
|
|
|
block::ListType::Description => unreachable!(),
|
|
|
|
};
|
|
|
|
Container::List { kind, tight }
|
|
|
|
}
|
2023-01-22 06:39:04 -05:00
|
|
|
}
|
2023-02-04 04:20:05 -05:00
|
|
|
block::Container::ListItem(ty) => match ty {
|
|
|
|
block::ListType::Task => Container::TaskListItem {
|
|
|
|
checked: content.as_bytes()[3] != b' ',
|
|
|
|
},
|
|
|
|
block::ListType::Description => Container::DescriptionDetails,
|
|
|
|
_ => Container::ListItem,
|
|
|
|
},
|
2023-01-25 13:27:12 -05:00
|
|
|
block::Container::Table => Container::Table,
|
|
|
|
block::Container::TableRow { head } => {
|
|
|
|
if enter {
|
|
|
|
self.table_head_row = head;
|
|
|
|
}
|
|
|
|
Container::TableRow { head }
|
|
|
|
}
|
2023-01-29 09:10:01 -05:00
|
|
|
block::Container::Section => Container::Section {
|
|
|
|
id: self
|
|
|
|
.pre_pass
|
|
|
|
.heading_id_by_location(ev.span.start())
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string()
|
|
|
|
.into(),
|
|
|
|
},
|
2023-01-21 15:07:42 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
if enter {
|
2023-01-29 09:47:39 -05:00
|
|
|
Event::Start(cont, self.block_attributes.take())
|
2023-01-21 15:07:42 -05:00
|
|
|
} else {
|
|
|
|
Event::End(cont)
|
2022-12-12 12:22:13 -05:00
|
|
|
}
|
2023-01-21 15:07:42 -05:00
|
|
|
}
|
2023-01-30 12:28:32 -05:00
|
|
|
tree::EventKind::Inline => Event::Str(content.into()), // verbatim
|
2022-12-07 13:32:42 -05:00
|
|
|
};
|
|
|
|
return Some(event);
|
|
|
|
}
|
|
|
|
None
|
2022-11-20 13:13:48 -05:00
|
|
|
}
|
2023-01-18 16:30:24 -05:00
|
|
|
|
|
|
|
fn footnote(&mut self) -> Option<Event<'s>> {
|
|
|
|
if self.footnote_active {
|
|
|
|
let tag = self.footnote_references.get(self.footnote_index).unwrap();
|
|
|
|
self.footnote_index += 1;
|
|
|
|
self.footnote_active = false;
|
|
|
|
Some(Event::End(Container::Footnote {
|
|
|
|
tag,
|
|
|
|
number: self.footnote_index,
|
|
|
|
}))
|
|
|
|
} else if let Some(tag) = self.footnote_references.get(self.footnote_index) {
|
|
|
|
self.tree = self
|
|
|
|
.footnotes
|
|
|
|
.remove(tag)
|
2023-01-21 07:36:21 -05:00
|
|
|
.unwrap_or_else(block::Tree::empty);
|
2023-01-18 16:30:24 -05:00
|
|
|
self.footnote_active = true;
|
|
|
|
|
|
|
|
Some(Event::Start(
|
|
|
|
Container::Footnote {
|
|
|
|
tag,
|
|
|
|
number: self.footnote_index + 1,
|
|
|
|
},
|
|
|
|
Attributes::new(),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 13:13:48 -05:00
|
|
|
}
|
2022-11-22 13:19:21 -05:00
|
|
|
|
2023-01-15 09:47:28 -05:00
|
|
|
impl<'s> Iterator for Parser<'s> {
|
|
|
|
type Item = Event<'s>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2023-01-18 16:30:24 -05:00
|
|
|
self.inline()
|
|
|
|
.or_else(|| self.block())
|
|
|
|
.or_else(|| self.footnote())
|
2023-01-15 09:47:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:19:21 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-11-28 18:33:43 -05:00
|
|
|
use super::Attributes;
|
2022-11-29 12:34:13 -05:00
|
|
|
use super::Container::*;
|
2022-11-22 13:19:21 -05:00
|
|
|
use super::Event::*;
|
2022-12-13 15:19:16 -05:00
|
|
|
use super::LinkType;
|
2023-01-22 06:39:04 -05:00
|
|
|
use super::ListKind;
|
2023-01-21 15:21:43 -05:00
|
|
|
use super::OrderedListNumbering::*;
|
|
|
|
use super::OrderedListStyle::*;
|
2022-12-17 12:03:06 -05:00
|
|
|
use super::SpanLinkType;
|
2022-11-22 13:19:21 -05:00
|
|
|
|
2022-11-22 13:48:17 -05:00
|
|
|
macro_rules! test_parse {
|
2022-12-13 15:19:16 -05:00
|
|
|
($src:expr $(,$($token:expr),* $(,)?)?) => {
|
2022-11-22 13:48:17 -05:00
|
|
|
#[allow(unused)]
|
2022-11-28 14:19:22 -05:00
|
|
|
let actual = super::Parser::new($src).collect::<Vec<_>>();
|
2022-11-22 13:48:17 -05:00
|
|
|
let expected = &[$($($token),*,)?];
|
2022-11-28 18:33:43 -05:00
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
concat!(
|
|
|
|
"\n",
|
|
|
|
"\x1b[0;1m====================== INPUT =========================\x1b[0m\n",
|
|
|
|
"\x1b[2m{}",
|
|
|
|
"\x1b[0;1m================ ACTUAL vs EXPECTED ==================\x1b[0m\n",
|
|
|
|
"{}",
|
|
|
|
"\x1b[0;1m======================================================\x1b[0m\n",
|
|
|
|
),
|
|
|
|
$src,
|
|
|
|
{
|
|
|
|
let a = actual.iter().map(|n| format!("{:?}", n)).collect::<Vec<_>>();
|
|
|
|
let b = expected.iter().map(|n| format!("{:?}", n)).collect::<Vec<_>>();
|
|
|
|
let max = a.len().max(b.len());
|
|
|
|
let a_width = a.iter().map(|a| a.len()).max().unwrap_or(0);
|
|
|
|
a.iter()
|
|
|
|
.map(AsRef::as_ref)
|
|
|
|
.chain(std::iter::repeat(""))
|
|
|
|
.zip(b.iter().map(AsRef::as_ref).chain(std::iter::repeat("")))
|
|
|
|
.take(max)
|
|
|
|
.map(|(a, b)|
|
|
|
|
format!(
|
|
|
|
"\x1b[{}m{:a_width$}\x1b[0m {}= \x1b[{}m{}\x1b[0m\n",
|
|
|
|
if a == b { "2" } else { "31" },
|
|
|
|
a,
|
|
|
|
if a == b { '=' } else { '!' },
|
|
|
|
if a == b { "2" } else { "32" },
|
|
|
|
b,
|
|
|
|
a_width = a_width,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.collect::<String>()
|
|
|
|
},
|
|
|
|
);
|
2022-11-22 13:48:17 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-17 06:21:15 -05:00
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
test_parse!("");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn heading() {
|
|
|
|
test_parse!(
|
|
|
|
"#\n",
|
2023-01-29 09:10:01 -05:00
|
|
|
Start(Section { id: "s-1".into() }, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "s-1".into()
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
End(Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "s-1".into()
|
|
|
|
}),
|
|
|
|
End(Section { id: "s-1".into() }),
|
2022-12-17 06:21:15 -05:00
|
|
|
);
|
|
|
|
test_parse!(
|
|
|
|
"# abc\ndef\n",
|
2023-01-29 09:10:01 -05:00
|
|
|
Start(
|
|
|
|
Section {
|
|
|
|
id: "abc-def".into()
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Start(
|
|
|
|
Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "abc-def".into()
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
2022-12-18 12:05:39 -05:00
|
|
|
Str("abc".into()),
|
2023-02-04 15:59:01 -05:00
|
|
|
Softbreak,
|
2022-12-18 12:05:39 -05:00
|
|
|
Str("def".into()),
|
2023-01-29 09:10:01 -05:00
|
|
|
End(Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "abc-def".into(),
|
|
|
|
}),
|
|
|
|
End(Section {
|
|
|
|
id: "abc-def".into()
|
|
|
|
}),
|
2022-12-17 06:21:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-29 09:47:39 -05:00
|
|
|
#[test]
|
|
|
|
fn heading_attr() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"# abc\n",
|
|
|
|
"{a=b}\n",
|
|
|
|
"# def\n", //
|
|
|
|
),
|
2023-01-29 09:10:01 -05:00
|
|
|
Start(Section { id: "abc".into() }, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "abc".into()
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
2023-01-29 09:47:39 -05:00
|
|
|
Str("abc".into()),
|
2023-01-29 09:10:01 -05:00
|
|
|
End(Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "abc".into(),
|
|
|
|
}),
|
|
|
|
End(Section { id: "abc".into() }),
|
|
|
|
Start(
|
|
|
|
Section { id: "def".into() },
|
|
|
|
[("a", "b")].into_iter().collect(),
|
|
|
|
),
|
|
|
|
Start(
|
|
|
|
Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "def".into()
|
|
|
|
},
|
|
|
|
Attributes::new(),
|
|
|
|
),
|
2023-01-29 09:47:39 -05:00
|
|
|
Str("def".into()),
|
2023-01-29 09:10:01 -05:00
|
|
|
End(Heading {
|
|
|
|
level: 1,
|
|
|
|
has_section: true,
|
|
|
|
id: "def".into(),
|
|
|
|
}),
|
|
|
|
End(Section { id: "def".into() }),
|
2023-01-29 09:47:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-17 06:21:15 -05:00
|
|
|
#[test]
|
|
|
|
fn blockquote() {
|
|
|
|
test_parse!(
|
|
|
|
">\n",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Blockquote, Attributes::new()),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2022-12-17 06:21:15 -05:00
|
|
|
End(Blockquote),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:19:21 -05:00
|
|
|
#[test]
|
2022-11-22 13:48:17 -05:00
|
|
|
fn para() {
|
|
|
|
test_parse!(
|
2022-11-26 19:12:56 -05:00
|
|
|
"para",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("para".into()),
|
2022-11-28 18:33:43 -05:00
|
|
|
End(Paragraph),
|
2022-11-26 19:12:56 -05:00
|
|
|
);
|
|
|
|
test_parse!(
|
|
|
|
"pa ra",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("pa ra".into()),
|
2022-11-28 18:33:43 -05:00
|
|
|
End(Paragraph),
|
2022-11-26 19:12:56 -05:00
|
|
|
);
|
|
|
|
test_parse!(
|
|
|
|
"para0\n\npara1",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("para0".into()),
|
2022-11-28 18:33:43 -05:00
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("para1".into()),
|
2022-11-28 18:33:43 -05:00
|
|
|
End(Paragraph),
|
2022-11-22 13:19:21 -05:00
|
|
|
);
|
|
|
|
}
|
2022-12-08 11:42:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verbatim() {
|
|
|
|
test_parse!(
|
|
|
|
"`abc\ndef",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(Verbatim, Attributes::new()),
|
|
|
|
Str("abc\ndef".into()),
|
2022-12-08 11:42:54 -05:00
|
|
|
End(Verbatim),
|
|
|
|
End(Paragraph),
|
|
|
|
);
|
2022-12-17 06:21:15 -05:00
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"> `abc\n",
|
|
|
|
"> def\n", //
|
|
|
|
),
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Blockquote, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(Verbatim, Attributes::new()),
|
|
|
|
Str("abc\ndef".into()),
|
2022-12-17 06:21:15 -05:00
|
|
|
End(Verbatim),
|
|
|
|
End(Paragraph),
|
|
|
|
End(Blockquote),
|
|
|
|
);
|
2022-12-08 11:42:54 -05:00
|
|
|
}
|
2022-12-11 04:45:05 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_inline() {
|
|
|
|
test_parse!(
|
2022-12-11 15:43:22 -05:00
|
|
|
"``raw\nraw``{=format}",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(RawInline { format: "format" }, Attributes::new()),
|
|
|
|
Str("raw\nraw".into()),
|
2022-12-11 04:45:05 -05:00
|
|
|
End(RawInline { format: "format" }),
|
|
|
|
End(Paragraph),
|
|
|
|
);
|
|
|
|
}
|
2022-12-13 15:19:16 -05:00
|
|
|
|
2023-01-15 15:56:48 -05:00
|
|
|
#[test]
|
|
|
|
fn raw_block() {
|
|
|
|
test_parse!(
|
|
|
|
"``` =html\n<table>\n```",
|
|
|
|
Start(RawBlock { format: "html" }, Attributes::new()),
|
2023-01-30 12:28:32 -05:00
|
|
|
Str("<table>\n".into()),
|
2023-01-15 15:56:48 -05:00
|
|
|
End(RawBlock { format: "html" }),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-04 11:10:38 -05:00
|
|
|
#[test]
|
|
|
|
fn symbol() {
|
|
|
|
test_parse!(
|
|
|
|
"abc :+1: def",
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("abc ".into()),
|
2023-02-04 15:59:01 -05:00
|
|
|
Symbol("+1".into()),
|
2023-02-04 11:10:38 -05:00
|
|
|
Str(" def".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-13 15:19:16 -05:00
|
|
|
#[test]
|
|
|
|
fn link_inline() {
|
|
|
|
test_parse!(
|
|
|
|
"[text](url)",
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
2022-12-13 15:19:16 -05:00
|
|
|
Start(
|
2022-12-18 12:05:39 -05:00
|
|
|
Link("url".into(), LinkType::Span(SpanLinkType::Inline)),
|
|
|
|
Attributes::new()
|
2022-12-13 15:19:16 -05:00
|
|
|
),
|
2022-12-18 12:05:39 -05:00
|
|
|
Str("text".into()),
|
|
|
|
End(Link("url".into(), LinkType::Span(SpanLinkType::Inline))),
|
2022-12-13 15:19:16 -05:00
|
|
|
End(Paragraph),
|
|
|
|
);
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"> [text](url\n",
|
|
|
|
"> url)\n", //
|
|
|
|
),
|
2022-12-18 12:05:39 -05:00
|
|
|
Start(Blockquote, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
2022-12-13 15:19:16 -05:00
|
|
|
Start(
|
2022-12-18 12:05:39 -05:00
|
|
|
Link("urlurl".into(), LinkType::Span(SpanLinkType::Inline)),
|
|
|
|
Attributes::new()
|
2022-12-13 15:19:16 -05:00
|
|
|
),
|
2022-12-18 12:05:39 -05:00
|
|
|
Str("text".into()),
|
|
|
|
End(Link("urlurl".into(), LinkType::Span(SpanLinkType::Inline))),
|
2022-12-13 15:19:16 -05:00
|
|
|
End(Paragraph),
|
2022-12-17 06:21:15 -05:00
|
|
|
End(Blockquote),
|
2022-12-13 15:19:16 -05:00
|
|
|
);
|
|
|
|
}
|
2022-12-18 12:05:39 -05:00
|
|
|
|
2023-01-16 17:22:44 -05:00
|
|
|
#[test]
|
|
|
|
fn link_reference() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"[text][tag]\n",
|
|
|
|
"\n",
|
|
|
|
"[tag]: url\n" //
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Str("text".into()),
|
|
|
|
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
|
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-16 17:22:44 -05:00
|
|
|
);
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"![text][tag]\n",
|
|
|
|
"\n",
|
|
|
|
"[tag]: url\n" //
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Image("url".into(), SpanLinkType::Reference),
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Str("text".into()),
|
|
|
|
End(Image("url".into(), SpanLinkType::Reference)),
|
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-16 17:22:44 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-17 12:05:34 -05:00
|
|
|
#[test]
|
|
|
|
fn link_reference_multiline() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"[text][tag]\n",
|
|
|
|
"\n",
|
|
|
|
"[tag]: u\n",
|
|
|
|
" rl\n", //
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Str("text".into()),
|
|
|
|
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
|
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-17 12:05:34 -05:00
|
|
|
);
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"[text][tag]\n",
|
|
|
|
"\n",
|
|
|
|
"[tag]:\n",
|
|
|
|
" url\n", //
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Str("text".into()),
|
|
|
|
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
|
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-17 12:05:34 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-28 10:03:01 -05:00
|
|
|
#[test]
|
|
|
|
fn link_reference_attrs() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"[text][tag]{b=c}\n",
|
|
|
|
"\n",
|
|
|
|
"{a=b}\n",
|
|
|
|
"[tag]: url\n",
|
|
|
|
"para\n",
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Start(
|
|
|
|
Link("url".into(), LinkType::Span(SpanLinkType::Reference)),
|
|
|
|
[("b", "c"), ("a", "b")].into_iter().collect(),
|
|
|
|
),
|
|
|
|
Str("text".into()),
|
|
|
|
End(Link("url".into(), LinkType::Span(SpanLinkType::Reference))),
|
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-28 10:03:01 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("para".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-18 16:30:24 -05:00
|
|
|
#[test]
|
|
|
|
fn footnote_references() {
|
|
|
|
test_parse!(
|
|
|
|
"[^a][^b][^c]",
|
|
|
|
Start(Paragraph, Attributes::new()),
|
2023-02-04 15:59:01 -05:00
|
|
|
FootnoteReference("a", 1),
|
|
|
|
FootnoteReference("b", 2),
|
|
|
|
FootnoteReference("c", 3),
|
2023-01-18 16:30:24 -05:00
|
|
|
End(Paragraph),
|
|
|
|
Start(
|
|
|
|
Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
End(Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
}),
|
|
|
|
Start(
|
|
|
|
Footnote {
|
|
|
|
tag: "b",
|
|
|
|
number: 2
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
End(Footnote {
|
|
|
|
tag: "b",
|
|
|
|
number: 2
|
|
|
|
}),
|
|
|
|
Start(
|
|
|
|
Footnote {
|
|
|
|
tag: "c",
|
|
|
|
number: 3
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
End(Footnote {
|
|
|
|
tag: "c",
|
|
|
|
number: 3
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn footnote() {
|
|
|
|
test_parse!(
|
|
|
|
"[^a]\n\n[^a]: a\n",
|
|
|
|
Start(Paragraph, Attributes::new()),
|
2023-02-04 15:59:01 -05:00
|
|
|
FootnoteReference("a", 1),
|
2023-01-18 16:30:24 -05:00
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-18 16:30:24 -05:00
|
|
|
Start(
|
|
|
|
Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("a".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn footnote_multiblock() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"[^a]\n",
|
|
|
|
"\n",
|
|
|
|
"[^a]: abc\n",
|
|
|
|
"\n",
|
|
|
|
" def", //
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
2023-02-04 15:59:01 -05:00
|
|
|
FootnoteReference("a", 1),
|
2023-01-18 16:30:24 -05:00
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-18 16:30:24 -05:00
|
|
|
Start(
|
|
|
|
Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("abc".into()),
|
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-18 16:30:24 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("def".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn footnote_post() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"[^a]\n",
|
|
|
|
"\n",
|
|
|
|
"[^a]: note\n",
|
|
|
|
"para\n", //
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
2023-02-04 15:59:01 -05:00
|
|
|
FootnoteReference("a", 1),
|
2023-01-18 16:30:24 -05:00
|
|
|
End(Paragraph),
|
2023-02-04 15:59:01 -05:00
|
|
|
Blankline,
|
2023-01-18 16:30:24 -05:00
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("para".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
Start(
|
|
|
|
Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
},
|
|
|
|
Attributes::new()
|
|
|
|
),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("note".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(Footnote {
|
|
|
|
tag: "a",
|
|
|
|
number: 1
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-18 12:05:39 -05:00
|
|
|
#[test]
|
|
|
|
fn attr_block() {
|
|
|
|
test_parse!(
|
|
|
|
"{.some_class}\npara\n",
|
|
|
|
Start(Paragraph, [("class", "some_class")].into_iter().collect()),
|
|
|
|
Str("para".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
);
|
|
|
|
}
|
2023-01-15 09:47:28 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn attr_inline() {
|
|
|
|
test_parse!(
|
|
|
|
"abc _def_{.ghi}",
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("abc ".into()),
|
|
|
|
Start(Emphasis, [("class", "ghi")].into_iter().collect()),
|
|
|
|
Str("def".into()),
|
|
|
|
End(Emphasis),
|
|
|
|
End(Paragraph),
|
|
|
|
);
|
|
|
|
}
|
2023-01-21 15:21:43 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_item_unordered() {
|
|
|
|
test_parse!(
|
|
|
|
"- abc",
|
2023-01-22 06:39:04 -05:00
|
|
|
Start(
|
|
|
|
List {
|
|
|
|
kind: ListKind::Unordered,
|
|
|
|
tight: true,
|
|
|
|
},
|
|
|
|
Attributes::new(),
|
|
|
|
),
|
2023-01-21 15:21:43 -05:00
|
|
|
Start(ListItem, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("abc".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(ListItem),
|
2023-01-22 06:39:04 -05:00
|
|
|
End(List {
|
|
|
|
kind: ListKind::Unordered,
|
|
|
|
tight: true,
|
|
|
|
}),
|
2023-01-21 15:21:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_item_ordered_decimal() {
|
|
|
|
test_parse!(
|
|
|
|
"123. abc",
|
|
|
|
Start(
|
2023-01-22 06:39:04 -05:00
|
|
|
List {
|
|
|
|
kind: ListKind::Ordered {
|
|
|
|
numbering: Decimal,
|
|
|
|
style: Period,
|
|
|
|
start: 123
|
|
|
|
},
|
|
|
|
tight: true,
|
|
|
|
},
|
|
|
|
Attributes::new(),
|
2023-01-21 15:21:43 -05:00
|
|
|
),
|
|
|
|
Start(ListItem, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("abc".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(ListItem),
|
2023-01-22 06:39:04 -05:00
|
|
|
End(List {
|
|
|
|
kind: ListKind::Ordered {
|
|
|
|
numbering: Decimal,
|
|
|
|
style: Period,
|
|
|
|
start: 123
|
|
|
|
},
|
|
|
|
tight: true,
|
|
|
|
}),
|
2023-01-21 15:21:43 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_task() {
|
|
|
|
test_parse!(
|
|
|
|
concat!(
|
|
|
|
"- [ ] a\n", //
|
|
|
|
"- [x] b\n", //
|
|
|
|
"- [X] c\n", //
|
|
|
|
),
|
2023-01-22 06:39:04 -05:00
|
|
|
Start(
|
|
|
|
List {
|
|
|
|
kind: ListKind::Task,
|
|
|
|
tight: true,
|
|
|
|
},
|
|
|
|
Attributes::new(),
|
|
|
|
),
|
2023-01-21 15:21:43 -05:00
|
|
|
Start(TaskListItem { checked: false }, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("a".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(TaskListItem { checked: false }),
|
|
|
|
Start(TaskListItem { checked: true }, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("b".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(TaskListItem { checked: true }),
|
|
|
|
Start(TaskListItem { checked: true }, Attributes::new()),
|
|
|
|
Start(Paragraph, Attributes::new()),
|
|
|
|
Str("c".into()),
|
|
|
|
End(Paragraph),
|
|
|
|
End(TaskListItem { checked: true }),
|
2023-01-22 06:39:04 -05:00
|
|
|
End(List {
|
|
|
|
kind: ListKind::Task,
|
|
|
|
tight: true,
|
|
|
|
}),
|
2023-01-21 15:21:43 -05:00
|
|
|
);
|
|
|
|
}
|
2023-01-23 15:41:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn numbering_alpha() {
|
|
|
|
assert_eq!(AlphaLower.parse_number("a"), 1);
|
|
|
|
assert_eq!(AlphaUpper.parse_number("B"), 2);
|
|
|
|
assert_eq!(AlphaUpper.parse_number("Z"), 26);
|
|
|
|
assert_eq!(AlphaLower.parse_number("aa"), 27);
|
|
|
|
}
|
2022-11-22 13:19:21 -05:00
|
|
|
}
|