From 412596c30baec47041ccb3b1823f9beca7c98d76 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Sun, 2 Jan 2022 11:04:10 +0100 Subject: Switch to hslua-2.1 This allows for some code simplification and improves stability. --- src/Text/Pandoc/Lua/Module/MediaBag.hs | 14 +++++++------- src/Text/Pandoc/Lua/Module/Pandoc.hs | 33 +++++++++++++++------------------ src/Text/Pandoc/Lua/Module/Template.hs | 6 +++--- src/Text/Pandoc/Lua/Module/Utils.hs | 7 +++---- 4 files changed, 28 insertions(+), 32 deletions(-) (limited to 'src/Text/Pandoc/Lua/Module') diff --git a/src/Text/Pandoc/Lua/Module/MediaBag.hs b/src/Text/Pandoc/Lua/Module/MediaBag.hs index 51d813517..8be668089 100644 --- a/src/Text/Pandoc/Lua/Module/MediaBag.hs +++ b/src/Text/Pandoc/Lua/Module/MediaBag.hs @@ -16,7 +16,7 @@ import Prelude hiding (lookup) import Data.Maybe (fromMaybe) import HsLua ( LuaE, DocumentedFunction, Module (..) , (<#>), (###), (=#>), (=?>), defun, functionResult - , optionalParameter , parameter) + , opt, parameter, stringParam, textParam) import Text.Pandoc.Class.CommonState (CommonState (..)) import Text.Pandoc.Class.PandocMonad (fetchItem, getMediaBag, modifyCommonState, setMediaBag) @@ -55,7 +55,7 @@ delete :: DocumentedFunction PandocError delete = defun "delete" ### (\fp -> unPandocLua $ modifyCommonState (\st -> st { stMediaBag = MB.deleteMedia fp (stMediaBag st) })) - <#> parameter Lua.peekString "string" "filepath" "filename of item to delete" + <#> stringParam "filepath" "filename of item to delete" =#> [] @@ -72,10 +72,10 @@ insert = defun "insert" mb <- getMediaBag setMediaBag $ MB.insertMedia fp mmime contents mb return (Lua.NumResults 0)) - <#> parameter Lua.peekString "string" "filepath" "item file path" - <#> optionalParameter Lua.peekText "string" "mimetype" "the item's MIME type" + <#> stringParam "filepath" "item file path" + <#> opt (textParam "mimetype" "the item's MIME type") <#> parameter Lua.peekLazyByteString "string" "contents" "binary contents" - =?> "Nothing" + =#> [] -- | Returns iterator values to be used with a Lua @for@ loop. items :: DocumentedFunction PandocError @@ -98,7 +98,7 @@ lookup = defun "lookup" Just item -> 2 <$ do Lua.pushText $ MB.mediaMimeType item Lua.pushLazyByteString $ MB.mediaContents item) - <#> parameter Lua.peekString "string" "filepath" "path of item to lookup" + <#> stringParam "filepath" "path of item to lookup" =?> "MIME type and contents" -- | Function listing all mediabag items. @@ -122,5 +122,5 @@ fetch = defun "fetch" Lua.pushText $ fromMaybe "" mimeType Lua.pushByteString bs return 2) - <#> parameter Lua.peekText "string" "src" "URI to fetch" + <#> textParam "src" "URI to fetch" =?> "Returns two string values: the fetched contents and the mimetype." diff --git a/src/Text/Pandoc/Lua/Module/Pandoc.hs b/src/Text/Pandoc/Lua/Module/Pandoc.hs index 9864da0db..7d8a98bb1 100644 --- a/src/Text/Pandoc/Lua/Module/Pandoc.hs +++ b/src/Text/Pandoc/Lua/Module/Pandoc.hs @@ -26,7 +26,6 @@ import Data.Default (Default (..)) import Data.Maybe (fromMaybe) import Data.Proxy (Proxy (Proxy)) import HsLua hiding (pushModule) -import HsLua.Class.Peekable (PeekError) import System.Exit (ExitCode (..)) import Text.Pandoc.Definition import Text.Pandoc.Error (PandocError (..)) @@ -49,7 +48,6 @@ import qualified HsLua as Lua import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as BSL import qualified Data.Text as T -import qualified Text.Pandoc.Lua.Util as LuaUtil import qualified Text.Pandoc.UTF8 as UTF8 -- | Push the "pandoc" package to the Lua stack. Requires the `List` @@ -198,9 +196,9 @@ functions = Left e -> throwM e) <#> parameter peekByteString "string" "content" "text to parse" - <#> optionalParameter peekText "string" "formatspec" "format and extensions" - <#> optionalParameter peekReaderOptions "ReaderOptions" "reader_options" - "reader options" + <#> opt (textParam "formatspec" "format and extensions") + <#> opt (parameter peekReaderOptions "ReaderOptions" "reader_options" + "reader options") =#> functionResult pushPandoc "Pandoc" "result document" , sha1 @@ -227,10 +225,9 @@ functions = (ByteStringWriter w, es) -> Left <$> w writerOpts{ writerExtensions = es } doc) <#> parameter peekPandoc "Pandoc" "doc" "document to convert" - <#> optionalParameter peekText "string" "formatspec" - "format and extensions" - <#> optionalParameter peekWriterOptions "WriterOptions" "writer_options" - "writer options" + <#> opt (textParam "formatspec" "format and extensions") + <#> opt (parameter peekWriterOptions "WriterOptions" "writer_options" + "writer options") =#> functionResult (either pushLazyByteString pushText) "string" "result document" ] @@ -247,23 +244,23 @@ data PipeError = PipeError , pipeErrorOutput :: BL.ByteString } -peekPipeError :: PeekError e => StackIndex -> LuaE e PipeError +peekPipeError :: LuaError e => StackIndex -> LuaE e PipeError peekPipeError idx = PipeError <$> (Lua.getfield idx "command" *> Lua.peek (-1) <* Lua.pop 1) <*> (Lua.getfield idx "error_code" *> Lua.peek (-1) <* Lua.pop 1) <*> (Lua.getfield idx "output" *> Lua.peek (-1) <* Lua.pop 1) -pushPipeError :: PeekError e => Pusher e PipeError +pushPipeError :: LuaError e => Pusher e PipeError pushPipeError pipeErr = do - Lua.newtable - LuaUtil.addField "command" (pipeErrorCommand pipeErr) - LuaUtil.addField "error_code" (pipeErrorCode pipeErr) - LuaUtil.addField "output" (pipeErrorOutput pipeErr) + pushAsTable [ ("command" , pushText . pipeErrorCommand) + , ("error_code" , pushIntegral . pipeErrorCode) + , ("output" , pushLazyByteString . pipeErrorOutput) + ] pipeErr pushPipeErrorMetaTable - Lua.setmetatable (-2) + Lua.setmetatable (nth 2) where - pushPipeErrorMetaTable :: PeekError e => LuaE e () + pushPipeErrorMetaTable :: LuaError e => LuaE e () pushPipeErrorMetaTable = do v <- Lua.newmetatable "pandoc pipe error" when v $ do @@ -271,7 +268,7 @@ pushPipeError pipeErr = do pushHaskellFunction pipeErrorMessage rawset (nth 3) - pipeErrorMessage :: PeekError e => LuaE e NumResults + pipeErrorMessage :: LuaError e => LuaE e NumResults pipeErrorMessage = do (PipeError cmd errorCode output) <- peekPipeError (nthBottom 1) pushByteString . BSL.toStrict . BSL.concat $ diff --git a/src/Text/Pandoc/Lua/Module/Template.hs b/src/Text/Pandoc/Lua/Module/Template.hs index cd66ce1c1..967fe31a8 100644 --- a/src/Text/Pandoc/Lua/Module/Template.hs +++ b/src/Text/Pandoc/Lua/Module/Template.hs @@ -42,7 +42,7 @@ functions = Nothing -> runWithDefaultPartials (compileTemplate "templates/default" template)) <#> parameter peekText "string" "template" "template string" - <#> optionalParameter peekString "string" "templ_path" "template path" + <#> opt (stringParam "templ_path" "template path") =#> functionResult (either failLua pushTemplate) "pandoc Template" "compiled template" @@ -53,8 +53,8 @@ functions = forcePeek $ peekText top `lastly` pop 1 format <- maybe getFORMAT pure mformat getDefaultTemplate format) - <#> optionalParameter peekText "string" "writer" - "writer for which the template should be returned." + <#> opt (textParam "writer" + "writer for which the template should be returned.") =#> functionResult pushText "string" "string representation of the writer's default template" diff --git a/src/Text/Pandoc/Lua/Module/Utils.hs b/src/Text/Pandoc/Lua/Module/Utils.hs index 0c3969e13..14796b146 100644 --- a/src/Text/Pandoc/Lua/Module/Utils.hs +++ b/src/Text/Pandoc/Lua/Module/Utils.hs @@ -56,8 +56,7 @@ documentedModule = Module return $ B.toList (Shared.blocksToInlinesWithSep sep blks)) <#> parameter (peekList peekBlock) "list of blocks" "blocks" "" - <#> optionalParameter (peekList peekInline) "list of inlines" - "inline" "" + <#> opt (parameter (peekList peekInline) "list of inlines" "inline" "") =#> functionResult pushInlines "list of inlines" "" , defun "equals" @@ -121,8 +120,8 @@ documentedModule = Module ) <#> parameter peekPandoc "Pandoc" "doc" "input document" <#> parameter peekString "filepath" "filter_path" "path to filter" - <#> optionalParameter (peekList peekString) "list of strings" - "args" "arguments to pass to the filter" + <#> opt (parameter (peekList peekString) "list of strings" + "args" "arguments to pass to the filter") =#> functionResult pushPandoc "Pandoc" "filtered document" , defun "stringify" -- cgit v1.2.3