use alloc::string::String;
use crate::tree::NodeRef;
mod html;
mod text;
#[cfg(feature = "markdown")]
mod markdown;
#[cfg(feature = "markdown")]
mod chunk;
#[cfg(feature = "markdown")]
pub use chunk::Chunk;
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "serde")]
mod serde_impl;
pub(crate) fn walk<'a>(root: NodeRef<'a>, mut f: impl FnMut(NodeRef<'a>, bool) -> bool) {
let mut cur = root;
loop {
if f(cur, true) {
if let Some(child) = cur.first_child() {
cur = child;
continue;
}
f(cur, false);
}
loop {
if cur.id() == root.id() {
return;
}
if let Some(next) = cur.next_sibling() {
cur = next;
break;
}
match cur.parent() {
Some(parent) => {
cur = parent;
f(cur, false);
}
None => return,
}
}
}
}
pub(crate) fn is_space(c: char) -> bool {
matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C' | '\u{a0}')
}
pub(crate) fn skip_newline(text: &str) -> &str {
text.strip_prefix("\r\n")
.or_else(|| text.strip_prefix(['\n', '\r']))
.unwrap_or(text)
}
pub(crate) fn push_verbatim(out: &mut String, text: &str) {
let mut cr = false;
for c in text.chars() {
if c == '\n' && cr {
cr = false;
continue;
}
cr = c == '\r';
out.push(if cr { '\n' } else { c });
}
}