diff options
| author | Albert Krewinkel <[email protected]> | 2025-12-11 19:57:54 +0100 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2025-12-11 19:57:54 +0100 |
| commit | 47ada83bcb74e1007838cc125709539c5663906b (patch) | |
| tree | 5f43007255b91357eee814cf46196f5c9afb4a25 | |
| parent | 20973b12cce4521fce462440d8f732fc40a631a0 (diff) | |
JATS writer: fix XML output for nested figures
Subfigures are now wrapped inside a `<fig-group>` element. Furthermore,
figure content that isn't allowed as children of `<fig>` elements, such
as raw text, gets wrapped in `<p>` elements to ensure schema-conform
XML production.
Fixes: #11342
| -rw-r--r-- | src/Text/Pandoc/Writers/JATS.hs | 36 | ||||
| -rw-r--r-- | test/command/11342.md | 33 |
2 files changed, 63 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Writers/JATS.hs b/src/Text/Pandoc/Writers/JATS.hs index d7975bff3..2ac1497f3 100644 --- a/src/Text/Pandoc/Writers/JATS.hs +++ b/src/Text/Pandoc/Writers/JATS.hs @@ -439,12 +439,36 @@ blockToJATS opts (Figure (ident, _, kvs) (Caption _short longcapt) body) = do capt <- if null longcapt then pure empty else inTagsSimple "caption" <$> blocksToJATS opts longcapt - figbod <- blocksToJATS opts $ walk unsetAltIfDupl body - let figattr = [("id", escapeNCName ident) | not (T.null ident)] ++ - [(k,v) | (k,v) <- kvs - , k `elem` [ "fig-type", "orientation" - , "position", "specific-use"]] - return $ inTags True "fig" figattr $ capt $$ figbod + + -- We handle the element specially if it's a figure with subfigures, i.e., if + -- all immediate children are figures themselves. + let hasSubfigures = all (\case { Figure{} -> True; _ -> False}) body + needsWrapping = if hasSubfigures + then (const False) + else \case + -- Wrap all figure content elements, except for those + -- allowed as direct subelements. + BlockQuote{} -> False + CodeBlock{} -> False + Para{} -> False + Plain [Image{}] -> False + Plain [Math{}] -> False + Table{} -> False + _ -> True + + children <- wrappedBlocksToJATS needsWrapping opts $ walk unsetAltIfDupl body + let (tag, allowedAttributes) = + if hasSubfigures + then ( "fig-group" + , ["content-type", "orientation", "position", "specific-use"] + ) + else ("fig" + , ["fig-type", "orientation", "position", "specific-use"] + ) + let xmlattr = [("id", escapeNCName ident) | not (T.null ident)] ++ + [(k,v) | (k,v) <- kvs + , k `elem` allowedAttributes] + return $ inTags True tag xmlattr $ capt $$ children -- | Convert a list of inline elements to JATS. inlinesToJATS :: PandocMonad m => WriterOptions -> [Inline] -> JATS m (Doc Text) diff --git a/test/command/11342.md b/test/command/11342.md new file mode 100644 index 000000000..7e6df30ee --- /dev/null +++ b/test/command/11342.md @@ -0,0 +1,33 @@ +Subfigures should be converted to *fig-group* elements. + +``` +% pandoc -f latex -t jats +\begin{figure}[H] + \begin{subfigure} + \centering + \includegraphics[height=1.5in]{assets/A.png} + \caption{Graph for function *A*.} + \label{fig:A} + \end{subfigure} + \begin{subfigure} + \centering + \includegraphics[height=1.5in]{assets/B.png} + \caption{Graph for function *B*.} + \label{fig:B} + \end{subfigure} + \label{fig:graphs} + \caption{Function graphs.} +\end{figure} +^D +<fig-group id="figU003Agraphs"> + <caption><p>Function graphs.</p></caption> + <fig id="figU003AA"> + <caption><p>Graph for function *A*.</p></caption> + <graphic mimetype="image" mime-subtype="png" xlink:href="assets/A.png" /> + </fig> + <fig id="figU003AB"> + <caption><p>Graph for function *B*.</p></caption> + <graphic mimetype="image" mime-subtype="png" xlink:href="assets/B.png" /> + </fig> +</fig-group> +```
\ No newline at end of file |
