aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2022-11-02 09:23:09 -0700
committerJohn MacFarlane <[email protected]>2022-11-02 09:23:09 -0700
commitb3f4a66ec8a1d4c903ce3b1b7ad761545d7de290 (patch)
treed3ae6e9d47c8fe3d5ef1179f6f9513be98c093b3
parent0c641c1025eebbeab476bbc3b462b9cc780bb286 (diff)
ipynb reader: add cell id to attachment filename...
...when storing in the MediaBag. Otherwise attachments with the same name can overwrite each other. Closes #8415.
-rw-r--r--src/Text/Pandoc/Readers/Ipynb.hs23
-rw-r--r--test/ipynb/simple.out.native2
2 files changed, 14 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Readers/Ipynb.hs b/src/Text/Pandoc/Readers/Ipynb.hs
index a82877242..7385cc4ba 100644
--- a/src/Text/Pandoc/Readers/Ipynb.hs
+++ b/src/Text/Pandoc/Readers/Ipynb.hs
@@ -81,13 +81,13 @@ cellToBlocks opts lang c = do
Nothing -> mempty
Just (MimeAttachments m) -> M.toList m
let ident = fromMaybe mempty $ cellId c
- mapM_ addAttachment attachments
+ mapM_ (addAttachment (cellId c)) attachments
case cellType c of
Ipynb.Markdown -> do
bs <- if isEnabled Ext_raw_markdown opts
then return [RawBlock (Format "markdown") source]
else do
- Pandoc _ bs <- walk fixImage <$> readMarkdown opts source
+ Pandoc _ bs <- walk (fixImage (cellId c)) <$> readMarkdown opts source
return bs
return $ B.divWith (ident,["cell","markdown"],kvs)
$ B.fromList bs
@@ -121,14 +121,17 @@ cellToBlocks opts lang c = do
<> outputBlocks
-- Remove attachment: prefix from images...
-fixImage :: Inline -> Inline
-fixImage (Image attr lab (src,tit))
- | "attachment:" `T.isPrefixOf` src = Image attr lab (T.drop 11 src, tit)
-fixImage x = x
-
-addAttachment :: PandocMonad m => (Text, MimeBundle) -> m ()
-addAttachment (fname, mimeBundle) = do
- let fp = T.unpack fname
+fixImage :: Maybe Text -> Inline -> Inline
+fixImage mbident (Image attr lab (src,tit))
+ | "attachment:" `T.isPrefixOf` src =
+ let src' = T.drop 11 src
+ qualifiedSrc = maybe src' (<> ("-" <> src')) mbident
+ in Image attr lab (qualifiedSrc, tit)
+fixImage _ x = x
+
+addAttachment :: PandocMonad m => Maybe Text -> (Text, MimeBundle) -> m ()
+addAttachment mbident (fname, mimeBundle) = do
+ let fp = T.unpack $ maybe fname (<> ("-" <> fname)) mbident
case M.toList (unMimeBundle mimeBundle) of
(mimeType, BinaryData bs):_ ->
insertMedia fp (Just mimeType) (BL.fromStrict bs)
diff --git a/test/ipynb/simple.out.native b/test/ipynb/simple.out.native
index a17c36a41..1c7e979cc 100644
--- a/test/ipynb/simple.out.native
+++ b/test/ipynb/simple.out.native
@@ -83,7 +83,7 @@ Pandoc
, Image
( "" , [] , [] )
[ Str "the" , Space , Str "moon" ]
- ( "lalune.jpg" , "" )
+ ( "uid6-lalune.jpg" , "" )
, Space
, Str "will"
, Space