use crate::converter::utility::content::chomp_inline;
use crate::converter::walk::{CaptureEnd, Close, Open};
use std::rc::Rc;
pub fn handle_dfn(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
handler: super::HandlerContext<'_>,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let mut content = String::with_capacity(32);
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(
child_handle,
parser,
&mut content,
crate::converter::block::container::HandlerContext::new(
handler.options,
handler.ctx,
handler.depth + 1,
handler.dom_ctx,
),
);
}
}
let (prefix, suffix, trimmed) = chomp_inline(&content);
if !trimmed.is_empty() {
output.push_str(prefix);
output.push(handler.options.strong_em_symbol);
output.push_str(trimmed);
output.push(handler.options.strong_em_symbol);
append_inline_suffix(
output,
suffix,
!trimmed.is_empty(),
node_handle,
parser,
handler.dom_ctx,
);
}
}
}
pub fn handle_abbr(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
handler: super::HandlerContext<'_>,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let mut content = String::with_capacity(32);
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(
child_handle,
parser,
&mut content,
crate::converter::block::container::HandlerContext::new(
handler.options,
handler.ctx,
handler.depth + 1,
handler.dom_ctx,
),
);
}
}
let trimmed = content.trim();
if !trimmed.is_empty() {
output.push_str(trimmed);
if handler.options.expand_abbreviations
&& let Some(title) = crate::converter::utility::attributes::decoded_attribute(tag, "title")
{
let trimmed_title = title.trim();
if !trimmed_title.is_empty() {
output.push_str(" (");
output.push_str(trimmed_title);
output.push(')');
}
}
}
}
}
pub fn handle_time_data(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
handler: super::HandlerContext<'_>,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(
child_handle,
parser,
output,
crate::converter::block::container::HandlerContext::new(
handler.options,
handler.ctx,
handler.depth + 1,
handler.dom_ctx,
),
);
}
}
}
}
pub fn handle_cite(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
handler: super::HandlerContext<'_>,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let mut content = String::with_capacity(32);
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(
child_handle,
parser,
&mut content,
crate::converter::block::container::HandlerContext::new(
handler.options,
handler.ctx,
handler.depth + 1,
handler.dom_ctx,
),
);
}
}
let trimmed = content.trim();
if !trimmed.is_empty() {
if handler.ctx.convert_as_inline {
output.push_str(trimmed);
} else {
output.push('*');
output.push_str(trimmed);
output.push('*');
}
}
}
}
pub fn open_q(
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
handler: super::HandlerContext<'_>,
children: &mut Vec<tl::NodeHandle>,
) -> Option<Open> {
let Some(tl::Node::Tag(tag)) = node_handle.get(parser) else {
return None;
};
children.extend(tag.children().top().iter().copied());
if let Some(links) = handler.ctx.inline_code_links.as_ref() {
let end = CollectedQuoteEnd {
link_count: links.borrow().len(),
start: output.len(),
};
output.push('"');
return Some(Open {
scope: None,
close: Close::CollectedQuote(end),
});
}
let q_ctx = super::Context {
text_in_markers: true,
..handler.ctx.clone()
};
Some(Open {
scope: Some(Rc::new(q_ctx)),
close: Close::Capture(CaptureEnd::Quote),
})
}
pub fn close_q(content: &str, output: &mut String, handler: super::HandlerContext<'_>) {
let (prefix, suffix, trimmed) =
if crate::converter::utility::content::keeps_source_white_space(handler.options, handler.ctx) {
("", "", content.trim())
} else {
chomp_inline(content)
};
if trimmed.is_empty() {
if !content.is_empty() {
crate::converter::inline::wrapped::emit_whitespace_only_inline_body(content, output);
}
return;
}
if !handler.ctx.convert_as_inline && content.contains("\n\n") {
let quoted = format!("\"{trimmed}\"");
crate::converter::block::div::push_block(output, handler.options, handler.ctx, "ed);
return;
}
crate::converter::utility::content::push_inline_prefix(output, prefix, handler.options, handler.ctx);
output.push('"');
output.push_str(trimmed);
output.push('"');
output.push_str(suffix);
}
pub struct CollectedQuoteEnd {
link_count: usize,
start: usize,
}
pub fn close_collected_q(end: &CollectedQuoteEnd, output: &mut String, handler: super::HandlerContext<'_>) {
let Some(links) = handler.ctx.inline_code_links.as_ref() else {
return;
};
let CollectedQuoteEnd { link_count, start } = *end;
if links.borrow().len() == link_count {
let body = output[start + 1..].to_string();
output.truncate(start);
if body.trim().is_empty() {
crate::converter::inline::wrapped::emit_whitespace_only_inline_body(&body, output);
} else {
output.push('"');
output.push_str(body.trim());
output.push('"');
}
} else {
output.push('"');
}
}
pub fn handle(
tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
handler: super::HandlerContext<'_>,
) {
match tag_name {
"dfn" => handle_dfn(tag_name, node_handle, parser, output, handler),
"abbr" => handle_abbr(tag_name, node_handle, parser, output, handler),
"time" | "data" => handle_time_data(tag_name, node_handle, parser, output, handler),
"cite" => handle_cite(tag_name, node_handle, parser, output, handler),
_ => {}
}
}
fn append_inline_suffix(
output: &mut String,
suffix: &str,
_is_nonempty: bool,
_node_handle: &tl::NodeHandle,
_parser: &tl::Parser,
_dom_ctx: &super::DomContext,
) {
output.push_str(suffix);
}