use crate::converter::inline::HandlerContext;
use crate::converter::walk::{CaptureEnd, Close, Open};
use crate::options::OutputFormat;
#[cfg(feature = "visitor")]
use std::borrow::Cow;
use std::rc::Rc;
type Context = crate::converter::Context;
use crate::converter::inline::wrapped::{
EMPHASIS_SIBLING_TAGS, InlineDelimiters, InlineSite, STRONG_SIBLING_TAGS, block_runs_are_plain,
block_runs_are_single_line, emit_first_block_wrapped, emit_wrapped_inline, wrap_block_runs,
};
pub fn emit_strong_wrapped(output: &mut String, content: &str, site: InlineSite<'_>) {
let options = site.options;
let ctx = site.ctx;
let marker = if ctx.in_strong {
String::new()
} else if options.output_format == OutputFormat::Djot {
String::from("*")
} else {
[options.strong_em_symbol; 2].iter().collect()
};
if emit_first_block_wrapped(output, content, &marker, &marker, ctx, site.parser) {
return;
}
if !marker.is_empty() && content.contains("\n\n") && block_runs_are_plain(content) {
output.push_str(&wrap_block_runs(content, &marker, &marker));
return;
}
if ctx.in_strong {
emit_wrapped_inline(
output,
content,
&InlineDelimiters {
open: "",
close: "",
merge_symbol: Some(options.strong_em_symbol),
sibling_tag_names: &STRONG_SIBLING_TAGS,
},
InlineSite {
node_handle: site.node_handle,
parser: site.parser,
dom_ctx: site.dom_ctx,
ctx,
options,
},
);
} else if options.output_format == OutputFormat::Djot {
emit_wrapped_inline(
output,
content,
&InlineDelimiters {
open: "*",
close: "*",
merge_symbol: Some('*'),
sibling_tag_names: &STRONG_SIBLING_TAGS,
},
InlineSite {
node_handle: site.node_handle,
parser: site.parser,
dom_ctx: site.dom_ctx,
ctx,
options,
},
);
} else {
emit_wrapped_inline(
output,
content,
&InlineDelimiters {
open: &marker,
close: &marker,
merge_symbol: Some(options.strong_em_symbol),
sibling_tag_names: &STRONG_SIBLING_TAGS,
},
InlineSite {
node_handle: site.node_handle,
parser: site.parser,
dom_ctx: site.dom_ctx,
ctx,
options,
},
);
}
}
pub fn emit_strong_wrapped_blocks(output: &mut String, content: &str, site: InlineSite<'_>) {
let options = site.options;
let ctx = site.ctx;
let marker = if ctx.in_strong {
String::new()
} else if options.output_format == OutputFormat::Djot {
String::from("*")
} else {
[options.strong_em_symbol; 2].iter().collect()
};
if !marker.is_empty() && content.contains("\n\n") && block_runs_are_single_line(content) {
output.push_str(&wrap_block_runs(content, &marker, &marker));
} else {
emit_strong_wrapped(output, content, site);
}
}
fn emit_emphasis_wrapped(output: &mut String, content: &str, site: InlineSite<'_>) {
let options = site.options;
let marker = if options.output_format == OutputFormat::Djot {
String::from("_")
} else {
options.strong_em_symbol.to_string()
};
if emit_first_block_wrapped(output, content, &marker, &marker, site.ctx, site.parser) {
return;
}
if content.contains("\n\n") && block_runs_are_plain(content) {
output.push_str(&wrap_block_runs(content, &marker, &marker));
return;
}
if options.output_format == OutputFormat::Djot {
emit_wrapped_inline(
output,
content,
&InlineDelimiters {
open: "_",
close: "_",
merge_symbol: Some('_'),
sibling_tag_names: &EMPHASIS_SIBLING_TAGS,
},
InlineSite {
node_handle: site.node_handle,
parser: site.parser,
dom_ctx: site.dom_ctx,
ctx: site.ctx,
options,
},
);
} else {
emit_wrapped_inline(
output,
content,
&InlineDelimiters {
open: &marker,
close: &marker,
merge_symbol: Some(options.strong_em_symbol),
sibling_tag_names: &EMPHASIS_SIBLING_TAGS,
},
InlineSite {
node_handle: site.node_handle,
parser: site.parser,
dom_ctx: site.dom_ctx,
ctx: site.ctx,
options,
},
);
}
}
#[derive(Clone, Copy)]
pub enum EmphasisKind {
Strong,
Emphasis,
}
pub fn open(
tag_name: &str,
node_handle: &crate::dom::NodeHandle,
parser: &crate::dom::Parser,
output: &str,
context: &Context,
children: &mut Vec<crate::dom::NodeHandle>,
) -> Option<Open> {
let kind = match tag_name {
"strong" | "b" => EmphasisKind::Strong,
_ => EmphasisKind::Emphasis,
};
let Some(crate::dom::Node::Tag(tag)) = node_handle.get(parser) else {
return None;
};
children.extend(tag.children().top().iter().copied());
if context.in_code {
return Some(Open::children_only());
}
let buffer_context = context.inline_buffer(output, true);
let child_context = Context {
inline_depth: context.inline_depth + 1,
in_strong: context.in_strong || matches!(kind, EmphasisKind::Strong),
inline_buffer_after_hard_break: buffer_context.inline_buffer_after_hard_break,
..context.clone()
};
Some(Open {
scope: Some(Rc::new(child_context)),
close: Close::Capture(CaptureEnd::Emphasis(kind)),
})
}
pub fn close(kind: EmphasisKind, content: &str, handler: HandlerContext<'_>) {
let Some(crate::dom::Node::Tag(tag)) = handler.node_handle.get(handler.parser) else {
return;
};
#[cfg(feature = "visitor")]
if let Some(custom_output) = visit_emphasis(tag, kind, &handler) {
handler.output.push_str(&custom_output);
return;
}
let site = handler.inline_site();
match kind {
EmphasisKind::Strong => emit_strong_wrapped(handler.output, content, site),
EmphasisKind::Emphasis => {
emit_emphasis_wrapped(handler.output, content, site);
maybe_emit_caret(handler.output, content, tag);
}
}
}
#[cfg(feature = "visitor")]
fn visit_emphasis(tag: &crate::dom::HTMLTag<'_>, kind: EmphasisKind, handler: &HandlerContext<'_>) -> Option<String> {
use crate::converter::{get_text_content, serialize_node};
use crate::visitor::{NodeContext, NodeType, VisitResult};
let visitor_handle = handler.context.visitor.as_ref()?;
let text_content = get_text_content(handler.node_handle, handler.parser, handler.dom_context);
let node_id = handler.node_handle.get_inner();
let node_context = NodeContext::with_lazy_attributes(
match kind {
EmphasisKind::Strong => NodeType::Strong,
EmphasisKind::Emphasis => NodeType::Em,
},
tag.name().as_utf8_str(),
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),
true,
);
let result = {
let mut visitor = visitor_handle.lock().expect("visitor mutex poisoned");
match kind {
EmphasisKind::Strong => visitor.visit_strong(&node_context, &text_content),
EmphasisKind::Emphasis => visitor.visit_emphasis(&node_context, &text_content),
}
};
if handler.options.whitespace_mode == crate::options::WhitespaceMode::Strict
&& (matches!(&result, VisitResult::Skip)
|| matches!(&result, VisitResult::Custom(custom) if custom.trim_matches([' ', '\t']).is_empty()))
{
handler.dom_context.record_absent_breaks(node_id, true);
}
match result {
VisitResult::Continue => None,
VisitResult::Custom(custom) => {
crate::converter::structure_capture::replace_element(handler.context, Some(&custom));
Some(custom)
}
VisitResult::Skip => {
crate::converter::structure_capture::replace_element(handler.context, None);
Some(String::new())
}
VisitResult::PreserveHtml => {
let html = serialize_node(handler.node_handle, handler.parser);
crate::converter::structure_capture::replace_element(handler.context, Some(&html));
Some(html)
}
VisitResult::Error(error) => {
crate::converter::structure_capture::replace_element(handler.context, None);
if handler.context.visitor_error.borrow().is_none() {
*handler.context.visitor_error.borrow_mut() = Some(error);
}
None
}
}
}
fn maybe_emit_caret(output: &mut String, content: &str, tag: &crate::dom::HTMLTag) {
if !content.is_empty() {
return;
}
if let Some(class_value) = tag
.attributes()
.get("class")
.and_then(|v| v.as_ref().map(|val| val.as_utf8_str()))
{
if class_value.contains("caret") && !output.ends_with(' ') {
output.push_str(" > ");
}
}
}