diff options
| author | John MacFarlane <[email protected]> | 2023-10-20 12:18:46 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-20 12:18:46 -0700 |
| commit | a67223c44219c5a6b2154a06dffedecdd26e925e (patch) | |
| tree | 8704193154020eefffa76994bd9cec2538e9c840 /src | |
| parent | 5e668118751b50aaf7f6ab675dd5dc628ced30bc (diff) | |
Typst writer: add `#box` around image to make it inline. (#9149)
An `#image` by itself in typst is a block-level element.
To force images to be inline (as they are in pandoc), we need
to add a box with an explicit width. When a width is not given
in image attributes, we compute one from the image itself, when
possible.
Closes #9104, supersedes #9105.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index f8cc651de..d66897fba 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -17,7 +17,8 @@ module Text.Pandoc.Writers.Typst ( writeTypst ) where import Text.Pandoc.Definition -import Text.Pandoc.Class.PandocMonad ( PandocMonad ) +import Text.Pandoc.Class ( PandocMonad, fetchItem ) +import Text.Pandoc.ImageSize (imageSize, sizeInPoints) import Text.Pandoc.Options ( WriterOptions(..), WrapOption(..), isEnabled ) import Data.Text (Text) import Data.List (intercalate, intersperse) @@ -30,6 +31,7 @@ import Text.Pandoc.Writers.Math (convertMath) import qualified Text.TeXMath as TM import Text.DocLayout import Text.DocTemplates (renderTemplate) +import Control.Monad.Except (catchError) import Text.Pandoc.Extensions (Extension(..)) import Text.Collate.Lang (Lang(..), parseLang) @@ -280,10 +282,31 @@ inlineToTypst inline = then mempty else nowrap $ brackets contents Image (_,_,kvs) _inlines (src,_tit) -> do - let width' = maybe mempty ((", width: " <>) . literal) $ lookup "width" kvs - let height' = maybe mempty ((", height: " <>) . literal) $ - lookup "height" kvs - return $ "#image(" <> doubleQuoted src <> width' <> height' <> ")" + opts <- gets stOptions + let mbHeight = lookup "height" kvs + let mdWidth = lookup "width" kvs + let coreImage = "image" <> + parens (doubleQuoted src <> + maybe mempty (\w -> ", width: " <> literal w) mdWidth <> + maybe mempty (\h -> ", height: " <> literal h) mbHeight) + -- see #9104; we need a box or the image is treated as block-level: + case (mdWidth, mbHeight) of + (Nothing, Nothing) -> do + realWidth <- catchError + (do (bs, _mt) <- fetchItem src + case imageSize opts bs of + Right x -> pure $ Just $ T.pack $ + show (fst (sizeInPoints x)) <> "pt" + Left _ -> pure Nothing) + (\_ -> pure Nothing) + case realWidth of + Just w -> return $ "#box" <> + parens ("width: " <> literal w <> ", " <> coreImage) + Nothing -> return $ "#" <> coreImage + (Just w, _) -> return $ "#box" <> + parens ("width: " <> literal w <> ", " <> coreImage) + (_, Just h) -> return $ "#box" <> + parens ("height: " <> literal h <> ", " <> coreImage) Note blocks -> do contents <- blocksToTypst blocks return $ "#footnote" <> brackets (chomp contents) |
