diff --git a/src/core/src/converter.rs b/src/core/src/converter.rs index 88013df..4f87c3e 100644 --- a/src/core/src/converter.rs +++ b/src/core/src/converter.rs @@ -122,7 +122,52 @@ impl ContentVisitor for ContentConverter<'_> { } fn visit_space(&mut self, content: &Content) -> Node { - Node::Array(Vec::new()) + let sequence: Vec<_> = self.parent.as_ref().unwrap().to_sequence().unwrap().collect(); + let left = sequence.get(self.position.unwrap() - 1); + let right = sequence.get(self.position.unwrap() + 1); + + fn induced_space(elem: &Content) -> Option { + if elem.is_text() { + let text = elem.to_text().text(); + if text.chars().count() == 1 { + match text.as_str() { + "|" => { + let node = katex::KernBuilder::default() + .mode(katex::Mode::Math) + .dimension(katex::Measurement { + number: 5f32, + unit: "mu".to_string(), + }).build().unwrap().into_node(); + return Some(Node::Node(node)); + } + _ => return None + } + } + if text.chars().count() > 1 { + let node = katex::SpacingBuilder::default() + .mode(katex::Mode::Math) + .text("\\ ".to_string()) + .build().unwrap().into_node(); + return Some(Node::Node(node)); + } + } + None + } + + if let Some(elem) = right { + if elem.is_linebreak() || elem.is_align_point() { + return Node::Array(Vec::new()); + } + if let Some(space) = induced_space(elem) { + return space; + } + } + if let Some(elem) = left { + if let Some(space) = induced_space(elem) { + return space; + } + } + return Node::Array(Vec::new()); } fn visit_text(&mut self, content: &Content) -> Node { @@ -523,7 +568,7 @@ impl<'a> SequenceConverter<'a> { pub fn process_sequence_elements(&mut self, visitor: &mut ContentConverter) { let sequence = self.content.to_sequence().unwrap(); - for elem in sequence { + for (i, elem) in sequence.enumerate() { if elem.is_linebreak() || elem.is_align_point() { self.dump_stack_onto_body(); if elem.is_linebreak() { @@ -533,6 +578,8 @@ impl<'a> SequenceConverter<'a> { self.is_aligned = true; } } + visitor.parent = Some(self.content.clone()); + visitor.position = Some(i); let node = elem.accept(visitor); self.stack.push(node); }