add cowstr
This commit is contained in:
parent
f74ea7a138
commit
903578b04d
2 changed files with 75 additions and 36 deletions
53
src/html.rs
53
src/html.rs
|
@ -162,34 +162,37 @@ impl<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write> Writer<I, W> {
|
||||||
Container::DoubleQuoted => self.out.write_str("”")?,
|
Container::DoubleQuoted => self.out.write_str("”")?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Str(mut s) => match self.raw {
|
Event::Str(s) => {
|
||||||
Raw::None => {
|
let mut s: &str = s.as_ref();
|
||||||
let mut ent = "";
|
match self.raw {
|
||||||
while let Some(i) = s.chars().position(|c| {
|
Raw::None => {
|
||||||
if let Some(s) = match c {
|
let mut ent = "";
|
||||||
'<' => Some("<"),
|
while let Some(i) = s.chars().position(|c| {
|
||||||
'>' => Some(">"),
|
if let Some(s) = match c {
|
||||||
'&' => Some("&"),
|
'<' => Some("<"),
|
||||||
'"' => Some("""),
|
'>' => Some(">"),
|
||||||
_ => None,
|
'&' => Some("&"),
|
||||||
} {
|
'"' => Some("""),
|
||||||
ent = s;
|
_ => None,
|
||||||
true
|
} {
|
||||||
} else {
|
ent = s;
|
||||||
false
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
self.out.write_str(&s[..i])?;
|
||||||
|
self.out.write_str(ent)?;
|
||||||
|
s = &s[i + 1..];
|
||||||
}
|
}
|
||||||
}) {
|
self.out.write_str(s)?;
|
||||||
self.out.write_str(&s[..i])?;
|
|
||||||
self.out.write_str(ent)?;
|
|
||||||
s = &s[i + 1..];
|
|
||||||
}
|
}
|
||||||
self.out.write_str(s)?;
|
Raw::Html => {
|
||||||
|
self.out.write_str(s)?;
|
||||||
|
}
|
||||||
|
Raw::Other => {}
|
||||||
}
|
}
|
||||||
Raw::Html => {
|
}
|
||||||
self.out.write_str(s)?;
|
|
||||||
}
|
|
||||||
Raw::Other => {}
|
|
||||||
},
|
|
||||||
|
|
||||||
Event::Atom(a) => match a {
|
Event::Atom(a) => match a {
|
||||||
Atom::Ellipsis => self.out.write_str("…")?,
|
Atom::Ellipsis => self.out.write_str("…")?,
|
||||||
|
|
58
src/lib.rs
58
src/lib.rs
|
@ -8,6 +8,8 @@ mod tree;
|
||||||
|
|
||||||
use span::Span;
|
use span::Span;
|
||||||
|
|
||||||
|
type CowStr<'s> = std::borrow::Cow<'s, str>;
|
||||||
|
|
||||||
const EOF: char = '\0';
|
const EOF: char = '\0';
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
@ -17,7 +19,7 @@ pub enum Event<'s> {
|
||||||
/// End of a container.
|
/// End of a container.
|
||||||
End(Container<'s>),
|
End(Container<'s>),
|
||||||
/// A string object, text only.
|
/// A string object, text only.
|
||||||
Str(&'s str),
|
Str(CowStr<'s>),
|
||||||
/// An atomic element.
|
/// An atomic element.
|
||||||
Atom(Atom),
|
Atom(Atom),
|
||||||
}
|
}
|
||||||
|
@ -57,9 +59,9 @@ pub enum Container<'s> {
|
||||||
/// An inline divider element.
|
/// An inline divider element.
|
||||||
Span,
|
Span,
|
||||||
/// An inline link with a destination URL.
|
/// An inline link with a destination URL.
|
||||||
Link(&'s str, LinkType),
|
Link(CowStr<'s>, LinkType),
|
||||||
/// An inline image.
|
/// An inline image.
|
||||||
Image(&'s str),
|
Image(CowStr<'s>),
|
||||||
/// An inline verbatim string.
|
/// An inline verbatim string.
|
||||||
Verbatim,
|
Verbatim,
|
||||||
/// An inline or display math element.
|
/// An inline or display math element.
|
||||||
|
@ -242,6 +244,9 @@ impl<'s> Event<'s> {
|
||||||
inline::Container::Mark => Container::Mark,
|
inline::Container::Mark => Container::Mark,
|
||||||
inline::Container::SingleQuoted => Container::SingleQuoted,
|
inline::Container::SingleQuoted => Container::SingleQuoted,
|
||||||
inline::Container::DoubleQuoted => Container::DoubleQuoted,
|
inline::Container::DoubleQuoted => Container::DoubleQuoted,
|
||||||
|
inline::Container::InlineLink => {
|
||||||
|
Container::Link(content.into(), LinkType::Inline)
|
||||||
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
};
|
};
|
||||||
if matches!(inline.kind, inline::EventKind::Enter(_)) {
|
if matches!(inline.kind, inline::EventKind::Enter(_)) {
|
||||||
|
@ -259,7 +264,7 @@ impl<'s> Event<'s> {
|
||||||
inline::Atom::Hardbreak => Atom::Hardbreak,
|
inline::Atom::Hardbreak => Atom::Hardbreak,
|
||||||
inline::Atom::Escape => Atom::Escape,
|
inline::Atom::Escape => Atom::Escape,
|
||||||
}),
|
}),
|
||||||
inline::EventKind::Str => Self::Str(content),
|
inline::EventKind::Str => Self::Str(content.into()),
|
||||||
inline::EventKind::Attributes => todo!(),
|
inline::EventKind::Attributes => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -418,10 +423,12 @@ mod test {
|
||||||
use super::Atom::*;
|
use super::Atom::*;
|
||||||
use super::Attributes;
|
use super::Attributes;
|
||||||
use super::Container::*;
|
use super::Container::*;
|
||||||
|
use super::CowStr;
|
||||||
use super::Event::*;
|
use super::Event::*;
|
||||||
|
use super::LinkType;
|
||||||
|
|
||||||
macro_rules! test_parse {
|
macro_rules! test_parse {
|
||||||
($($st:ident,)? $src:expr $(,$($token:expr),* $(,)?)?) => {
|
($src:expr $(,$($token:expr),* $(,)?)?) => {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
let actual = super::Parser::new($src).collect::<Vec<_>>();
|
let actual = super::Parser::new($src).collect::<Vec<_>>();
|
||||||
let expected = &[$($($token),*,)?];
|
let expected = &[$($($token),*,)?];
|
||||||
|
@ -469,23 +476,23 @@ mod test {
|
||||||
test_parse!(
|
test_parse!(
|
||||||
"para",
|
"para",
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Str("para"),
|
Str(CowStr::Borrowed("para")),
|
||||||
End(Paragraph),
|
End(Paragraph),
|
||||||
);
|
);
|
||||||
test_parse!(
|
test_parse!(
|
||||||
"pa ra",
|
"pa ra",
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Str("pa ra"),
|
Str(CowStr::Borrowed("pa ra")),
|
||||||
End(Paragraph),
|
End(Paragraph),
|
||||||
);
|
);
|
||||||
test_parse!(
|
test_parse!(
|
||||||
"para0\n\npara1",
|
"para0\n\npara1",
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Str("para0"),
|
Str(CowStr::Borrowed("para0")),
|
||||||
End(Paragraph),
|
End(Paragraph),
|
||||||
Atom(Blankline),
|
Atom(Blankline),
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Str("para1"),
|
Str(CowStr::Borrowed("para1")),
|
||||||
End(Paragraph),
|
End(Paragraph),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -496,7 +503,7 @@ mod test {
|
||||||
"`abc\ndef",
|
"`abc\ndef",
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Start(Verbatim, Attributes::none()),
|
Start(Verbatim, Attributes::none()),
|
||||||
Str("abc\ndef"),
|
Str(CowStr::Borrowed("abc\ndef")),
|
||||||
End(Verbatim),
|
End(Verbatim),
|
||||||
End(Paragraph),
|
End(Paragraph),
|
||||||
);
|
);
|
||||||
|
@ -508,9 +515,38 @@ mod test {
|
||||||
"``raw\nraw``{=format}",
|
"``raw\nraw``{=format}",
|
||||||
Start(Paragraph, Attributes::none()),
|
Start(Paragraph, Attributes::none()),
|
||||||
Start(RawInline { format: "format" }, Attributes::none()),
|
Start(RawInline { format: "format" }, Attributes::none()),
|
||||||
Str("raw\nraw"),
|
Str(CowStr::Borrowed("raw\nraw")),
|
||||||
End(RawInline { format: "format" }),
|
End(RawInline { format: "format" }),
|
||||||
End(Paragraph),
|
End(Paragraph),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn link_inline() {
|
||||||
|
test_parse!(
|
||||||
|
"[text](url)",
|
||||||
|
Start(Paragraph, Attributes::none()),
|
||||||
|
Start(
|
||||||
|
Link(CowStr::Borrowed("url"), LinkType::Inline),
|
||||||
|
Attributes::none()
|
||||||
|
),
|
||||||
|
Str(CowStr::Borrowed("text")),
|
||||||
|
End(Link(CowStr::Borrowed("url"), LinkType::Inline)),
|
||||||
|
End(Paragraph),
|
||||||
|
);
|
||||||
|
test_parse!(
|
||||||
|
concat!(
|
||||||
|
"> [text](url\n",
|
||||||
|
"> url)\n", //
|
||||||
|
),
|
||||||
|
Start(Paragraph, Attributes::none()),
|
||||||
|
Start(
|
||||||
|
Link(CowStr::Borrowed("urlurl"), LinkType::Inline),
|
||||||
|
Attributes::none()
|
||||||
|
),
|
||||||
|
Str(CowStr::Borrowed("text")),
|
||||||
|
End(Link(CowStr::Borrowed("urlurl"), LinkType::Inline)),
|
||||||
|
End(Paragraph),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue