git.iko.soy/typeable/pandoc
on master help sign in
Lua: cleanup Lua utils, remove unused functions.
commitacf6df1aef337f71612f6d05c8aaa526255b3942 authorAlbert Krewinkel <albert@zeitkraut.de> dateSun 12 Aug 2018, 00:38 (8 years ago) parentfb3ee94
5 files changed+75−104
src/Text/Pandoc/Lua.hs+7−9 src/Text/Pandoc/Lua/Module/Pandoc.hs+5−4 src/Text/Pandoc/Lua/StackInstances.hs+28−28 src/Text/Pandoc/Lua/Util.hs+28−56 src/Text/Pandoc/Writers/Custom.hs+7−7
src/Text/Pandoc/Lua.hs+7−9
@@ -34,14 +34,14 @@ module Text.Pandoc.Lua
3434
3535import Prelude
3636import Control.Monad ((>=>))
37−import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
38− Status (OK), ToLuaStack (push))
37+import Foreign.Lua (Lua, LuaException (..))
3938import Text.Pandoc.Class (PandocIO)
4039import Text.Pandoc.Definition (Pandoc)
4140import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
4241import Text.Pandoc.Lua.Init (runPandocLua, registerScriptPath)
4342import Text.Pandoc.Lua.Util (popValue)
4443import Text.Pandoc.Options (ReaderOptions)
44+
4545import qualified Foreign.Lua as Lua
4646
4747-- | Run the Lua filter in @filterPath@ for a transformation to target
@@ -60,25 +60,23 @@ runLuaFilter' ropts filterPath format pd = do
6060 registerScriptPath filterPath
6161 top <- Lua.gettop
6262 stat <- Lua.dofile filterPath
63− if stat /= OK
64− then do
65− luaErrMsg <- popValue
66− Lua.throwLuaError luaErrMsg
63+ if stat /= Lua.OK
64+ then Lua.throwTopMessageAsError
6765 else do
6866 newtop <- Lua.gettop
6967 -- Use the returned filters, or the implicitly defined global filter if
7068 -- nothing was returned.
7169 luaFilters <- if newtop - top >= 1
72− then peek (-1)
70+ then Lua.peek Lua.stackTop
7371 else Lua.getglobal "_G" *> fmap (:[]) popValue
7472 runAll luaFilters pd
7573 where
7674 registerFormat = do
77− push format
75+ Lua.push format
7876 Lua.setglobal "FORMAT"
7977
8078 registerReaderOptions = do
81− push ropts
79+ Lua.push ropts
8280 Lua.setglobal "PANDOC_READER_OPTIONS"
8381
8482runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
src/Text/Pandoc/Lua/Module/Pandoc.hs+5−4
@@ -42,7 +42,7 @@ import Text.Pandoc.Class (runIO)
4242import Text.Pandoc.Definition (Block, Inline)
4343import Text.Pandoc.Lua.Filter (walkInlines, walkBlocks, LuaFilter)
4444import Text.Pandoc.Lua.StackInstances ()
45−import Text.Pandoc.Lua.Util (addFunction, addValue, loadScriptFromDataDir)
45+import Text.Pandoc.Lua.Util (addFunction, loadScriptFromDataDir)
4646import Text.Pandoc.Walk (Walkable)
4747import Text.Pandoc.Options (ReaderOptions (readerExtensions))
4848import Text.Pandoc.Process (pipeProcess)
@@ -51,6 +51,7 @@ import Text.Pandoc.Readers (Reader (..), getReader)
5151import qualified Data.ByteString.Lazy as BL
5252import qualified Data.ByteString.Lazy.Char8 as BSL
5353import qualified Foreign.Lua as Lua
54+import qualified Text.Pandoc.Lua.Util as LuaUtil
5455
5556-- | Push the "pandoc" on the lua stack. Requires the `list` module to be
5657-- loaded.
@@ -114,9 +115,9 @@ instance FromLuaStack PipeError where
114115instance ToLuaStack PipeError where
115116 push pipeErr = do
116117 Lua.newtable
117− addValue "command" (pipeErrorCommand pipeErr)
118− addValue "error_code" (pipeErrorCode pipeErr)
119− addValue "output" (pipeErrorOutput pipeErr)
118+ LuaUtil.addField "command" (pipeErrorCommand pipeErr)
119+ LuaUtil.addField "error_code" (pipeErrorCode pipeErr)
120+ LuaUtil.addField "output" (pipeErrorOutput pipeErr)
120121 pushPipeErrorMetaTable
121122 Lua.setmetatable (-2)
122123 where
src/Text/Pandoc/Lua/StackInstances.hs+28−28
@@ -44,7 +44,7 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
4444 ToLuaStack (push), Type (..), throwLuaError, tryLua)
4545import Text.Pandoc.Definition
4646import Text.Pandoc.Extensions (Extensions)
47−import Text.Pandoc.Lua.Util (getTable, getTag, pushViaConstructor, typeCheck)
47+import Text.Pandoc.Lua.Util (pushViaConstructor, typeCheck)
4848import Text.Pandoc.Options (ReaderOptions (..), TrackChanges)
4949import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
5050
@@ -62,7 +62,7 @@ instance ToLuaStack Pandoc where
6262instance FromLuaStack Pandoc where
6363 peek idx = defineHowTo "get Pandoc value" $ do
6464 typeCheck idx Lua.TypeTable
65− blocks <- getTable idx "blocks"
65+ blocks <- LuaUtil.rawField idx "blocks"
6666 meta <- Lua.getfield idx "meta" *> (Lua.peek Lua.stackTop `finally` Lua.pop 1)
6767 return $ Pandoc meta blocks
6868
@@ -99,12 +99,12 @@ instance ToLuaStack Citation where
9999
100100instance FromLuaStack Citation where
101101 peek idx = do
102− id' <- getTable idx "id"
103− prefix <- getTable idx "prefix"
104− suffix <- getTable idx "suffix"
105− mode <- getTable idx "mode"
106− num <- getTable idx "note_num"
107− hash <- getTable idx "hash"
102+ id' <- LuaUtil.rawField idx "id"
103+ prefix <- LuaUtil.rawField idx "prefix"
104+ suffix <- LuaUtil.rawField idx "suffix"
105+ mode <- LuaUtil.rawField idx "mode"
106+ num <- LuaUtil.rawField idx "note_num"
107+ hash <- LuaUtil.rawField idx "hash"
108108 return $ Citation id' prefix suffix mode num hash
109109
110110instance ToLuaStack Alignment where
@@ -178,7 +178,7 @@ peekMetaValue idx = defineHowTo "get MetaValue" $ do
178178 TypeBoolean -> MetaBool <$> peek idx
179179 TypeString -> MetaString <$> peek idx
180180 TypeTable -> do
181− tag <- tryLua $ getTag idx
181+ tag <- tryLua $ LuaUtil.getTag idx
182182 case tag of
183183 Right "MetaBlocks" -> MetaBlocks <$> elementContent
184184 Right "MetaBool" -> MetaBool <$> elementContent
@@ -220,7 +220,7 @@ pushBlock = \case
220220peekBlock :: StackIndex -> Lua Block
221221peekBlock idx = defineHowTo "get Block value" $ do
222222 typeCheck idx Lua.TypeTable
223− tag <- getTag idx
223+ tag <- LuaUtil.getTag idx
224224 case tag of
225225 "BlockQuote" -> BlockQuote <$> elementContent
226226 "BulletList" -> BulletList <$> elementContent
@@ -243,7 +243,7 @@ peekBlock idx = defineHowTo "get Block value" $ do
243243 where
244244 -- Get the contents of an AST element.
245245 elementContent :: FromLuaStack a => Lua a
246− elementContent = getTable idx "c"
246+ elementContent = LuaUtil.rawField idx "c"
247247
248248-- | Push an inline element to the top of the lua stack.
249249pushInline :: Inline -> Lua ()
@@ -272,7 +272,7 @@ pushInline = \case
272272peekInline :: StackIndex -> Lua Inline
273273peekInline idx = defineHowTo "get Inline value" $ do
274274 typeCheck idx Lua.TypeTable
275− tag <- getTag idx
275+ tag <- LuaUtil.getTag idx
276276 case tag of
277277 "Cite" -> uncurry Cite <$> elementContent
278278 "Code" -> withAttr Code <$> elementContent
@@ -299,7 +299,7 @@ peekInline idx = defineHowTo "get Inline value" $ do
299299 where
300300 -- Get the contents of an AST element.
301301 elementContent :: FromLuaStack a => Lua a
302− elementContent = getTable idx "c"
302+ elementContent = LuaUtil.rawField idx "c"
303303
304304withAttr :: (Attr -> a -> b) -> (LuaAttr, a) -> b
305305withAttr f (attributes, x) = f (fromLuaAttr attributes) x
@@ -321,11 +321,11 @@ instance ToLuaStack Element where
321321 push (Blk blk) = push blk
322322 push (Sec lvl num attr label contents) = do
323323 Lua.newtable
324− LuaUtil.addValue "level" lvl
325− LuaUtil.addValue "numbering" num
326− LuaUtil.addValue "attr" (LuaAttr attr)
327− LuaUtil.addValue "label" label
328− LuaUtil.addValue "contents" contents
324+ LuaUtil.addField "level" lvl
325+ LuaUtil.addField "numbering" num
326+ LuaUtil.addField "attr" (LuaAttr attr)
327+ LuaUtil.addField "label" label
328+ LuaUtil.addField "contents" contents
329329 pushSecMetaTable
330330 Lua.setmetatable (-2)
331331 where
@@ -333,7 +333,7 @@ instance ToLuaStack Element where
333333 pushSecMetaTable = do
334334 inexistant <- Lua.newmetatable "PandocElementSec"
335335 when inexistant $ do
336− LuaUtil.addValue "t" "Sec"
336+ LuaUtil.addField "t" "Sec"
337337 Lua.push "__index"
338338 Lua.pushvalue (-2)
339339 Lua.rawset (-3)
@@ -367,12 +367,12 @@ instance ToLuaStack ReaderOptions where
367367 (stripComments :: Bool)
368368 = ro
369369 Lua.newtable
370− LuaUtil.addValue "extensions" extensions
371− LuaUtil.addValue "standalone" standalone
372− LuaUtil.addValue "columns" columns
373− LuaUtil.addValue "tabStop" tabStop
374− LuaUtil.addValue "indentedCodeClasses" indentedCodeClasses
375− LuaUtil.addValue "abbreviations" abbreviations
376− LuaUtil.addValue "defaultImageExtension" defaultImageExtension
377− LuaUtil.addValue "trackChanges" trackChanges
378− LuaUtil.addValue "stripComments" stripComments
370+ LuaUtil.addField "extensions" extensions
371+ LuaUtil.addField "standalone" standalone
372+ LuaUtil.addField "columns" columns
373+ LuaUtil.addField "tabStop" tabStop
374+ LuaUtil.addField "indentedCodeClasses" indentedCodeClasses
375+ LuaUtil.addField "abbreviations" abbreviations
376+ LuaUtil.addField "defaultImageExtension" defaultImageExtension
377+ LuaUtil.addField "trackChanges" trackChanges
378+ LuaUtil.addField "stripComments" stripComments
src/Text/Pandoc/Lua/Util.hs+28−56
@@ -31,14 +31,11 @@ Lua utility functions.
3131-}
3232module Text.Pandoc.Lua.Util
3333 ( getTag
34− , getTable
35− , addValue
34+ , rawField
35+ , addField
3636 , addFunction
37− , getRawInt
38− , setRawInt
39− , addRawInt
37+ , addValue
4038 , typeCheck
41− , raiseError
4239 , popValue
4340 , PushViaCall
4441 , pushViaCall
@@ -51,34 +48,30 @@ import Prelude
5148import Control.Monad (when)
5249import Control.Monad.Catch (finally)
5350import Data.ByteString.Char8 (unpack)
54−import Foreign.Lua (FromLuaStack (..), NumResults, Lua, NumArgs, StackIndex,
55− ToLuaStack (..), ToHaskellFunction)
56−import Foreign.Lua.Api (Status, call, pop, rawget, rawgeti, rawset, rawseti)
51+import Foreign.Lua (FromLuaStack, Lua, NumArgs, StackIndex, Status,
52+ ToLuaStack, ToHaskellFunction)
5753import Text.Pandoc.Class (readDataFile, runIOorExplode, setUserDataDir)
5854
5955import qualified Foreign.Lua as Lua
6056
61−-- | Adjust the stack index, assuming that @n@ new elements have been pushed on
62−-- the stack.
63−adjustIndexBy :: StackIndex -> StackIndex -> StackIndex
64−adjustIndexBy idx n =
65− if idx < 0
66− then idx - n
67− else idx
68−
6957-- | Get value behind key from table at given index.
70−getTable :: (ToLuaStack a, FromLuaStack b) => StackIndex -> a -> Lua b
71−getTable idx key = do
72− push key
73− rawget (idx `adjustIndexBy` 1)
58+rawField :: FromLuaStack a => StackIndex -> String -> Lua a
59+rawField idx key = do
60+ absidx <- Lua.absindex idx
61+ Lua.push key
62+ Lua.rawget absidx
7463 popValue
7564
65+-- | Add a value to the table at the top of the stack at a string-index.
66+addField :: ToLuaStack a => String -> a -> Lua ()
67+addField = addValue
68+
7669-- | Add a key-value pair to the table at the top of the stack.
7770addValue :: (ToLuaStack a, ToLuaStack b) => a -> b -> Lua ()
7871addValue key value = do
79− push key
80− push value
81− rawset (-3)
72+ Lua.push key
73+ Lua.push value
74+ Lua.rawset (Lua.nthFromTop 3)
8275
8376-- | Add a function to the table at the top of the stack, using the given name.
8477addFunction :: ToHaskellFunction a => String -> a -> Lua ()
@@ -88,22 +81,6 @@ addFunction name fn = do
8881 Lua.wrapHaskellFunction
8982 Lua.rawset (-3)
9083
91−-- | Get value behind key from table at given index.
92−getRawInt :: FromLuaStack a => StackIndex -> Int -> Lua a
93−getRawInt idx key = do
94− rawgeti idx key
95− popValue
96−
97−-- | Set numeric key/value in table at the given index
98−setRawInt :: ToLuaStack a => StackIndex -> Int -> a -> Lua ()
99−setRawInt idx key value = do
100− push value
101− rawseti (idx `adjustIndexBy` 1) key
102−
103−-- | Set numeric key/value in table at the top of the stack.
104−addRawInt :: ToLuaStack a => Int -> a -> Lua ()
105−addRawInt = setRawInt (-1)
106−
10784typeCheck :: StackIndex -> Lua.Type -> Lua ()
10885typeCheck idx expected = do
10986 actual <- Lua.ltype idx
@@ -112,16 +89,11 @@ typeCheck idx expected = do
11289 actName <- Lua.typename actual
11390 Lua.throwLuaError $ "expected " ++ expName ++ " but got " ++ actName ++ "."
11491
115−raiseError :: ToLuaStack a => a -> Lua NumResults
116−raiseError e = do
117− Lua.push e
118− fromIntegral <$> Lua.lerror
119−
12092-- | Get, then pop the value at the top of the stack.
12193popValue :: FromLuaStack a => Lua a
12294popValue = do
12395 resOrError <- Lua.peekEither (-1)
124− pop 1
96+ Lua.pop 1
12597 case resOrError of
12698 Left err -> Lua.throwLuaError err
12799 Right x -> return x
@@ -136,11 +108,11 @@ instance PushViaCall (Lua ()) where
136108 Lua.push fn
137109 Lua.rawget Lua.registryindex
138110 pushArgs
139− call num 1
111+ Lua.call num 1
140112
141113instance (ToLuaStack a, PushViaCall b) => PushViaCall (a -> b) where
142114 pushViaCall' fn pushArgs num x =
143− pushViaCall' fn (pushArgs *> push x) (num + 1)
115+ pushViaCall' fn (pushArgs *> Lua.push x) (num + 1)
144116
145117-- | Push an value to the stack via a lua function. The lua function is called
146118-- with all arguments that are passed to this function and is expected to return
@@ -163,9 +135,9 @@ loadScriptFromDataDir datadir scriptFile = do
163135 "Couldn't load '" ++ scriptFile ++ "'.\n" ++ msg
164136
165137-- | Load a string and immediately perform a full garbage collection. This is
166−-- important to keep the program from hanging: If the program contained a call
167−-- to @require@, the a new loader function was created which then become
168−-- garbage. If that function is collected at an inopportune times, i.e. when the
138+-- important to keep the program from hanging: If the program containes a call
139+-- to @require@, then a new loader function is created which then becomes
140+-- garbage. If that function is collected at an inopportune time, i.e. when the
169141-- Lua API is called via a function that doesn't allow calling back into Haskell
170142-- (getraw, setraw, …), then the function's finalizer, and the full program,
171143-- will hang.
@@ -182,8 +154,8 @@ dostring' script = do
182154-- metatable.
183155getTag :: StackIndex -> Lua String
184156getTag idx = do
185− top <- Lua.gettop
186− hasMT <- Lua.getmetatable idx
187− push "tag"
188− if hasMT then Lua.rawget (-2) else Lua.rawget (idx `adjustIndexBy` 1)
189− peek Lua.stackTop `finally` Lua.settop top
157+ -- push metatable or just the table
158+ Lua.getmetatable idx >>= \hasMT -> when (not hasMT) (Lua.pushvalue idx)
159+ Lua.push "tag"
160+ Lua.rawget (Lua.nthFromTop 2)
161+ Lua.peek Lua.stackTop `finally` Lua.pop 2
src/Text/Pandoc/Writers/Custom.hs+7−7
@@ -48,7 +48,7 @@ import Text.Pandoc.Definition
4848import Text.Pandoc.Error
4949import Text.Pandoc.Lua.Init (runPandocLua, registerScriptPath)
5050import Text.Pandoc.Lua.StackInstances ()
51−import Text.Pandoc.Lua.Util (addValue, dostring')
51+import Text.Pandoc.Lua.Util (addField, addValue, dostring')
5252import Text.Pandoc.Options
5353import Text.Pandoc.Templates
5454import qualified Text.Pandoc.UTF8 as UTF8
@@ -82,12 +82,12 @@ instance ToLuaStack (Stringify MetaValue) where
8282instance ToLuaStack (Stringify Citation) where
8383 push (Stringify cit) = do
8484 createtable 6 0
85− addValue "citationId" $ citationId cit
86− addValue "citationPrefix" . Stringify $ citationPrefix cit
87− addValue "citationSuffix" . Stringify $ citationSuffix cit
88− addValue "citationMode" $ show (citationMode cit)
89− addValue "citationNoteNum" $ citationNoteNum cit
90− addValue "citationHash" $ citationHash cit
85+ addField "citationId" $ citationId cit
86+ addField "citationPrefix" . Stringify $ citationPrefix cit
87+ addField "citationSuffix" . Stringify $ citationSuffix cit
88+ addField "citationMode" $ show (citationMode cit)
89+ addField "citationNoteNum" $ citationNoteNum cit
90+ addField "citationHash" $ citationHash cit
9191
9292-- | Key-value pair, pushed as a table with @a@ as the only key and @v@ as the
9393-- associated value.