use super::{ends_with_block_line_end, is_inline_content};
use crate::converter::block::container::HandlerContext;
use crate::converter::dom_context::DomContext;
use crate::converter::main_helpers::{is_inline_element, strip_trailing_backslash_breaks, trim_trailing_whitespace};
use crate::converter::preprocessing_helpers::{is_page_header, should_drop_for_preprocessing};
use crate::converter::utility::content::{is_block_level_element, is_inline_code};
use crate::converter::utility::siblings::previous_content_block_through;
use crate::options::NewlineStyle;
pub(super) fn separate_in_list_item(
node: &crate::dom::Node,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: HandlerContext<'_>,
) {
let HandlerContext {
options, ctx, dom_ctx, ..
} = handler;
let starts_block = match node {
crate::dom::Node::Tag(tag) => dom_ctx.tag_info(node_handle.get_inner(), parser).is_some_and(|info| {
is_block_level_element(&info.name)
&& !matches!(info.name.as_str(), "ul" | "ol" | "li")
&& !should_drop_for_preprocessing(
&info.name,
tag,
options,
info.name == "header" && is_page_header(node_handle, parser, dom_ctx),
)
}),
_ => false,
};
let indent =
crate::converter::list::utils::continuation_indent_string(ctx.list_indent_columns, options).unwrap_or_default();
let (blank_line, block_continues_lazily) = if starts_block {
if options.newline_style == NewlineStyle::Backslash {
strip_trailing_backslash_breaks(output, ctx.block_content_start);
}
let line_start = output.rfind('\n').map_or(0, |pos| pos + 1);
let line = &output[line_start..];
let after_content = line.is_empty()
|| (!line.trim().is_empty() && !crate::converter::list::utils::line_is_bare_list_marker(output));
if !after_content {
return;
}
(false, false)
} else if ends_with_block_line_end(output) && is_inline_content(node, node_handle, parser, dom_ctx) {
match crate::converter::utility::siblings::previous_content_block(node_handle, parser, dom_ctx) {
Some(block) => (
!matches!(block, "h1" | "h2" | "h3" | "h4" | "h5" | "h6"),
matches!(block, "blockquote" | "ul" | "ol"),
),
None => return,
}
} else {
return;
};
let item_is_open = crate::converter::list::utils::item_is_open(output, &indent, ctx);
let separates =
item_is_open || (block_continues_lazily && crate::converter::list::utils::block_is_real(ctx, options));
if !separates {
return;
}
trim_trailing_whitespace(output);
if !output.ends_with('\n') {
output.push('\n');
}
if blank_line && !output.ends_with("\n\n") {
output.push('\n');
}
if item_is_open {
output.push_str(&indent);
} else if let Some(real_item_indent) =
crate::converter::list::utils::continuation_indent_string(ctx.real_item_columns, options)
{
output.push_str(&real_item_indent);
}
}
pub(super) fn separate_words_at_block_boundary(
node: &crate::dom::Node,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &mut String,
handler: HandlerContext<'_>,
) {
let HandlerContext { ctx, dom_ctx, .. } = handler;
if output.ends_with(char::is_whitespace) {
return;
}
let kept_as_html = dom_ctx
.tag_info(node_handle.get_inner(), parser)
.is_some_and(|info| ctx.preserve_tags.contains(info.name.as_str()));
if kept_as_html {
return;
}
let looks_into: fn(&str) -> bool = if ctx.in_link {
is_inline_element
} else {
wraps_running_text
};
let at_boundary = starts_with_block(node_handle, parser, dom_ctx, looks_into)
|| (is_inline_content(node, node_handle, parser, dom_ctx)
&& previous_content_block_through(node_handle, parser, dom_ctx, looks_into)
.is_some_and(|block| !ctx.preserve_tags.contains(block)));
if at_boundary {
output.push(' ');
crate::converter::structure_capture::append_text(ctx, " ");
}
}
fn wraps_running_text(tag_name: &str) -> bool {
is_inline_element(tag_name) && !is_inline_code(tag_name)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn starts_with_block(
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
dom_ctx: &DomContext,
looks_into: fn(&str) -> bool,
) -> bool {
let mut handle = *node_handle;
loop {
if crate::converter::utility::content::node_is_block_level(&handle, parser, dom_ctx) {
return true;
}
let is_closed = dom_ctx
.tag_info(handle.get_inner(), parser)
.is_some_and(|info| is_inline_element(&info.name) && !looks_into(&info.name));
if is_closed {
return false;
}
let first_content = dom_ctx.children_of(handle.get_inner()).and_then(|children| {
children.iter().find(|child| match child.get(parser) {
Some(crate::dom::Node::Raw(raw)) => !raw.as_utf8_str().trim().is_empty(),
Some(crate::dom::Node::Tag(_)) => true,
_ => false,
})
});
let Some(first) = first_content else {
return false;
};
handle = *first;
}
}