use crate::converter::block::container::HandlerContext;
use crate::converter::main_helpers::{strip_trailing_backslash_breaks, trim_trailing_whitespace};
use crate::converter::walk::{Close, Open};
use crate::options::{ConversionOptions, NewlineStyle};
use std::rc::Rc;
use tl::{NodeHandle, Parser};
type Context = crate::converter::Context;
pub fn handle(node_handle: &NodeHandle, parser: &Parser, output: &mut String, handler: HandlerContext<'_>) {
crate::converter::main::walk_opened(parser, output, handler, |output, children| {
open(node_handle, parser, output, handler, children)
});
}
pub struct DivEnd {
content_start_pos: usize,
kept: Option<(usize, String)>,
children_start: usize,
is_list_continuation: bool,
}
pub struct InlineDivEnd {
in_heading_line: bool,
content_start: usize,
needs_prefix: bool,
}
pub fn open(
node_handle: &NodeHandle,
parser: &Parser,
output: &mut String,
handler: HandlerContext<'_>,
children: &mut Vec<NodeHandle>,
) -> Option<Open> {
let HandlerContext { options, ctx, .. } = handler;
let Some(tl::Node::Tag(tag)) = node_handle.get(parser) else {
return None;
};
let is_table_continuation = (ctx.in_table_cell || ctx.in_layout_cell)
&& !output.is_empty()
&& !output.ends_with('|')
&& !output.ends_with("<br>")
&& !output.ends_with('\n');
if ctx.convert_as_inline {
let end = open_inline(tag, output, handler, is_table_continuation);
children.extend(tag.children().top().iter().copied());
return Some(Open {
scope: None,
close: Close::InlineDiv(end),
});
}
let content_start_pos = output.len();
let kept = ctx.in_list_item.then(|| {
let kept_len = output.trim_end_matches([' ', '\t']).len();
(kept_len, output[kept_len..].to_string())
});
let is_list_continuation = open_block(output, options, ctx);
let children_start = output.len();
let div_ctx = Context {
block_content_start: output.len(),
block_output_ptr: std::ptr::from_ref::<String>(output) as usize,
inline_break_owner: None,
..ctx.clone()
};
children.extend(tag.children().top().iter().copied());
Some(Open {
scope: Some(Rc::new(div_ctx)),
close: Close::Div(DivEnd {
content_start_pos,
kept,
children_start,
is_list_continuation,
}),
})
}
pub fn close(end: DivEnd, output: &mut String, handler: HandlerContext<'_>) {
let DivEnd {
content_start_pos,
kept,
children_start,
is_list_continuation,
} = end;
let HandlerContext { options, ctx, .. } = handler;
if options.newline_style == NewlineStyle::Backslash {
strip_trailing_backslash_breaks(output, content_start_pos);
}
if let Some((kept_len, kept_tail)) = kept.filter(|_| output.len() == children_start) {
output.truncate(kept_len);
output.push_str(&kept_tail);
}
close_block(output, ctx, content_start_pos, is_list_continuation);
}
fn open_inline(
tag: &tl::HTMLTag<'_>,
output: &mut String,
handler: HandlerContext<'_>,
is_table_continuation: bool,
) -> InlineDivEnd {
if is_table_continuation {
crate::converter::main_helpers::emit_table_cell_break_in_context(
output,
handler.options.br_in_tables,
handler.ctx,
);
}
let in_heading_line = handler.ctx.in_heading
&& !is_table_continuation
&& (tag.name().as_utf8_str().eq_ignore_ascii_case("div")
|| (!handler.ctx.in_code && handler.options.whitespace_mode != crate::options::WhitespaceMode::Strict));
InlineDivEnd {
in_heading_line,
content_start: output.len(),
needs_prefix: in_heading_line && !output.is_empty() && !output.ends_with(char::is_whitespace),
}
}
pub fn close_inline(end: &InlineDivEnd, output: &mut String) {
if end.needs_prefix && !output[end.content_start..].starts_with(char::is_whitespace) {
output.insert(end.content_start, ' ');
}
if end.in_heading_line && !output.ends_with(char::is_whitespace) {
output.push(' ');
}
}
pub fn push_block(output: &mut String, options: &ConversionOptions, ctx: &Context, text: &str) {
let content_start_pos = output.len();
let is_list_continuation = open_block(output, options, ctx);
output.push_str(text);
close_block(output, ctx, content_start_pos, is_list_continuation);
}
fn open_block(output: &mut String, options: &ConversionOptions, ctx: &Context) -> bool {
let is_table_continuation = (ctx.in_table_cell || ctx.in_layout_cell)
&& !output.is_empty()
&& !output.ends_with('|')
&& !output.ends_with("<br>")
&& !output.ends_with('\n');
let is_list_continuation =
ctx.in_list_item && !output.is_empty() && !crate::converter::list::utils::line_is_bare_list_marker(output);
let needs_leading_sep = !ctx.in_table_cell
&& !ctx.in_list_item
&& !ctx.convert_as_inline
&& !output.is_empty()
&& !output.ends_with("\n\n");
if is_table_continuation {
crate::converter::main_helpers::emit_table_cell_break_in_context(output, options.br_in_tables, ctx);
} else if is_list_continuation {
crate::converter::list::utils::start_block_in_list_item(output, ctx, options);
} else if needs_leading_sep {
trim_trailing_whitespace(output);
output.push_str("\n\n");
}
is_list_continuation
}
fn close_block(output: &mut String, ctx: &Context, content_start_pos: usize, is_list_continuation: bool) {
let has_content = output.len() > content_start_pos;
if has_content {
if content_start_pos == 0 && output.starts_with('\n') && !output.starts_with("\n\n") {
output.remove(0);
}
trim_trailing_whitespace(output);
if ctx.in_table_cell {
} else if ctx.in_list_item {
if is_list_continuation {
if !output.ends_with('\n') {
output.push('\n');
}
} else if !output.ends_with("\n\n") {
if output.ends_with('\n') {
output.push('\n');
} else {
output.push_str("\n\n");
}
}
} else if !ctx.in_list_item && !ctx.convert_as_inline {
if output.ends_with("\n\n") {
} else if output.ends_with('\n') {
output.push('\n');
} else {
output.push_str("\n\n");
}
}
}
}