mod output;
#[cfg(test)]
mod stack_tests;
mod walker;
use std::collections::HashSet;
use crate::options::ConversionOptions;
use crate::text;
use output::PlainOutput;
#[cfg(feature = "visitor")]
use crate::converter::utility::content::is_block_level_element;
#[cfg(feature = "visitor")]
use crate::visitor::EMPTY_ATTRS;
#[cfg(feature = "visitor")]
use crate::visitor::{NodeContext, NodeType, VisitResult, VisitorHandle};
#[cfg(feature = "visitor")]
use output::Checkpoint;
#[cfg(feature = "visitor")]
use std::borrow::Cow;
#[derive(Clone, Copy, Debug)]
enum ListContext {
None,
Unordered,
Ordered { next_index: u32 },
}
const SKIP_TAGS: &[&str] = &["script", "style", "head", "template", "noscript", "math"];
const BLOCK_TAGS: &[&str] = &[
"p",
"div",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"blockquote",
"section",
"article",
"aside",
"main",
"nav",
"header",
"footer",
"figure",
"figcaption",
"details",
"summary",
"address",
"hgroup",
"search",
"center",
"dialog",
"menu",
"legend",
"dt",
"dd",
];
#[derive(Clone, Copy)]
struct WalkState<'a> {
options: &'a ConversionOptions,
excluded_node_ids: &'a HashSet<u32>,
item_marker_end: Option<usize>,
in_pre: bool,
in_option_list: bool,
in_document_body: bool,
in_content_header_scope: bool,
depth: usize,
#[cfg(feature = "visitor")]
visitor: Option<&'a VisitorHandle>,
}
impl WalkState<'_> {
const fn descend(&self) -> Self {
WalkState {
options: self.options,
excluded_node_ids: self.excluded_node_ids,
item_marker_end: self.item_marker_end,
in_pre: self.in_pre,
in_option_list: self.in_option_list,
in_document_body: self.in_document_body,
in_content_header_scope: self.in_content_header_scope,
depth: self.depth + 1,
#[cfg(feature = "visitor")]
visitor: self.visitor,
}
}
const fn for_own_buffer(&self) -> Self {
WalkState {
item_marker_end: None,
..*self
}
}
fn excludes(&self, node_handle: &tl::NodeHandle) -> bool {
self.excluded_node_ids.contains(&node_handle.get_inner())
}
}
pub fn extract_plain_text(dom: &tl::VDom, parser: &tl::Parser, options: &ConversionOptions) -> String {
let excluded_node_ids: HashSet<u32> = if options.exclude_selectors.is_empty() {
HashSet::new()
} else {
let mut ids = HashSet::new();
for selector in &options.exclude_selectors {
if let Some(iter) = dom.query_selector(selector) {
for handle in iter {
ids.insert(handle.get_inner());
}
}
}
ids
};
let state = WalkState {
options,
excluded_node_ids: &excluded_node_ids,
item_marker_end: None,
in_pre: false,
in_option_list: false,
in_document_body: false,
in_content_header_scope: false,
depth: 0,
#[cfg(feature = "visitor")]
visitor: options.visitor.as_ref(),
};
let mut text = walker::extract(dom.children(), parser, state);
post_process(&mut text);
text
}
fn emit_raw_text(bytes: &tl::Bytes<'_>, buf: &mut PlainOutput, state: &WalkState<'_>, after_passed_over: bool) {
let raw = bytes.as_utf8_str();
let decoded = text::decode_html_entities_cow(raw.as_ref());
#[cfg(feature = "visitor")]
if visit_plain_text(&decoded, buf, state) {
return;
}
if state.in_pre {
buf.push_text(&decoded);
return;
}
let normalized = text::normalize_whitespace_cow(&decoded);
let joins_run = after_passed_over && decoded.starts_with([' ', '\t']) && buf.ends_with_space();
let words = normalized
.strip_prefix(' ')
.filter(|_| joins_run)
.unwrap_or_else(|| normalized.as_ref());
if !(words.is_empty() || words == " " && buf.ends_with_line_end()) {
buf.push_text(words);
}
}
#[cfg(feature = "visitor")]
fn visit_plain_text(decoded: &str, buf: &mut PlainOutput, state: &WalkState<'_>) -> bool {
let Some(visitor_handle) = state.visitor else {
return false;
};
let node_ctx = NodeContext::with_borrowed_attributes(
NodeType::Text,
Cow::Borrowed(""),
&EMPTY_ATTRS,
state.depth,
0,
None,
true,
);
let visit_result = visitor_handle
.lock()
.expect("visitor mutex poisoned")
.visit_text(&node_ctx, decoded);
match visit_result {
VisitResult::Skip => true,
VisitResult::Custom(custom) => {
buf.push_text(&custom);
true
}
_ => false,
}
}
#[cfg(feature = "visitor")]
fn visit_plain_element_start(
tag_name: &str,
tag: &tl::HTMLTag<'_>,
buf: &mut PlainOutput,
state: &WalkState<'_>,
) -> bool {
let Some(visitor_handle) = state.visitor else {
return false;
};
let node_ctx = plain_element_context(tag_name, tag, state.depth);
let result = visitor_handle
.lock()
.expect("visitor mutex poisoned")
.visit_element_start(&node_ctx);
let VisitResult::Custom(custom) = result else {
return matches!(result, VisitResult::Skip);
};
let start = buf.open();
buf.push_text(&custom);
let end_result = visitor_handle
.lock()
.expect("visitor mutex poisoned")
.visit_element_end(&node_ctx, &custom);
close_plain_checkpoint(buf, start, end_result);
true
}
#[cfg(feature = "visitor")]
fn visit_plain_element_end(
visitor_handle: &VisitorHandle,
tag_name: &str,
tag: &tl::HTMLTag<'_>,
buf: &mut PlainOutput,
depth: usize,
element_start: Checkpoint,
) {
let node_ctx = plain_element_context(tag_name, tag, depth);
let result = visitor_handle
.lock()
.expect("visitor mutex poisoned")
.visit_element_end(&node_ctx, buf.since(&element_start));
close_plain_checkpoint(buf, element_start, result);
}
#[cfg(feature = "visitor")]
fn close_plain_checkpoint(buf: &mut PlainOutput, checkpoint: Checkpoint, end_result: VisitResult) {
match end_result {
VisitResult::Custom(custom) => {
buf.rollback(checkpoint);
buf.push_text(&custom);
}
VisitResult::Skip => buf.rollback(checkpoint),
_ => buf.commit(checkpoint),
}
}
#[cfg(feature = "visitor")]
fn plain_element_context<'a>(tag_name: &'a str, tag: &'a tl::HTMLTag<'a>, depth: usize) -> NodeContext<'a> {
NodeContext::with_lazy_attributes(
NodeType::Element,
Cow::Borrowed(tag_name),
tag,
depth,
0,
None,
!is_block_level_element(tag_name),
)
}
fn emit_plain_image(tag: &tl::HTMLTag<'_>, buf: &mut PlainOutput, options: &ConversionOptions) {
if options.skip_images {
return;
}
if let Some(alt) = crate::converter::utility::attributes::decoded_attribute(tag, "alt")
&& !alt.is_empty()
{
buf.push_text(alt.as_ref());
}
}
fn collect_descendant_handles(
tag: &tl::HTMLTag,
parser: &tl::Parser,
target_tag: &str,
state: &WalkState<'_>,
result: &mut Vec<tl::NodeHandle>,
) {
let children = tag.children();
let mut stack: Vec<_> = children.top().iter().copied().collect();
stack.reverse();
while let Some(handle) = stack.pop() {
if state.excludes(&handle) {
continue;
}
if let Some(tl::Node::Tag(child_tag)) = handle.get(parser) {
let name = child_tag.name().as_utf8_str();
if SKIP_TAGS.iter().any(|skip| name.eq_ignore_ascii_case(skip)) {
continue;
}
if name.eq_ignore_ascii_case(target_tag) {
result.push(handle);
} else {
let child_children = child_tag.children();
let mut child_handles: Vec<_> = child_children.top().iter().copied().collect();
child_handles.reverse();
for child in child_handles {
stack.push(child);
}
}
}
}
}
fn normalize_plain_output(buf: &mut String) {
let input = std::mem::take(buf);
let mut out = String::with_capacity(input.len());
let mut last_was_blank = false;
for line in input.lines().map(str::trim_end) {
if line.is_empty() {
if !last_was_blank {
out.push('\n');
last_was_blank = true;
}
} else {
out.push_str(line);
out.push('\n');
last_was_blank = false;
}
}
let keep = out.trim_end_matches('\n').len();
out.truncate(keep);
if !out.is_empty() {
out.push('\n');
}
*buf = out;
}
fn post_process(buf: &mut String) {
normalize_plain_output(buf);
}