Skip to content

Commit

Permalink
refactor(createPngFallbacks): store Image attributes too
Browse files Browse the repository at this point in the history
Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Dec 27, 2023
1 parent 6237700 commit ce30f8b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/Text/Pandoc/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Control.Monad.Catch ( MonadMask )
import Control.Monad.Except (throwError)
import qualified Data.ByteString.Lazy as BL
import Data.Maybe (fromMaybe, isJust, isNothing)
import Data.Map (findWithDefault)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -308,7 +309,7 @@ convertWithOpts' scriptingEngine istty datadir opts = do
)

when (format == "docx" && not (optSandbox opts)) $ do
createPngFallbacks (writerDpi writerOptions)
createPngFallbacks writerOptions

output <- case writer of
ByteStringWriter f
Expand Down Expand Up @@ -372,14 +373,16 @@ readAbbreviations mbfilepath =
>>= fmap (Set.fromList . filter (not . T.null) . T.lines) .
toTextM (fromMaybe mempty mbfilepath)

createPngFallbacks :: (PandocMonad m, MonadIO m) => Int -> m ()
createPngFallbacks dpi = do
createPngFallbacks :: (PandocMonad m, MonadIO m) => WriterOptions -> m ()
createPngFallbacks opts = do
-- create fallback pngs for svgs
items <- mediaItems <$> getMediaBag
attributes <- getImageAttrs
forM_ items $ \(fp, mt, bs) ->
case T.takeWhile (/=';') mt of
"image/svg+xml" -> do
res <- svgToPng dpi bs
let attr = Data.Map.findWithDefault nullAttr fp attributes
res <- svgToPng (writerDpi opts) bs
case res of
Right bs' -> do
let fp' = fp <> ".png"
Expand Down
5 changes: 5 additions & 0 deletions src/Text/Pandoc/Class/CommonState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import Text.Collate.Lang (Lang)
import Text.Pandoc.MediaBag (MediaBag)
import Text.Pandoc.Logging (LogMessage, Verbosity (WARNING))
import Text.Pandoc.Translations.Types (Translations)
import Text.Pandoc.Definition (Attr)
import qualified Data.Map as M

-- | 'CommonState' represents state that is used by all
-- instances of 'PandocMonad'. Normally users should not
Expand All @@ -41,6 +43,8 @@ data CommonState = CommonState
-- ^ Controls whether certificate validation is disabled
, stMediaBag :: MediaBag
-- ^ Media parsed from binary containers
, stImageAttrs :: M.Map FilePath Attr
-- ^ Image attributes
, stTranslations :: Maybe (Lang, Maybe Translations)
-- ^ Translations for localization
, stInputFiles :: [FilePath]
Expand Down Expand Up @@ -71,6 +75,7 @@ defaultCommonState = CommonState
, stRequestHeaders = []
, stNoCheckCertificate = False
, stMediaBag = mempty
, stImageAttrs = M.empty
, stTranslations = Nothing
, stInputFiles = []
, stOutputFile = Nothing
Expand Down
25 changes: 21 additions & 4 deletions src/Text/Pandoc/Class/PandocMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Text.Pandoc.Class.PandocMonad
, getLog
, setVerbosity
, getVerbosity
, getImageAttrs
, getMediaBag
, setMediaBag
, insertMedia
Expand Down Expand Up @@ -86,6 +87,7 @@ import qualified Debug.Trace
import qualified Text.Pandoc.MediaBag as MB
import qualified Data.Text.Encoding as TSE
import qualified Data.Text.Encoding.Error as TSE
import Data.Map (Map, insert)

-- | The PandocMonad typeclass contains all the potentially
-- IO-related functions used in pandoc's readers and writers.
Expand Down Expand Up @@ -202,12 +204,26 @@ setMediaBag mb = modifyCommonState $ \st -> st{stMediaBag = mb}
getMediaBag :: PandocMonad m => m MediaBag
getMediaBag = getsCommonState stMediaBag

-- | Insert an item into the media bag.
-- | Initialize the image attributes
setImageAttrs :: PandocMonad m => Map FilePath Attr -> m ()
setImageAttrs mb = modifyCommonState $ \st -> st{stImageAttrs = mb}

-- | Retrieve the image attributes
getImageAttrs :: PandocMonad m => m (Map FilePath Attr)
getImageAttrs = getsCommonState stImageAttrs

insertMedia :: PandocMonad m => FilePath -> Maybe MimeType -> BL.ByteString -> m ()
insertMedia fp mime bs = do
mb <- getMediaBag
let mb' = MB.insertMedia fp mime bs mb
setMediaBag mb'
mb <- getMediaBag
let mb' = MB.insertMedia fp mime bs mb
setMediaBag mb'

-- | Insert an item into the media bag.
insertAttr :: PandocMonad m => FilePath -> Attr -> m ()
insertAttr fp attr = do
attrs <- getImageAttrs
let attrs' = Data.Map.insert fp attr attrs
setImageAttrs attrs'

-- | Retrieve the input filenames.
getInputFiles :: PandocMonad m => m [FilePath]
Expand Down Expand Up @@ -464,6 +480,7 @@ fillMediaBag d = walkM handleImage d
Nothing -> do
(bs, mt) <- fetchItem src
insertMedia fp mt (BL.fromStrict bs)
insertAttr fp attr
return $ Image attr lab (src, tit))
(\e ->
case e of
Expand Down

0 comments on commit ce30f8b

Please sign in to comment.