Skip to content

Commit

Permalink
Make the code more perspicuous.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Oct 20, 2023
1 parent b70e3e2 commit 4a4cfb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
35 changes: 19 additions & 16 deletions src/Text/Pandoc/Writers/Typst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -282,24 +282,27 @@ inlineToTypst inline =
then mempty
else nowrap $ brackets contents
Image (_,_,kvs) _inlines (src,_tit) -> do
opts <- gets stOptions
let height' = lookup "height" kvs
width' <- case lookup "width" kvs of
Just w -> pure $ Just w
Nothing -> 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)
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) width' <>
maybe mempty (\h -> ", height: " <> literal h) height')
case (width', height') of
(Nothing, Nothing) -> return $ "#" <> coreImage
-- see #9104; we need a box or the image is treated as block-level:
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" <>
Expand Down
4 changes: 2 additions & 2 deletions test/writer.typst
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,13 @@ or here: <http://example.com/>
<images>
From "Voyage dans la Lune" by Georges Melies \(1902):

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

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

#horizontalrule

Expand Down

0 comments on commit 4a4cfb1

Please sign in to comment.