module Nbparts.Pack.Sources where import Data.Ipynb qualified as Ipynb import Data.Map qualified as Map import Nbparts.Pack.Mime (embedMimeAttachments) import Nbparts.Types ( CellSource (CellSource), CellType (Code, Markdown, Raw), ) fillSources :: FilePath -> [CellSource] -> Ipynb.Notebook a -> IO (Ipynb.Notebook a) fillSources :: forall a. FilePath -> [CellSource] -> Notebook a -> IO (Notebook a) fillSources FilePath readDir [CellSource] sources (Ipynb.Notebook JSONMeta meta (Int, Int) format [Cell a] _) = do [Cell a] cells <- (CellSource -> IO (Cell a)) -> [CellSource] -> IO [Cell a] forall (t :: * -> *) (f :: * -> *) a b. (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b) forall (f :: * -> *) a b. Applicative f => (a -> f b) -> [a] -> f [b] traverse (FilePath -> CellSource -> IO (Cell a) forall a. FilePath -> CellSource -> IO (Cell a) sourceToCell FilePath readDir) [CellSource] sources Notebook a -> IO (Notebook a) forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return (Notebook a -> IO (Notebook a)) -> Notebook a -> IO (Notebook a) forall a b. (a -> b) -> a -> b $ JSONMeta -> (Int, Int) -> [Cell a] -> Notebook a forall a. JSONMeta -> (Int, Int) -> [Cell a] -> Notebook a Ipynb.Notebook JSONMeta meta (Int, Int) format [Cell a] cells sourceToCell :: FilePath -> CellSource -> IO (Ipynb.Cell a) sourceToCell :: forall a. FilePath -> CellSource -> IO (Cell a) sourceToCell FilePath readDir (CellSource Text cellId CellType cellType [Text] source Maybe UnembeddedMimeAttachments attachments) = do let cellType' :: CellType a cellType' = case CellType cellType of CellType Markdown -> CellType a forall a. CellType a Ipynb.Markdown CellType Raw -> CellType a forall a. CellType a Ipynb.Raw CellType Code -> Maybe Int -> [Output a] -> CellType a forall a. Maybe Int -> [Output a] -> CellType a Ipynb.Code Maybe Int forall a. Maybe a Nothing [] Maybe MimeAttachments attachments' <- (UnembeddedMimeAttachments -> IO MimeAttachments) -> Maybe UnembeddedMimeAttachments -> IO (Maybe MimeAttachments) forall (t :: * -> *) (f :: * -> *) a b. (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b) forall (f :: * -> *) a b. Applicative f => (a -> f b) -> Maybe a -> f (Maybe b) traverse (FilePath -> UnembeddedMimeAttachments -> IO MimeAttachments embedMimeAttachments FilePath readDir) Maybe UnembeddedMimeAttachments attachments Cell a -> IO (Cell a) forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return (Cell a -> IO (Cell a)) -> Cell a -> IO (Cell a) forall a b. (a -> b) -> a -> b $ CellType a -> Maybe Text -> Source -> JSONMeta -> Maybe MimeAttachments -> Cell a forall a. CellType a -> Maybe Text -> Source -> JSONMeta -> Maybe MimeAttachments -> Cell a Ipynb.Cell CellType a forall a. CellType a cellType' (Text -> Maybe Text forall a. a -> Maybe a Just Text cellId) ([Text] -> Source Ipynb.Source [Text] source) (Map Text Value -> JSONMeta Ipynb.JSONMeta Map Text Value forall k a. Map k a Map.empty) Maybe MimeAttachments attachments'