{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Lua.Module.MediaBag
( documentedModule
) where
import Prelude hiding (lookup)
import Data.Maybe (fromMaybe)
import HsLua ( LuaE, DocumentedFunction, Module (..)
, (<#>), (###), (=#>), (=?>), defun, functionResult
, optionalParameter , parameter)
import Text.Pandoc.Class.CommonState (CommonState (..))
import Text.Pandoc.Class.PandocMonad (fetchItem, getMediaBag, modifyCommonState,
setMediaBag)
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.Marshal.List (pushPandocList)
import Text.Pandoc.Lua.Orphans ()
import Text.Pandoc.Lua.PandocLua (unPandocLua)
import Text.Pandoc.MIME (MimeType)
import qualified Data.ByteString.Lazy as BL
import qualified HsLua as Lua
import qualified Text.Pandoc.MediaBag as MB
documentedModule :: Module PandocError
documentedModule :: Module PandocError
documentedModule = Module :: forall e.
Name
-> MimeType
-> [Field e]
-> [DocumentedFunction e]
-> [(Operation, DocumentedFunction e)]
-> Module e
Module
{ moduleName :: Name
moduleName = Name
"pandoc.mediabag"
, moduleDescription :: MimeType
moduleDescription = MimeType
"mediabag access"
, moduleFields :: [Field PandocError]
moduleFields = []
, moduleFunctions :: [DocumentedFunction PandocError]
moduleFunctions =
[ DocumentedFunction PandocError
delete
, DocumentedFunction PandocError
empty
, DocumentedFunction PandocError
fetch
, DocumentedFunction PandocError
insert
, DocumentedFunction PandocError
items
, DocumentedFunction PandocError
list
, DocumentedFunction PandocError
lookup
]
, moduleOperations :: [(Operation, DocumentedFunction PandocError)]
moduleOperations = []
}
delete :: DocumentedFunction PandocError
delete :: DocumentedFunction PandocError
delete = Name
-> (FilePath -> LuaE PandocError ())
-> HsFnPrecursor PandocError (FilePath -> LuaE PandocError ())
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"delete"
### (\fp -> unPandocLua $ modifyCommonState
(\st -> st { stMediaBag = MB.deleteMedia fp (stMediaBag st) }))
HsFnPrecursor PandocError (FilePath -> LuaE PandocError ())
-> Parameter PandocError FilePath
-> HsFnPrecursor PandocError (LuaE PandocError ())
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError FilePath
-> MimeType
-> MimeType
-> MimeType
-> Parameter PandocError FilePath
forall e a.
Peeker e a -> MimeType -> MimeType -> MimeType -> Parameter e a
parameter Peeker PandocError FilePath
forall e. Peeker e FilePath
Lua.peekString MimeType
"string" MimeType
"filepath" MimeType
"filename of item to delete"
HsFnPrecursor PandocError (LuaE PandocError ())
-> FunctionResults PandocError () -> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> []
empty :: DocumentedFunction PandocError
empty :: DocumentedFunction PandocError
empty = Name
-> LuaE PandocError ()
-> HsFnPrecursor PandocError (LuaE PandocError ())
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"empty"
### unPandocLua (modifyCommonState (\st -> st { stMediaBag = mempty }))
HsFnPrecursor PandocError (LuaE PandocError ())
-> FunctionResults PandocError () -> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> []
insert :: DocumentedFunction PandocError
insert :: DocumentedFunction PandocError
insert = Name
-> (FilePath
-> Maybe MimeType -> ByteString -> LuaE PandocError NumResults)
-> HsFnPrecursor
PandocError
(FilePath
-> Maybe MimeType -> ByteString -> LuaE PandocError NumResults)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"insert"
### (\fp mmime contents -> unPandocLua $ do
mb <- getMediaBag
setMediaBag $ MB.insertMedia fp mmime contents mb
return (Lua.NumResults 0))
HsFnPrecursor
PandocError
(FilePath
-> Maybe MimeType -> ByteString -> LuaE PandocError NumResults)
-> Parameter PandocError FilePath
-> HsFnPrecursor
PandocError
(Maybe MimeType -> ByteString -> LuaE PandocError NumResults)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError FilePath
-> MimeType
-> MimeType
-> MimeType
-> Parameter PandocError FilePath
forall e a.
Peeker e a -> MimeType -> MimeType -> MimeType -> Parameter e a
parameter Peeker PandocError FilePath
forall e. Peeker e FilePath
Lua.peekString MimeType
"string" MimeType
"filepath" MimeType
"item file path"
HsFnPrecursor
PandocError
(Maybe MimeType -> ByteString -> LuaE PandocError NumResults)
-> Parameter PandocError (Maybe MimeType)
-> HsFnPrecursor
PandocError (ByteString -> LuaE PandocError NumResults)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError MimeType
-> MimeType
-> MimeType
-> MimeType
-> Parameter PandocError (Maybe MimeType)
forall e a.
Peeker e a
-> MimeType -> MimeType -> MimeType -> Parameter e (Maybe a)
optionalParameter Peeker PandocError MimeType
forall e. Peeker e MimeType
Lua.peekText MimeType
"string" MimeType
"mimetype" MimeType
"the item's MIME type"
HsFnPrecursor
PandocError (ByteString -> LuaE PandocError NumResults)
-> Parameter PandocError ByteString
-> HsFnPrecursor PandocError (LuaE PandocError NumResults)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError ByteString
-> MimeType
-> MimeType
-> MimeType
-> Parameter PandocError ByteString
forall e a.
Peeker e a -> MimeType -> MimeType -> MimeType -> Parameter e a
parameter Peeker PandocError ByteString
forall e. Peeker e ByteString
Lua.peekLazyByteString MimeType
"string" MimeType
"contents" MimeType
"binary contents"
HsFnPrecursor PandocError (LuaE PandocError NumResults)
-> MimeType -> DocumentedFunction PandocError
forall e.
HsFnPrecursor e (LuaE e NumResults)
-> MimeType -> DocumentedFunction e
=?> MimeType
"Nothing"
items :: DocumentedFunction PandocError
items :: DocumentedFunction PandocError
items = Name
-> LuaE PandocError NumResults
-> HsFnPrecursor PandocError (LuaE PandocError NumResults)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"items"
### (do
mb <-unPandocLua getMediaBag
let pushItem (fp, mimetype, contents) = do
Lua.pushString fp
Lua.pushText mimetype
Lua.pushByteString $ BL.toStrict contents
return (Lua.NumResults 3)
Lua.pushIterator pushItem (MB.mediaItems mb))
HsFnPrecursor PandocError (LuaE PandocError NumResults)
-> MimeType -> DocumentedFunction PandocError
forall e.
HsFnPrecursor e (LuaE e NumResults)
-> MimeType -> DocumentedFunction e
=?> MimeType
"Iterator triple"
lookup :: DocumentedFunction PandocError
lookup :: DocumentedFunction PandocError
lookup = Name
-> (FilePath -> LuaE PandocError NumResults)
-> HsFnPrecursor
PandocError (FilePath -> LuaE PandocError NumResults)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"lookup"
### (\fp -> unPandocLua (MB.lookupMedia fp <$> getMediaBag) >>= \case
Nothing -> 1 <$ Lua.pushnil
Just item -> 2 <$ do
Lua.pushText $ MB.mediaMimeType item
Lua.pushLazyByteString $ MB.mediaContents item)
HsFnPrecursor PandocError (FilePath -> LuaE PandocError NumResults)
-> Parameter PandocError FilePath
-> HsFnPrecursor PandocError (LuaE PandocError NumResults)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError FilePath
-> MimeType
-> MimeType
-> MimeType
-> Parameter PandocError FilePath
forall e a.
Peeker e a -> MimeType -> MimeType -> MimeType -> Parameter e a
parameter Peeker PandocError FilePath
forall e. Peeker e FilePath
Lua.peekString MimeType
"string" MimeType
"filepath" MimeType
"path of item to lookup"
HsFnPrecursor PandocError (LuaE PandocError NumResults)
-> MimeType -> DocumentedFunction PandocError
forall e.
HsFnPrecursor e (LuaE e NumResults)
-> MimeType -> DocumentedFunction e
=?> MimeType
"MIME type and contents"
list :: DocumentedFunction PandocError
list :: DocumentedFunction PandocError
list = Name
-> LuaE PandocError [(FilePath, MimeType, Int)]
-> HsFnPrecursor
PandocError (LuaE PandocError [(FilePath, MimeType, Int)])
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"list"
### (unPandocLua (MB.mediaDirectory <$> getMediaBag))
HsFnPrecursor
PandocError (LuaE PandocError [(FilePath, MimeType, Int)])
-> FunctionResults PandocError [(FilePath, MimeType, Int)]
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError [(FilePath, MimeType, Int)]
-> MimeType
-> MimeType
-> FunctionResults PandocError [(FilePath, MimeType, Int)]
forall e a.
Pusher e a -> MimeType -> MimeType -> FunctionResults e a
functionResult (Pusher PandocError (FilePath, MimeType, Int)
-> Pusher PandocError [(FilePath, MimeType, Int)]
forall e a. LuaError e => Pusher e a -> Pusher e [a]
pushPandocList Pusher PandocError (FilePath, MimeType, Int)
pushEntry) MimeType
"table" MimeType
"list of entry triples"
where
pushEntry :: (FilePath, MimeType, Int) -> LuaE PandocError ()
pushEntry :: Pusher PandocError (FilePath, MimeType, Int)
pushEntry (FilePath
fp, MimeType
mimeType, Int
contentLength) = do
LuaE PandocError ()
forall e. LuaE e ()
Lua.newtable
Name -> LuaE PandocError ()
forall e. Name -> LuaE e ()
Lua.pushName Name
"path" LuaE PandocError () -> LuaE PandocError () -> LuaE PandocError ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> FilePath -> LuaE PandocError ()
forall e. FilePath -> LuaE e ()
Lua.pushString FilePath
fp LuaE PandocError () -> LuaE PandocError () -> LuaE PandocError ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> StackIndex -> LuaE PandocError ()
forall e. LuaError e => StackIndex -> LuaE e ()
Lua.rawset (-StackIndex
3)
Name -> LuaE PandocError ()
forall e. Name -> LuaE e ()
Lua.pushName Name
"type" LuaE PandocError () -> LuaE PandocError () -> LuaE PandocError ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Pusher PandocError MimeType
forall e. Pusher e MimeType
Lua.pushText MimeType
mimeType LuaE PandocError () -> LuaE PandocError () -> LuaE PandocError ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> StackIndex -> LuaE PandocError ()
forall e. LuaError e => StackIndex -> LuaE e ()
Lua.rawset (-StackIndex
3)
Name -> LuaE PandocError ()
forall e. Name -> LuaE e ()
Lua.pushName Name
"length" LuaE PandocError () -> LuaE PandocError () -> LuaE PandocError ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> LuaE PandocError ()
forall a e. (Integral a, Show a) => a -> LuaE e ()
Lua.pushIntegral Int
contentLength LuaE PandocError () -> LuaE PandocError () -> LuaE PandocError ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> StackIndex -> LuaE PandocError ()
forall e. LuaError e => StackIndex -> LuaE e ()
Lua.rawset (-StackIndex
3)
fetch :: DocumentedFunction PandocError
fetch :: DocumentedFunction PandocError
fetch = Name
-> (MimeType -> LuaE PandocError NumResults)
-> HsFnPrecursor
PandocError (MimeType -> LuaE PandocError NumResults)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"fetch"
### (\src -> do
(bs, mimeType) <- unPandocLua $ fetchItem src
Lua.pushText $ fromMaybe "" mimeType
Lua.pushByteString bs
return 2)
HsFnPrecursor PandocError (MimeType -> LuaE PandocError NumResults)
-> Parameter PandocError MimeType
-> HsFnPrecursor PandocError (LuaE PandocError NumResults)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError MimeType
-> MimeType
-> MimeType
-> MimeType
-> Parameter PandocError MimeType
forall e a.
Peeker e a -> MimeType -> MimeType -> MimeType -> Parameter e a
parameter Peeker PandocError MimeType
forall e. Peeker e MimeType
Lua.peekText MimeType
"string" MimeType
"src" MimeType
"URI to fetch"
HsFnPrecursor PandocError (LuaE PandocError NumResults)
-> MimeType -> DocumentedFunction PandocError
forall e.
HsFnPrecursor e (LuaE e NumResults)
-> MimeType -> DocumentedFunction e
=?> MimeType
"Returns two string values: the fetched contents and the mimetype."