Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typst writer: add #box around image to make it inline. #9149

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/Text/Pandoc/Writers/Typst.hs
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions test/writer.typst
Original file line number Diff line number Diff line change
@@ -779,13 +779,13 @@ or here: <http://example.com/>
<images>
From "Voyage dans la Lune" by Georges Melies \(1902):

#figure([#image("lalune.jpg")],
#figure([#box(width: 150.0pt, image("lalune.jpg"))],
caption: [
lalune
]
)

Here is a movie #image("movie.jpg") icon.
Here is a movie #box(width: 20.0pt, image("movie.jpg")) icon.

#horizontalrule