#![deny(unsafe_code)]
mod emph;
mod link;
use self::emph::{Delim, Node};
use super::ast::{Inline, LinkRefs};
pub fn parse_inlines(src: &str, link_refs: &LinkRefs) -> Vec<Inline> {
let chars: Vec<char> = src.chars().collect();
emph::resolve(scan(&chars, link_refs, false))
}
fn parse_nested(src: &str, link_refs: &LinkRefs) -> Vec<Inline> {
let chars: Vec<char> = src.chars().collect();
emph::resolve(scan(&chars, link_refs, true))
}
pub(crate) struct Scan {
nodes: Vec<Node>,
buf: String,
}
impl Scan {
fn flush(&mut self) {
if !self.buf.is_empty() {
let s = std::mem::take(&mut self.buf);
self.nodes.push(Node::Inl(Inline::Text(s)));
}
}
pub(crate) fn push(&mut self, inl: Inline) {
self.flush();
self.nodes.push(Node::Inl(inl));
}
}
fn scan(chars: &[char], refs: &LinkRefs, in_link: bool) -> Vec<Node> {
let mut st = Scan {
nodes: Vec::new(),
buf: String::new(),
};
let mut i = 0;
while i < chars.len() {
i = match step(&mut st, chars, i, refs, in_link) {
Some(next) if next > i => next,
_ => {
st.buf.push(chars[i]);
i + 1
}
};
}
st.flush();
st.nodes
}
fn step(st: &mut Scan, chars: &[char], i: usize, refs: &LinkRefs, in_link: bool) -> Option<usize> {
match chars[i] {
'\\' => Some(escape(st, chars, i)),
'`' => Some(code_span(st, chars, i)),
'\n' => Some(line_break(st, chars, i)),
'<' => link::angle(st, chars, i),
'!' if !in_link && chars.get(i + 1) == Some(&'[') => link::image(st, chars, i, refs),
'[' if !in_link => link::bracket(st, chars, i, refs),
'*' | '_' | '~' => Some(delim_run(st, chars, i)),
'h' if !in_link => link::bare_url(st, chars, i),
_ => None,
}
}
fn escape(st: &mut Scan, chars: &[char], i: usize) -> usize {
match chars.get(i + 1) {
Some('\n') => {
st.push(Inline::HardBreak);
skip_indent(chars, i + 2)
}
Some(&c) if c.is_ascii_punctuation() => {
st.buf.push(c);
i + 2
}
_ => {
st.buf.push('\\');
i + 1
}
}
}
fn code_span(st: &mut Scan, chars: &[char], i: usize) -> usize {
let n = link::run_len(chars, i, '`');
let mut j = i + n;
while j < chars.len() {
if chars[j] != '`' {
j += 1;
continue;
}
let m = link::run_len(chars, j, '`');
if m == n {
st.push(Inline::Code(code_text(&chars[i + n..j])));
return j + m;
}
j += m;
}
for _ in 0..n {
st.buf.push('`');
}
i + n
}
fn code_text(body: &[char]) -> String {
let mut s: String = body
.iter()
.map(|&c| if c == '\n' { ' ' } else { c })
.collect();
let trimmable = s.starts_with(' ') && s.ends_with(' ') && s.chars().any(|c| c != ' ');
if trimmable {
s.pop();
s.remove(0);
}
s
}
fn line_break(st: &mut Scan, chars: &[char], i: usize) -> usize {
let keep = st.buf.trim_end_matches([' ', '\t']).len();
let hard = st.buf.len() - keep >= 2;
st.buf.truncate(keep);
st.push(if hard {
Inline::HardBreak
} else {
Inline::SoftBreak
});
skip_indent(chars, i + 1)
}
fn skip_indent(chars: &[char], mut j: usize) -> usize {
while chars.get(j).is_some_and(|&c| c == ' ' || c == '\t') {
j += 1;
}
j
}
fn delim_run(st: &mut Scan, chars: &[char], i: usize) -> usize {
let ch = chars[i];
let n = link::run_len(chars, i, ch);
let prev = if i > 0 { Some(chars[i - 1]) } else { None };
let next = chars.get(i + n).copied();
let (mut open, mut close) = flanking(ch, prev, next);
if ch == '~' && n > 2 {
open = false;
close = false;
}
if !open && !close {
for _ in 0..n {
st.buf.push(ch);
}
return i + n;
}
st.flush();
st.nodes.push(Node::Delim(Delim {
ch,
len: n,
orig: n,
open,
close,
}));
i + n
}
fn flanking(ch: char, prev: Option<char>, next: Option<char>) -> (bool, bool) {
let (pw, nw) = (
prev.map_or(true, char::is_whitespace),
next.map_or(true, char::is_whitespace),
);
let (pp, np) = (prev.is_some_and(is_punct), next.is_some_and(is_punct));
let left = !nw && (!np || pw || pp);
let right = !pw && (!pp || nw || np);
if ch == '_' {
(left && (!right || pp), right && (!left || np))
} else {
(left, right)
}
}
pub(crate) fn is_punct(c: char) -> bool {
c.is_ascii_punctuation() || (!c.is_ascii() && !c.is_alphanumeric() && !c.is_whitespace())
}
#[cfg(test)]
mod tests;