use cmark_writer::ast::{CustomNodeWriter, Node};
use cmark_writer::custom_node;
use cmark_writer::WriteResult;
use ecow::EcoString;
use std::path::PathBuf;
use crate::Result;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListState {
Ordered,
Unordered,
}
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
#[default]
Md,
LaTeX,
#[cfg(feature = "docx")]
Docx,
}
#[derive(Debug, PartialEq, Clone)]
#[custom_node]
pub struct FigureNode {
pub body: Box<Node>,
pub caption: String,
}
impl FigureNode {
fn write_custom(&self, writer: &mut dyn CustomNodeWriter) -> WriteResult<()> {
let mut temp_writer = cmark_writer::writer::CommonMarkWriter::new();
temp_writer.write(&self.body)?;
let content = temp_writer.into_string();
writer.write_str(&content)?;
writer.write_str("\n")?;
writer.write_str(&self.caption)?;
Ok(())
}
fn is_block_custom(&self) -> bool {
true
}
}
#[derive(Debug, PartialEq, Clone)]
#[custom_node]
pub struct ExternalFrameNode {
pub file_path: PathBuf,
pub alt_text: String,
pub svg_data: String,
}
impl ExternalFrameNode {
fn write_custom(&self, writer: &mut dyn CustomNodeWriter) -> WriteResult<()> {
writer.write_str(&format!(
"",
self.alt_text,
self.file_path.display()
))?;
Ok(())
}
fn is_block_custom(&self) -> bool {
true
}
}
#[derive(Debug, PartialEq, Clone)]
#[custom_node]
pub struct HighlightNode {
pub content: Vec<Node>,
}
impl HighlightNode {
fn write_custom(&self, writer: &mut dyn CustomNodeWriter) -> WriteResult<()> {
let mut temp_writer = cmark_writer::writer::CommonMarkWriter::new();
for node in &self.content {
temp_writer.write(node)?;
}
let content = temp_writer.into_string();
writer.write_str(&format!("=={}==", content))?;
Ok(())
}
fn is_block_custom(&self) -> bool {
false
}
}
pub trait FormatWriter {
fn write_eco(&mut self, document: &Node, output: &mut EcoString) -> Result<()>;
fn write_vec(&mut self, document: &Node) -> Result<Vec<u8>>;
}