summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2024-03-17 11:29:25 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2024-03-17 11:29:25 -0700
commita6392207fc560fff2d136132ecd6219b7e5f6bfd (patch)
tree68d805dc18cee645a300cf6497640776f0e79726
parent9ae7557ac0ac27ee63e8b32aec2bab53b008f72a (diff)
Typst writer: avoid unnecessary box around image in figure.
See #9236.
-rw-r--r--src/Text/Pandoc/Writers/Typst.hs35
-rw-r--r--test/command/9236.md36
-rw-r--r--test/writer.typst2
3 files changed, 60 insertions, 13 deletions
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs
index ce2f2d8f0..9f3c9a0bf 100644
--- a/src/Text/Pandoc/Writers/Typst.hs
+++ b/src/Text/Pandoc/Writers/Typst.hs
@@ -192,13 +192,19 @@ blockToTypst block =
$$ blankline
Figure (ident,_,_) (Caption _mbshort capt) blocks -> do
caption <- blocksToTypst capt
- contents <- blocksToTypst blocks
+ contents <- case blocks of
+ -- don't need #box around block-level image
+ [Para [Image (_,_,kvs) _ (src, _)]]
+ -> pure $ mkImage False src kvs
+ [Plain [Image (_,_,kvs) _ (src, _)]]
+ -> pure $ mkImage False src kvs
+ _ -> brackets <$> blocksToTypst blocks
let lab = toLabel FreestandingLabel ident
let kind = case blocks of
Table{}:_ -> "table"
CodeBlock{}:_ -> "code"
_ -> "auto"
- return $ "#figure(" <> nest 2 ((brackets contents <> ",")
+ return $ "#figure(" <> nest 2 ((contents <> ",")
$$
("caption: [" $$ nest 2 caption $$ "],")
$$
@@ -304,20 +310,25 @@ inlineToTypst inline =
(if inlines == [Str src]
then mempty
else nowrap $ brackets contents) <> endCode
- Image (_,_,kvs) _inlines (src,_tit) -> do
- let src' = T.pack $ unEscapeString $ T.unpack src -- #9389
- let toDimAttr k =
- case lookup k kvs of
- Just v -> ", " <> literal k <> ": " <> literal v
- Nothing -> mempty
- let dimAttrs = mconcat $ map toDimAttr ["height", "width"]
- pure $ "#box" <> -- see #9104; need box or image is treated as block-level
- parens ("image" <>
- parens (doubleQuoted src' <> dimAttrs))
+ Image (_,_,kvs) _inlines (src,_tit) -> pure $ mkImage True src kvs
Note blocks -> do
contents <- blocksToTypst blocks
return $ "#footnote" <> brackets (chomp contents) <> endCode
+-- see #9104; need box or image is treated as block-level
+mkImage :: Bool -> Text -> [(Text, Text)] -> Doc Text
+mkImage useBox src kvs
+ | useBox = "#box" <> parens coreImage
+ | otherwise = coreImage
+ where
+ src' = T.pack $ unEscapeString $ T.unpack src -- #9389
+ toDimAttr k =
+ case lookup k kvs of
+ Just v -> ", " <> literal k <> ": " <> literal v
+ Nothing -> mempty
+ dimAttrs = mconcat $ map toDimAttr ["height", "width"]
+ coreImage = "image" <> parens (doubleQuoted src' <> dimAttrs)
+
textstyle :: PandocMonad m => Doc Text -> [Inline] -> TW m (Doc Text)
textstyle s inlines =
(<> endCode) . (s <>) . brackets <$> inlinesToTypst inlines
diff --git a/test/command/9236.md b/test/command/9236.md
new file mode 100644
index 000000000..4536f0b09
--- /dev/null
+++ b/test/command/9236.md
@@ -0,0 +1,36 @@
+```
+% pandoc -t typst
+![minimal](command/minimal.svg){width=3in}
+
+![minimal](command/minimal.svg){width=3in height=2in}
+
+![minimal](command/minimal.svg)
+
+And inline: ![minimal](command/minimal.svg){height=2in} and
+![minimal](command/minimal.svg).
+^D
+#figure(image("command/minimal.svg", width: 3in),
+ caption: [
+ minimal
+ ],
+ kind: auto
+)
+
+#figure(image("command/minimal.svg", height: 2in, width: 3in),
+ caption: [
+ minimal
+ ],
+ kind: auto
+)
+
+#figure(image("command/minimal.svg"),
+ caption: [
+ minimal
+ ],
+ kind: auto
+)
+
+And inline: #box(image("command/minimal.svg", height: 2in)) and
+#box(image("command/minimal.svg")).
+
+```
diff --git a/test/writer.typst b/test/writer.typst
index 6fcf3abe5..02151f4e6 100644
--- a/test/writer.typst
+++ b/test/writer.typst
@@ -784,7 +784,7 @@ or here: <http://example.com/>
<images>
From "Voyage dans la Lune" by Georges Melies (1902):
-#figure([#box(image("lalune.jpg"))],
+#figure(image("lalune.jpg"),
caption: [
lalune
],