use cmark_writer::{
custom_node, CommonMarkWriter, HtmlWriteResult, HtmlWriter, HtmlWriterOptions, Node,
WriteResult,
};
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = false, html_impl = true)]
struct ColoredTextWithHtmlImpl {
text: String,
color: String,
}
impl ColoredTextWithHtmlImpl {
fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
writer.write_str("<span style=\"color: ")?;
writer.write_str(&self.color)?;
writer.write_str("\">")?;
writer.write_str(&self.text)?;
writer.write_str("</span>")?;
Ok(())
}
fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
writer.start_tag("span")?;
writer.attribute("style", &format!("color: {}", self.color))?;
writer.finish_tag()?;
writer.text(&self.text)?;
writer.end_tag("span")?;
Ok(())
}
}
#[derive(Debug, PartialEq, Clone)]
#[custom_node(block = false)]
struct ColoredTextWithoutHtmlImpl {
text: String,
color: String,
}
impl ColoredTextWithoutHtmlImpl {
fn write_custom(&self, writer: &mut CommonMarkWriter) -> WriteResult<()> {
writer.write_str("<span style=\"color: ")?;
writer.write_str(&self.color)?;
writer.write_str("\">")?;
writer.write_str(&self.text)?;
writer.write_str("</span>")?;
Ok(())
}
}
#[test]
fn test_html_impl_parameter() {
let colored_with_impl = ColoredTextWithHtmlImpl {
text: "Hello, custom HTML!".to_string(),
color: "#ff0000".to_string(),
};
let mut html_writer = HtmlWriter::new();
html_writer
.write_node(&Node::Custom(Box::new(colored_with_impl)))
.unwrap();
let custom_html_output = html_writer.into_string();
assert!(custom_html_output.contains("<span style=\"color: #ff0000\">"));
assert!(custom_html_output.contains("Hello, custom HTML!"));
assert!(custom_html_output.contains("</span>"));
let colored_without_impl = ColoredTextWithoutHtmlImpl {
text: "Hello, default HTML!".to_string(),
color: "#00ff00".to_string(),
};
let mut html_writer = HtmlWriter::new();
html_writer
.write_node(&Node::Custom(Box::new(colored_without_impl)))
.unwrap();
let default_html_output = html_writer.into_string();
println!("Actual output: {}", default_html_output);
assert!(default_html_output.contains(
"<!-- HTML rendering not implemented for Custom Node: html_impl_test::ColoredTextWithoutHtmlImpl -->"
));
}
#[test]
fn test_html_writer_options() {
let options = HtmlWriterOptions {
strict: true,
code_block_language_class_prefix: Some("language-".to_string()),
#[cfg(feature = "gfm")]
enable_gfm: true,
#[cfg(feature = "gfm")]
gfm_disallowed_html_tags: vec!["script".to_string()],
};
let mut writer = HtmlWriter::with_options(options);
let code_block = Node::CodeBlock {
language: Some("rust".to_string()),
content: "fn main() {\n println!(\"Hello\");\n}".to_string(),
block_type: Default::default(),
};
writer.write_node(&code_block).unwrap();
let output = writer.into_string();
assert!(output.contains("class=\"language-rust\""));
}
#[test]
fn test_ensure_tag_closed() {
let mut writer = HtmlWriter::new();
writer.start_tag("div").unwrap();
let output = writer.into_string();
assert_eq!(output, "<div>");
}