diff --git a/crates/typlite/src/fixtures/integration/figure_raw.typ b/crates/typlite/src/fixtures/integration/figure_raw.typ new file mode 100644 index 000000000..db8b7598c --- /dev/null +++ b/crates/typlite/src/fixtures/integration/figure_raw.typ @@ -0,0 +1,5 @@ +#figure( + ```md + markdown + ```, +) diff --git a/crates/typlite/src/fixtures/integration/snaps/convert@figure_raw.typ.snap b/crates/typlite/src/fixtures/integration/snaps/convert@figure_raw.typ.snap new file mode 100644 index 000000000..2c59a1131 --- /dev/null +++ b/crates/typlite/src/fixtures/integration/snaps/convert@figure_raw.typ.snap @@ -0,0 +1,17 @@ +--- +source: crates/typlite/src/tests.rs +expression: "conv(world, ConvKind::Md { for_docs: false })" +input_file: crates/typlite/src/fixtures/integration/figure_raw.typ +--- + + + + + + + + + +===== +

markdown
+

diff --git a/crates/typlite/src/fixtures/integration/snaps/convert@ieee.typ.snap b/crates/typlite/src/fixtures/integration/snaps/convert@ieee.typ.snap index 8d8f9ed46..b1964f224 100644 --- a/crates/typlite/src/fixtures/integration/snaps/convert@ieee.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/convert@ieee.typ.snap @@ -2,7 +2,6 @@ source: crates/typlite/src/tests.rs expression: "conv(world, ConvKind::Md { for_docs: false })" input_file: crates/typlite/src/fixtures/integration/ieee.typ -snapshot_kind: text --- @@ -43,18 +42,48 @@ In Figure 1 you can see a common representation of the Sun, which is a star tha Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur. Quod idem licet transferre in voluptatem, ut postea variari voluptas distinguique possit, augeri amplificarique non possit. At etiam Athenis, ut e patre audiebam facete et urbane Stoicos irridente, statua est in quo a nobis philosophia defensa et collaudata est, cum id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum. -| Planet | Distance (million km) | -| --- | --- | -| Mercury | 57.9 | -| Venus | 108.2 | -| Earth | 149.6 | -| Mars | 227.9 | -| Jupiter | 778.6 | -| Saturn | 1,433.5 | -| Uranus | 2,872.5 | -| Neptune | 4,495.1 | - -

+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PlanetDistance (million km)
Mercury57.9
Venus108.2
Earth149.6
Mars227.9
Jupiter778.6
Saturn1,433.5
Uranus2,872.5
Neptune4,495.1

In Table 1, you see the planets of the solar system and their average distance from the Sun. The distances were calculated with Equation 1 that we presented in Section II. diff --git a/crates/typlite/src/fixtures/integration/snaps/convert_tex@figure_raw.typ.snap b/crates/typlite/src/fixtures/integration/snaps/convert_tex@figure_raw.typ.snap new file mode 100644 index 000000000..ed03f4e67 --- /dev/null +++ b/crates/typlite/src/fixtures/integration/snaps/convert_tex@figure_raw.typ.snap @@ -0,0 +1,25 @@ +--- +source: crates/typlite/src/tests.rs +expression: "conv(world, ConvKind::LaTeX)" +input_file: crates/typlite/src/fixtures/integration/figure_raw.typ +--- + + + + + + + + + +===== +\begin{center} +\begin{figure}[htbp] +\centering +\begin{lstlisting}[language=md] +markdown +\end{lstlisting} + +\end{figure} + +\end{center} diff --git a/crates/typlite/src/fixtures/integration/snaps/convert_tex@ieee.typ.snap b/crates/typlite/src/fixtures/integration/snaps/convert_tex@ieee.typ.snap index 706aeb0d2..f0054195d 100644 --- a/crates/typlite/src/fixtures/integration/snaps/convert_tex@ieee.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/convert_tex@ieee.typ.snap @@ -48,6 +48,9 @@ In Figure 1 you can see a common representation of the Sun, which is a star tha Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim aeque doleamus animo, cum corpore dolemus, fieri tamen permagna accessio potest, si aliquod aeternum et infinitum impendere malum nobis opinemur. Quod idem licet transferre in voluptatem, ut postea variari voluptas distinguique possit, augeri amplificarique non possit. At etiam Athenis, ut e patre audiebam facete et urbane Stoicos irridente, statua est in quo a nobis philosophia defensa et collaudata est, cum id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum. +\begin{center} +\begin{figure}[htbp] +\centering \begin{table}[htbp] \centering \begin{tabular}{cc} @@ -66,9 +69,6 @@ Neptune & 4,495.1 \\ \end{tabular} \end{table} -\begin{center} -\begin{figure}[htbp] -\centering \caption{The Planets of the Solar System and Their Average Distance from the Sun} \end{figure} diff --git a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_caption.typ.snap b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_caption.typ.snap index 9ac394ca0..5173ae058 100644 --- a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_caption.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_caption.typ.snap @@ -3,4 +3,4 @@ source: crates/typlite/src/tests.rs expression: hash input_file: crates/typlite/src/fixtures/integration/figure_caption.typ --- -siphash128_13:57521e02b1a93b78c212f39755cf70f +siphash128_13:894254b508d111b30f77342c90adc20f diff --git a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image.typ.snap b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image.typ.snap index 03902f5e2..96d8c9666 100644 --- a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image.typ.snap @@ -3,4 +3,4 @@ source: crates/typlite/src/tests.rs expression: hash input_file: crates/typlite/src/fixtures/integration/figure_image.typ --- -siphash128_13:57521e02b1a93b78c212f39755cf70f +siphash128_13:72f9bab3dff17389f9c18b2fc156f55a diff --git a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image_alt.typ.snap b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image_alt.typ.snap index 44f633794..29dfe98f6 100644 --- a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image_alt.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_image_alt.typ.snap @@ -3,4 +3,4 @@ source: crates/typlite/src/tests.rs expression: hash input_file: crates/typlite/src/fixtures/integration/figure_image_alt.typ --- -siphash128_13:57521e02b1a93b78c212f39755cf70f +siphash128_13:72f9bab3dff17389f9c18b2fc156f55a diff --git a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_raw.typ.snap b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_raw.typ.snap new file mode 100644 index 000000000..90d9fe06b --- /dev/null +++ b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@figure_raw.typ.snap @@ -0,0 +1,6 @@ +--- +source: crates/typlite/src/tests.rs +expression: hash +input_file: crates/typlite/src/fixtures/integration/figure_raw.typ +--- +siphash128_13:30232e8f06f64239089a80f0f753d388 diff --git a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@ieee.typ.snap b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@ieee.typ.snap index 9998164cb..030316e22 100644 --- a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@ieee.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@ieee.typ.snap @@ -2,6 +2,5 @@ source: crates/typlite/src/tests.rs expression: hash input_file: crates/typlite/src/fixtures/integration/ieee.typ -snapshot_kind: text --- -siphash128_13:914f20b049c227a73ad350294a7d79b3 +siphash128_13:47f5f496ccb3e39b89162536a33cc4de diff --git a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@outline.typ.snap b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@outline.typ.snap index bd258a0b1..927e510e2 100644 --- a/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@outline.typ.snap +++ b/crates/typlite/src/fixtures/integration/snaps/docx_generation_hash@outline.typ.snap @@ -3,4 +3,4 @@ source: crates/typlite/src/tests.rs expression: hash input_file: crates/typlite/src/fixtures/integration/outline.typ --- -siphash128_13:60d37f36c1e381dc70a08fa1bca48b73 +siphash128_13:7230fb7127284a0103c63b402e7eca68 diff --git a/crates/typlite/src/parser/core.rs b/crates/typlite/src/parser/core.rs index 914e8b5c2..0a5f1ca6d 100644 --- a/crates/typlite/src/parser/core.rs +++ b/crates/typlite/src/parser/core.rs @@ -274,6 +274,22 @@ impl HtmlToAstParser { self.inline_buffer = prev_buffer; Ok(()) } + + /// Convert element children while capturing both inline and block outputs. + pub fn capture_children(&mut self, element: &HtmlElement) -> Result<(Vec, Vec)> { + let prev_buffer = std::mem::take(&mut self.inline_buffer); + let prev_blocks = std::mem::take(&mut self.blocks); + + self.convert_children(element)?; + + let inline = std::mem::take(&mut self.inline_buffer); + let blocks = std::mem::take(&mut self.blocks); + + self.inline_buffer = prev_buffer; + self.blocks = prev_blocks; + + Ok((inline, blocks)) + } } #[derive(Debug, Clone)] diff --git a/crates/typlite/src/parser/inline.rs b/crates/typlite/src/parser/inline.rs index 40630ec98..97fe8ba6f 100644 --- a/crates/typlite/src/parser/inline.rs +++ b/crates/typlite/src/parser/inline.rs @@ -75,10 +75,21 @@ impl HtmlToAstParser { let attrs = FigureAttr::parse(&element.attrs)?; let caption = attrs.caption.to_string(); - // Find image and body content - let mut body_content = Vec::new(); - self.convert_children_into(&mut body_content, element)?; - let body = Box::new(Node::Paragraph(body_content)); + let (inline_content, mut block_content) = self.capture_children(element)?; + + let mut content_nodes = Vec::new(); + if !inline_content.is_empty() { + content_nodes.push(Node::Paragraph(inline_content)); + } + content_nodes.append(&mut block_content); + + let body = if content_nodes.is_empty() { + Box::new(Node::Paragraph(Vec::new())) + } else if content_nodes.len() == 1 { + Box::new(content_nodes.into_iter().next().unwrap()) + } else { + Box::new(Node::Document(content_nodes)) + }; // Create figure node with centering let figure_node = Box::new(FigureNode { body, caption }); diff --git a/crates/typlite/src/writer/docx/writer.rs b/crates/typlite/src/writer/docx/writer.rs index 3e911488f..0d29f8c1b 100644 --- a/crates/typlite/src/writer/docx/writer.rs +++ b/crates/typlite/src/writer/docx/writer.rs @@ -448,6 +448,17 @@ impl DocxWriter { para.property = para.property.clone().align(AlignmentType::Center); } } + Node::HtmlElement(element) => { + let start_idx = docx.document.children.len(); + for child in &element.children { + docx = self.process_node(docx, child)?; + } + for child in docx.document.children.iter_mut().skip(start_idx) { + if let DocumentChild::Paragraph(para) = child { + para.property = para.property.clone().align(AlignmentType::Center); + } + } + } other => { docx = self.process_node(docx, other)?; // Get the last element and center it if it's a paragraph