use crate::converter::inline::{HandlerContext, wrapped::InlineSite, wrapped::emit_code_span};
use crate::text;
type Context = crate::converter::Context;
pub fn handle(tag_name: &str, context: HandlerContext<'_>) {
match tag_name {
"kbd" | "samp" => handle_kbd_samp(context),
_ => {}
}
}
fn handle_kbd_samp(mut handler: HandlerContext<'_>) {
use crate::converter::{append_inline_suffix, chomp_inline};
let site = handler.inline_site();
let Some(node) = handler.node_handle.get(handler.parser) else {
return;
};
let tag = match node {
tl::Node::Tag(tag) => tag,
_ => return,
};
let children = tag.children();
if handler.context.in_code {
for child_handle in children.top().iter() {
walk_child(child_handle, handler.output, handler.context, handler.depth, site);
}
return;
}
let mut content = String::with_capacity(32);
let code_ctx = Context {
in_code: true,
..handler.context.clone()
};
for child_handle in children.top().iter() {
walk_child(child_handle, &mut content, &code_ctx, handler.depth, site);
}
let normalized = text::normalize_whitespace(&content);
let (prefix, suffix, trimmed) = chomp_inline(&normalized);
if content.is_empty() {
return;
}
let body = if trimmed.is_empty() {
normalized.as_str()
} else {
trimmed
};
let emit_prefix = if trimmed.is_empty() { "" } else { prefix };
let emit_suffix = if trimmed.is_empty() { "" } else { suffix };
handler.output.push_str(emit_prefix);
emit_kbd_samp_segments(body, emit_prefix.is_empty(), &mut handler);
append_inline_suffix(
handler.output,
emit_suffix,
!body.is_empty(),
handler.node_handle,
handler.parser,
handler.dom_context,
);
}
fn walk_child(
child_handle: &tl::NodeHandle,
output: &mut String,
context: &Context,
depth: usize,
site: InlineSite<'_>,
) {
crate::converter::walk_node(
child_handle,
site.parser,
output,
crate::converter::block::container::HandlerContext::new(site.options, context, depth + 1, site.dom_ctx),
);
}
fn emit_kbd_samp_segments(body: &str, may_merge_first: bool, handler: &mut HandlerContext<'_>) {
let separator = crate::converter::main_helpers::hard_break_marker(handler.options);
let mut first = true;
for segment in body.split('\n').filter(|segment| !segment.is_empty()) {
if first {
let mut span = String::with_capacity(segment.len() + 2);
crate::converter::handlers::code_block::format_inline_code(segment, &mut span);
if may_merge_first {
emit_code_span(
&span,
segment,
handler.output,
handler.node_handle,
handler.parser,
handler.dom_context,
);
} else {
handler.output.push_str(&span);
}
} else {
handler.output.push_str(separator);
crate::converter::handlers::code_block::format_inline_code(segment, handler.output);
}
first = false;
}
}