use super::walk_node;
use std::borrow::Cow;
pub fn handle_form(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
if ctx.convert_as_inline {
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, output, options, ctx, depth, dom_ctx);
}
}
return;
}
let mut content = String::new();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, &mut content, options, ctx, depth, dom_ctx);
}
}
let trimmed = content.trim();
if !trimmed.is_empty() {
if !output.is_empty() && !output.ends_with("\n\n") {
output.push_str("\n\n");
}
output.push_str(trimmed);
output.push_str("\n\n");
}
}
}
pub fn handle_fieldset(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
if ctx.convert_as_inline {
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, output, options, ctx, depth, dom_ctx);
}
}
return;
}
let mut content = String::new();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, &mut content, options, ctx, depth, dom_ctx);
}
}
let trimmed = content.trim();
if !trimmed.is_empty() {
if !output.is_empty() && !output.ends_with("\n\n") {
output.push_str("\n\n");
}
output.push_str(trimmed);
output.push_str("\n\n");
}
}
}
pub fn handle_legend(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let mut content = String::new();
let mut legend_ctx = ctx.clone();
if !ctx.convert_as_inline {
legend_ctx.in_strong = true;
}
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(
child_handle,
parser,
&mut content,
options,
&legend_ctx,
depth + 1,
dom_ctx,
);
}
}
let trimmed = content.trim();
if !trimmed.is_empty() {
if ctx.convert_as_inline {
output.push_str(trimmed);
} else {
let mut symbol = String::with_capacity(2);
symbol.push(options.strong_em_symbol);
symbol.push(options.strong_em_symbol);
output.push_str(&symbol);
output.push_str(trimmed);
output.push_str(&symbol);
output.push_str("\n\n");
}
}
}
}
pub fn handle_label(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let mut content = String::new();
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, &mut content, options, ctx, depth + 1, dom_ctx);
}
}
let trimmed = content.trim();
if !trimmed.is_empty() {
output.push_str(trimmed);
if !ctx.convert_as_inline {
output.push_str("\n\n");
}
}
}
}
pub fn handle_input(
_tag_name: &str,
_node_handle: &tl::NodeHandle,
_parser: &tl::Parser,
_output: &mut String,
_options: &crate::options::ConversionOptions,
_ctx: &super::Context,
_depth: usize,
_dom_ctx: &super::DomContext,
) {
}
pub fn handle_textarea(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push_str("\n\n");
}
}
}
pub fn handle_select(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push('\n');
}
}
}
pub fn handle_option(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let selected = tag.attributes().iter().any(|(name, _)| name.as_ref() == "selected");
let mut text = String::new();
let children = tag.children();
{
for child_handle in children.top().iter() {
super::walk_node(child_handle, parser, &mut text, options, ctx, depth + 1, dom_ctx);
}
}
let trimmed = text.trim();
if !trimmed.is_empty() {
if selected && !ctx.convert_as_inline {
output.push_str("* ");
}
output.push_str(trimmed);
if !ctx.convert_as_inline {
output.push('\n');
}
}
}
}
pub fn handle_optgroup(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let label = tag
.attributes()
.get("label")
.flatten()
.map_or(Cow::Borrowed(""), |v| v.as_utf8_str());
if !label.is_empty() {
let mut symbol = String::with_capacity(2);
symbol.push(options.strong_em_symbol);
symbol.push(options.strong_em_symbol);
output.push_str(&symbol);
output.push_str(&label);
output.push_str(&symbol);
output.push('\n');
}
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
}
}
pub fn handle_button(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push_str("\n\n");
}
}
}
pub fn handle_progress(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push_str("\n\n");
}
}
}
pub fn handle_meter(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push_str("\n\n");
}
}
}
pub fn handle_output(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push_str("\n\n");
}
}
}
pub fn handle_datalist(
_tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
let start_len = output.len();
let children = tag.children();
{
for child_handle in children.top().iter() {
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
}
}
if !ctx.convert_as_inline && output.len() > start_len {
output.push('\n');
}
}
}
pub fn handle(
tag_name: &str,
node_handle: &tl::NodeHandle,
parser: &tl::Parser,
output: &mut String,
options: &crate::options::ConversionOptions,
ctx: &super::Context,
depth: usize,
dom_ctx: &super::DomContext,
) {
match tag_name {
"form" => handle_form(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"fieldset" => handle_fieldset(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"legend" => handle_legend(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"label" => handle_label(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"input" => handle_input(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"textarea" => handle_textarea(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"select" => handle_select(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"option" => handle_option(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"optgroup" => handle_optgroup(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"button" => handle_button(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"progress" => handle_progress(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"meter" => handle_meter(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"output" => handle_output(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
"datalist" => handle_datalist(tag_name, node_handle, parser, output, options, ctx, depth, dom_ctx),
_ => {}
}
}