use crate::converter::Context;
use crate::converter::inline::HandlerContext;
use crate::converter::main_helpers::strip_trailing_backslash_breaks_from_fresh_buffer;
#[cfg(feature = "visitor")]
use crate::converter::utility::serialization::serialize_node_to_html;
#[cfg(feature = "visitor")]
use std::borrow::Cow;
pub fn open_blockquote(
tag: &tl::HTMLTag,
handler: &HandlerContext<'_>,
children: &mut Vec<tl::NodeHandle>,
) -> Option<crate::converter::walk::Open> {
use crate::converter::walk::{CaptureEnd, Close, Open};
children.extend(tag.children().top().iter().copied());
if handler.context.convert_as_inline && (!handler.context.in_heading || handler.context.in_table_cell) {
return Some(Open::children_only());
}
let follows_markers = handler.output.is_empty() && handler.context.in_marker_text();
let context = if handler.context.in_heading && !handler.context.in_table_cell {
handler.context.clone()
} else {
Context {
blockquote_depth: handler.context.blockquote_depth + 1,
in_list_item: false,
in_list: false,
list_indent_columns: 0,
real_item_columns: 0,
inline_buffer_column: None,
inline_depth: if follows_markers {
handler.context.inline_depth
} else {
0
},
quote_starts_after_markers: follows_markers,
item_lines: crate::converter::list::utils::ItemLineScan::new_item(),
..handler.context.clone()
}
};
Some(Open {
scope: Some(std::rc::Rc::new(context)),
close: Close::Capture(CaptureEnd::Blockquote),
})
}
pub fn close_blockquote(content: &str, mut handler: HandlerContext<'_>) {
let Some(tl::Node::Tag(tag)) = handler.node_handle.get(handler.parser) else {
return;
};
let mut content = content.to_owned();
strip_trailing_backslash_breaks_from_fresh_buffer(&mut content, handler.options.newline_style);
if handler.context.in_heading && !handler.context.in_table_cell {
let trimmed = content.trim();
if trimmed.is_empty() {
return;
}
#[cfg(feature = "visitor")]
if visit_blockquote(tag, trimmed, &mut handler) {
return;
}
if !handler.output.is_empty() && !handler.output.ends_with(char::is_whitespace) {
handler.output.push(' ');
}
handler.output.push_str(trimmed);
handler.output.push(' ');
return;
}
let cite = crate::converter::utility::attributes::decoded_attribute(tag, "cite")
.filter(|_| handler.options.include_blockquote_citations)
.map(std::borrow::Cow::into_owned)
.map(|value| {
handler
.context
.resolve_url(&value, handler.node_handle, handler.parser, handler.dom_context)
.unwrap_or(value)
});
let trimmed = container_content(tag, handler.parser, &content, handler.options.code_block_style);
#[cfg(feature = "visitor")]
if visit_blockquote(tag, trimmed, &mut handler) {
return;
}
if handler.context.in_table_cell {
render_table_cell_quote(trimmed, cite.as_deref(), &mut handler);
} else {
render_quote(trimmed, cite.as_deref(), &mut handler);
}
}
pub fn container_content<'p, 'a, 'c>(
tag: &'p tl::HTMLTag<'a>,
parser: &'p tl::Parser<'a>,
content: &'c str,
style: crate::options::CodeBlockStyle,
) -> &'c str {
let kept = crate::converter::code_scan::trimmed_quotes_content_range(content, style);
let start = if starts_with_code_block(tag, parser) {
kept.start
} else {
content.len() - content.trim_start().len()
};
&content[start..kept.end.max(start)]
}
fn starts_with_code_block<'p, 'a>(tag: &'p tl::HTMLTag<'a>, parser: &'p tl::Parser<'a>) -> bool {
let mut current = tag;
loop {
let first = current
.children()
.top()
.iter()
.find_map(|child| match child.get(parser) {
Some(tl::Node::Tag(element)) => Some(Some(element)),
Some(tl::Node::Raw(text)) if !text.as_utf8_str().trim().is_empty() => Some(None),
_ => None,
});
match first.flatten() {
Some(element) if element.name().as_bytes().eq_ignore_ascii_case(b"pre") => return true,
Some(element) => current = element,
None => return false,
}
}
}
#[cfg(feature = "visitor")]
fn visit_blockquote(tag: &tl::HTMLTag<'_>, content: &str, handler: &mut HandlerContext<'_>) -> bool {
use crate::visitor::{NodeContext, NodeType, VisitResult};
let Some(visitor) = handler.context.visitor.as_ref() else {
return false;
};
let node_id = handler.node_handle.get_inner();
let node_context = NodeContext::with_lazy_attributes(
NodeType::Blockquote,
Cow::Borrowed("blockquote"),
tag,
handler.depth,
handler.dom_context.get_sibling_index(node_id).unwrap_or(0),
handler
.dom_context
.parent_tag_name(node_id, handler.parser)
.map(Cow::Borrowed),
false,
);
let result = visitor.lock().expect("visitor mutex poisoned").visit_blockquote(
&node_context,
content,
handler.context.blockquote_depth,
);
match result {
VisitResult::Continue => return false,
VisitResult::Custom(custom) => handler.output.push_str(&custom),
VisitResult::Skip => {}
VisitResult::PreserveHtml => serialize_node_to_html(handler.node_handle, handler.parser, handler.output),
VisitResult::Error(error) => {
if handler.context.visitor_error.borrow().is_none() {
*handler.context.visitor_error.borrow_mut() = Some(error);
}
}
}
true
}
fn render_table_cell_quote(content: &str, cite: Option<&str>, handler: &mut HandlerContext<'_>) {
if content.is_empty() {
return;
}
if handler.context.in_code {
if !handler.output.is_empty() && !handler.output.ends_with('\n') {
handler.output.push('\n');
}
} else {
crate::converter::main_helpers::separate_block_in_cell(handler.output, handler.options.br_in_tables);
}
handler.output.push_str(content);
if handler.context.in_code {
handler.output.push('\n');
}
if let Some(url) = cite {
crate::converter::main_helpers::separate_block_in_cell(handler.output, handler.options.br_in_tables);
handler.output.push_str("— <");
handler.output.push_str(url);
handler.output.push('>');
}
}
fn render_quote(content: &str, cite: Option<&str>, handler: &mut HandlerContext<'_>) {
if content.is_empty() {
return;
}
let list_indent = handler.context.in_list_item.then(|| {
crate::converter::list::utils::continuation_indent_string(
crate::converter::list::utils::block_columns(handler.context, handler.options),
handler.options,
)
});
let list_indent = list_indent.flatten();
let continuation = list_indent.is_some()
&& !handler.output.is_empty()
&& !crate::converter::list::utils::line_is_bare_list_marker(handler.output);
separate_before_quote(handler.output, handler.context);
emit_quote_lines(
content,
list_indent.as_deref(),
continuation,
handler.options.code_block_style,
handler.output,
);
if let Some(url) = cite {
handler.output.push('\n');
if let Some(indent) = list_indent.as_deref() {
handler.output.push_str(indent);
}
handler.output.push_str("— <");
handler.output.push_str(url);
handler.output.push_str(">\n\n");
}
if !handler.context.in_list_item {
while handler.output.ends_with('\n') {
handler.output.truncate(handler.output.len() - 1);
}
handler.output.push_str("\n\n");
}
}
fn separate_before_quote(output: &mut String, context: &Context) {
if context.in_list_item && crate::converter::list::utils::trim_whitespace_after_bare_marker(output) {
return;
}
if context.blockquote_depth > 0 && !context.in_list_item {
if !output.is_empty() {
while output.ends_with('\n') {
output.truncate(output.len() - 1);
}
output.push_str("\n\n");
}
return;
}
if output.is_empty() {
return;
}
if context.in_list_item {
crate::converter::trim_trailing_whitespace(output);
}
if output.ends_with("\n\n") {
output.truncate(output.len() - 1);
} else if context.in_list_item {
if !output.ends_with('\n') {
output.push('\n');
}
} else if !output.ends_with('\n') {
output.push_str("\n\n");
} else if !output.ends_with("\n\n") {
output.push('\n');
}
}
fn emit_quote_lines(
content: &str,
indent: Option<&str>,
continuation: bool,
style: crate::options::CodeBlockStyle,
output: &mut String,
) {
let mut code = crate::converter::code_scan::CodeScan::of_quote_content(style, content);
for (index, line) in content.lines().enumerate() {
if (index > 0 || continuation) && indent.is_some() {
output.push_str(indent.unwrap_or_default());
}
let is_space = !line.is_empty() && line.trim().is_empty();
let is_code = if is_space {
code.is_code(line)
} else {
code.pass(line);
false
};
if line.is_empty() || (is_space && !is_code) {
output.push('>');
} else {
output.push_str("> ");
output.push_str(line);
}
output.push('\n');
}
}