git.iko.soy/typeable/pandoc
on master help sign in
Use hslua utils where possible

Some helper functions and types have been moved to hslua. Change: minor

commit856bc54526fc01b48a2d770406fcb9aaa2fa5da3 authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 4 Jan 2018, 19:05 (9 years ago) parentbdb9115
4 files changed+16−33
src/Text/Pandoc/Lua/Module/MediaBag.hs+5−5 src/Text/Pandoc/Lua/Module/Pandoc.hs+8−9 src/Text/Pandoc/Lua/Module/Utils.hs+3−3 src/Text/Pandoc/Lua/Util.hs−16
src/Text/Pandoc/Lua/Module/MediaBag.hs+5−5
@@ -32,11 +32,11 @@ module Text.Pandoc.Lua.Module.MediaBag
3232import Control.Monad (zipWithM_)
3333import Data.IORef (IORef, modifyIORef', readIORef)
3434import Data.Maybe (fromMaybe)
35−import Foreign.Lua (Lua, NumResults, liftIO)
35+import Foreign.Lua (Lua, NumResults, Optional, liftIO)
3636import Text.Pandoc.Class (CommonState (..), fetchItem, putCommonState,
3737 runIOorExplode, setMediaBag)
3838import Text.Pandoc.Lua.StackInstances ()
39−import Text.Pandoc.Lua.Util (OrNil (toMaybe), addFunction)
39+import Text.Pandoc.Lua.Util (addFunction)
4040import Text.Pandoc.MIME (MimeType)
4141
4242import qualified Data.ByteString.Lazy as BL
@@ -57,12 +57,12 @@ pushModule commonState mediaBagRef = do
5757
5858insertMediaFn :: IORef MB.MediaBag
5959 -> FilePath
60− -> OrNil MimeType
60+ -> Optional MimeType
6161 -> BL.ByteString
6262 -> Lua NumResults
63−insertMediaFn mbRef fp nilOrMime contents = do
63+insertMediaFn mbRef fp optionalMime contents = do
6464 liftIO . modifyIORef' mbRef $
65− MB.insertMedia fp (toMaybe nilOrMime) contents
65+ MB.insertMedia fp (Lua.fromOptional optionalMime) contents
6666 return 0
6767
6868lookupMediaFn :: IORef MB.MediaBag
src/Text/Pandoc/Lua/Module/Pandoc.hs+8−9
@@ -34,14 +34,13 @@ import Control.Monad (when)
3434import Data.Default (Default (..))
3535import Data.Maybe (fromMaybe)
3636import Data.Text (pack)
37−import Foreign.Lua (ToLuaStack, FromLuaStack, Lua, NumResults, liftIO)
37+import Foreign.Lua (ToLuaStack, FromLuaStack, Lua, NumResults, Optional, liftIO)
3838import System.Exit (ExitCode (..))
3939import Text.Pandoc.Class (runIO)
4040import Text.Pandoc.Definition (Block, Inline)
4141import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter)
4242import Text.Pandoc.Lua.StackInstances ()
43−import Text.Pandoc.Lua.Util (OrNil (toMaybe), addFunction, addValue,
44− loadScriptFromDataDir, raiseError)
43+import Text.Pandoc.Lua.Util (addFunction, addValue, loadScriptFromDataDir)
4544import Text.Pandoc.Walk (Walkable)
4645import Text.Pandoc.Options (ReaderOptions (readerExtensions))
4746import Text.Pandoc.Process (pipeProcess)
@@ -72,19 +71,19 @@ walkInline = walkElement
7271walkBlock :: Block -> LuaFilter -> Lua Block
7372walkBlock = walkElement
7473
75−readDoc :: String -> OrNil String -> Lua NumResults
74+readDoc :: String -> Optional String -> Lua NumResults
7675readDoc content formatSpecOrNil = do
77− let formatSpec = fromMaybe "markdown" (toMaybe formatSpecOrNil)
76+ let formatSpec = fromMaybe "markdown" (Lua.fromOptional formatSpecOrNil)
7877 case getReader formatSpec of
79− Left s -> raiseError s -- Unknown reader
78+ Left s -> Lua.raiseError s -- Unknown reader
8079 Right (reader, es) ->
8180 case reader of
8281 TextReader r -> do
8382 res <- liftIO $ runIO $ r def{ readerExtensions = es } (pack content)
8483 case res of
8584 Right pd -> (1 :: NumResults) <$ Lua.push pd -- success, push Pandoc
86− Left s -> raiseError (show s) -- error while reading
87− _ -> raiseError "Only string formats are supported at the moment."
85+ Left s -> Lua.raiseError (show s) -- error while reading
86+ _ -> Lua.raiseError "Only string formats are supported at the moment."
8887
8988-- | Pipes input through a command.
9089pipeFn :: String
@@ -95,7 +94,7 @@ pipeFn command args input = do
9594 (ec, output) <- liftIO $ pipeProcess Nothing command args input
9695 case ec of
9796 ExitSuccess -> 1 <$ Lua.push output
98− ExitFailure n -> raiseError (PipeError command n output)
97+ ExitFailure n -> Lua.raiseError (PipeError command n output)
9998
10099data PipeError = PipeError
101100 { pipeErrorCommand :: String
src/Text/Pandoc/Lua/Module/Utils.hs+3−3
@@ -33,7 +33,7 @@ import Control.Applicative ((<|>))
3333import Foreign.Lua (FromLuaStack, Lua, LuaInteger, NumResults)
3434import Text.Pandoc.Definition (Pandoc, Meta, MetaValue, Block, Inline)
3535import Text.Pandoc.Lua.StackInstances ()
36−import Text.Pandoc.Lua.Util (OrNil (OrNil), addFunction)
36+import Text.Pandoc.Lua.Util (addFunction)
3737
3838import qualified Data.Digest.Pure.SHA as SHA
3939import qualified Data.ByteString.Lazy as BSL
@@ -59,8 +59,8 @@ hierarchicalize = return . Shared.hierarchicalize
5959-- limit years to the range 1601-9999 (ISO 8601 accepts greater than
6060-- or equal to 1583, but MS Word only accepts dates starting 1601).
6161-- Returns nil instead of a string if the conversion failed.
62−normalizeDate :: String -> Lua (OrNil String)
63−normalizeDate = return . OrNil . Shared.normalizeDate
62+normalizeDate :: String -> Lua (Lua.Optional String)
63+normalizeDate = return . Lua.Optional . Shared.normalizeDate
6464
6565-- | Calculate the hash of the given contents.
6666sha1 :: BSL.ByteString
src/Text/Pandoc/Lua/Util.hs−16
@@ -38,7 +38,6 @@ module Text.Pandoc.Lua.Util
3838 , addRawInt
3939 , raiseError
4040 , popValue
41− , OrNil (..)
4241 , PushViaCall
4342 , pushViaCall
4443 , pushViaConstructor
@@ -115,21 +114,6 @@ popValue = do
115114 Left err -> Lua.throwLuaError err
116115 Right x -> return x
117116
118−-- | Newtype wrapper intended to be used for optional Lua values. Nesting this
119−-- type is strongly discouraged and will likely lead to a wrong result.
120−newtype OrNil a = OrNil { toMaybe :: Maybe a }
121−
122−instance FromLuaStack a => FromLuaStack (OrNil a) where
123− peek idx = do
124− noValue <- Lua.isnoneornil idx
125− if noValue
126− then return (OrNil Nothing)
127− else OrNil . Just <$> Lua.peek idx
128−
129−instance ToLuaStack a => ToLuaStack (OrNil a) where
130− push (OrNil Nothing) = Lua.pushnil
131− push (OrNil (Just x)) = Lua.push x
132−
133117-- | Helper class for pushing a single value to the stack via a lua function.
134118-- See @pushViaCall@.
135119class PushViaCall a where