git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #4153 from tarleb/unify-lua-init

Unify lua initalization

commit52a8116e71636f05053c959675b3abcb745e921a authorJohn MacFarlane <jgm@berkeley.edu> dateWed 13 Dec 2017, 21:42 (9 years ago) parent44053364c64af4
6 files changed+184−149
pandoc.cabal+2−1 src/Text/Pandoc/App.hs+2−2 src/Text/Pandoc/Lua.hs+14−53 src/Text/Pandoc/Lua/Init.hs+79 src/Text/Pandoc/Writers/Custom.hs+69−80 test/Tests/Lua.hs+18−13
pandoc.cabal+2−1
@@ -523,9 +523,10 @@ library
523523 Text.Pandoc.Readers.Org.ParserState,
524524 Text.Pandoc.Readers.Org.Parsing,
525525 Text.Pandoc.Readers.Org.Shared,
526+ Text.Pandoc.Lua.Filter,
527+ Text.Pandoc.Lua.Init,
526528 Text.Pandoc.Lua.Packages,
527529 Text.Pandoc.Lua.PandocModule,
528− Text.Pandoc.Lua.Filter,
529530 Text.Pandoc.Lua.StackInstances,
530531 Text.Pandoc.Lua.Util,
531532 Text.Pandoc.CSS,
src/Text/Pandoc/App.hs+2−2
@@ -223,7 +223,7 @@ convertWithOpts opts = do
223223 if ".lua" `isSuffixOf` format
224224 -- note: use non-lowercased version writerName
225225 then return (TextWriter
226− (\o d -> liftIO $ writeCustom writerName o d)
226+ (\o d -> writeCustom writerName o d)
227227 :: Writer PandocIO, mempty)
228228 else case getWriter writerName of
229229 Left e -> E.throwIO $ PandocAppError $
@@ -846,7 +846,7 @@ applyLuaFilters :: Maybe FilePath -> [FilePath] -> String -> Pandoc
846846applyLuaFilters mbDatadir filters format d = do
847847 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
848848 let go f d' = do
849− res <- runLuaFilter mbDatadir f format d'
849+ res <- runLuaFilter f format d'
850850 case res of
851851 Right x -> return x
852852 Left (LuaException s) -> E.throw (PandocFilterError f s)
src/Text/Pandoc/Lua.hs+14−53
@@ -1,9 +1,3 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE FlexibleContexts #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE ScopedTypeVariables #-}
6−{-# OPTIONS_GHC -fno-warn-orphans #-}
71{-
82Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
93
@@ -29,48 +23,36 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2923 Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3024 Stability : alpha
3125
32−Pandoc lua utils.
26+Running pandoc Lua filters.
3327-}
3428module Text.Pandoc.Lua
3529 ( LuaException (..)
36− , LuaPackageParams (..)
37− , pushPandocModule
3830 , runLuaFilter
39− , initLuaState
40− , luaPackageParams
31+ , runPandocLua
32+ , pushPandocModule
4133 ) where
4234
4335import Control.Monad (when, (>=>))
44−import Control.Monad.Identity (Identity)
45−import Control.Monad.Trans (MonadIO (..))
46−import Data.IORef (newIORef, readIORef)
4736import Foreign.Lua (FromLuaStack (peek), Lua, LuaException (..),
4837 Status (OK), ToLuaStack (push))
49−import Text.Pandoc.Class (PandocIO, getCommonState, getMediaBag, setMediaBag)
50−import Text.Pandoc.Definition
38+import Text.Pandoc.Class (PandocIO)
39+import Text.Pandoc.Definition (Pandoc)
5140import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
52−import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
53− installPandocPackageSearcher)
41+import Text.Pandoc.Lua.Init (runPandocLua)
5442import Text.Pandoc.Lua.PandocModule (pushPandocModule) -- TODO: remove
55−import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
5643import qualified Foreign.Lua as Lua
57−import qualified Foreign.Lua.Module.Text as Lua
5844
59−runLuaFilter :: Maybe FilePath -> FilePath -> String
45+-- | Run the Lua filter in @filterPath@ for a transformation to target
46+-- format @format@. Pandoc uses Lua init files to setup the Lua
47+-- interpreter.
48+runLuaFilter :: FilePath -> String
6049 -> Pandoc -> PandocIO (Either LuaException Pandoc)
61−runLuaFilter datadir filterPath format pd = do
62− luaPkgParams <- luaPackageParams datadir
63− res <- liftIO . Lua.runLuaEither $
64− runLuaFilter' luaPkgParams filterPath format pd
65− newMediaBag <- liftIO (readIORef (luaPkgMediaBag luaPkgParams))
66− setMediaBag newMediaBag
67− return res
50+runLuaFilter filterPath format doc =
51+ runPandocLua (runLuaFilter' filterPath format doc)
6852
69−runLuaFilter' :: LuaPackageParams
70− -> FilePath -> String
53+runLuaFilter' :: FilePath -> String
7154 -> Pandoc -> Lua Pandoc
72−runLuaFilter' luaPkgOpts filterPath format pd = do
73− initLuaState luaPkgOpts
55+runLuaFilter' filterPath format pd = do
7456 -- store module in global "pandoc"
7557 registerFormat
7658 top <- Lua.gettop
@@ -90,24 +72,6 @@ runLuaFilter' luaPkgOpts filterPath format pd = do
9072 push format
9173 Lua.setglobal "FORMAT"
9274
93−luaPackageParams :: Maybe FilePath -> PandocIO LuaPackageParams
94−luaPackageParams datadir = do
95− commonState <- getCommonState
96− mbRef <- liftIO . newIORef =<< getMediaBag
97− return LuaPackageParams
98− { luaPkgCommonState = commonState
99− , luaPkgDataDir = datadir
100− , luaPkgMediaBag = mbRef
101− }
102−
103−-- Initialize the lua state with all required values
104−initLuaState :: LuaPackageParams -> Lua ()
105−initLuaState luaPkgParams = do
106− Lua.openlibs
107− Lua.preloadTextModule "text"
108− installPandocPackageSearcher luaPkgParams
109− loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
110−
11175pushGlobalFilter :: Lua ()
11276pushGlobalFilter = do
11377 Lua.newtable
@@ -117,6 +81,3 @@ pushGlobalFilter = do
11781
11882runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
11983runAll = foldr ((>=>) . walkMWithLuaFilter) return
120−
121−instance (FromLuaStack a) => FromLuaStack (Identity a) where
122− peek = fmap return . peek
src/Text/Pandoc/Lua/Init.hsnew file+79
@@ -0,0 +1,79 @@
1+{-
2+Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+{- |
19+ Module : Text.Pandoc.Lua
20+ Copyright : Copyright © 2017 Albert Krewinkel
21+ License : GNU GPL, version 2 or above
22+
23+ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
24+ Stability : alpha
25+
26+Functions to initialize the Lua interpreter.
27+-}
28+module Text.Pandoc.Lua.Init
29+ ( LuaException (..)
30+ , LuaPackageParams (..)
31+ , runPandocLua
32+ , initLuaState
33+ , luaPackageParams
34+ ) where
35+
36+import Control.Monad.Trans (MonadIO (..))
37+import Data.IORef (newIORef, readIORef)
38+import Foreign.Lua (Lua, LuaException (..))
39+import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
40+import Text.Pandoc.Class (PandocIO, getCommonState, getUserDataDir, getMediaBag,
41+ setMediaBag)
42+import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
43+ installPandocPackageSearcher)
44+import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
45+
46+import qualified Foreign.Lua as Lua
47+import qualified Foreign.Lua.Module.Text as Lua
48+
49+-- | Run the lua interpreter, using pandoc's default way of environment
50+-- initalization.
51+runPandocLua :: Lua a -> PandocIO (Either LuaException a)
52+runPandocLua luaOp = do
53+ datadir <- getUserDataDir
54+ luaPkgParams <- luaPackageParams datadir
55+ enc <- liftIO $ getForeignEncoding <* setForeignEncoding utf8
56+ res <- liftIO $ Lua.runLuaEither (initLuaState luaPkgParams *> luaOp)
57+ liftIO $ setForeignEncoding enc
58+ newMediaBag <- liftIO (readIORef (luaPkgMediaBag luaPkgParams))
59+ setMediaBag newMediaBag
60+ return res
61+
62+-- | Generate parameters required to setup pandoc's lua environment.
63+luaPackageParams :: Maybe FilePath -> PandocIO LuaPackageParams
64+luaPackageParams datadir = do
65+ commonState <- getCommonState
66+ mbRef <- liftIO . newIORef =<< getMediaBag
67+ return LuaPackageParams
68+ { luaPkgCommonState = commonState
69+ , luaPkgDataDir = datadir
70+ , luaPkgMediaBag = mbRef
71+ }
72+
73+-- Initialize the lua state with all required values
74+initLuaState :: LuaPackageParams -> Lua ()
75+initLuaState luaPkgParams = do
76+ Lua.openlibs
77+ Lua.preloadTextModule "text"
78+ installPandocPackageSearcher luaPkgParams
79+ loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
src/Text/Pandoc/Writers/Custom.hs+69−80
@@ -1,11 +1,5 @@
1−{-# OPTIONS_GHC -fno-warn-orphans #-}
2−{-# LANGUAGE CPP #-}
31{-# LANGUAGE DeriveDataTypeable #-}
42{-# LANGUAGE FlexibleInstances #-}
5−#if MIN_VERSION_base(4,8,0)
6−#else
7−{-# LANGUAGE OverlappingInstances #-}
8−#endif
93{- Copyright (C) 2012-2017 John MacFarlane <jgm@berkeley.edu>
104
115This program is free software; you can redistribute it and/or modify
@@ -36,19 +30,23 @@ Conversion of 'Pandoc' documents to custom markup using
3630a lua writer.
3731-}
3832module Text.Pandoc.Writers.Custom ( writeCustom ) where
33+import Control.Arrow ((***))
3934import Control.Exception
4035import Control.Monad (when)
36+import Control.Monad.Trans (MonadIO (liftIO))
4137import Data.Char (toLower)
4238import Data.List (intersperse)
4339import qualified Data.Map as M
4440import Data.Text (Text, pack)
4541import Data.Typeable
46−import Foreign.Lua (Lua, ToLuaStack (..), callFunc, runLua)
42+import Foreign.Lua (Lua, ToLuaStack (..), callFunc)
4743import Foreign.Lua.Api
48−import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
44+import Text.Pandoc.Class (PandocIO)
4945import Text.Pandoc.Definition
5046import Text.Pandoc.Error
51−import Text.Pandoc.Lua.Util (addValue)
47+import Text.Pandoc.Lua.Init (runPandocLua)
48+import Text.Pandoc.Lua.StackInstances ()
49+import Text.Pandoc.Lua.Util (addValue, dostring')
5250import Text.Pandoc.Options
5351import Text.Pandoc.Templates
5452import qualified Text.Pandoc.UTF8 as UTF8
@@ -60,43 +58,31 @@ attrToMap (id',classes,keyvals) = M.fromList
6058 : ("class", unwords classes)
6159 : keyvals
6260
63−instance ToLuaStack Double where
64− push = push . (realToFrac :: Double -> LuaNumber)
65−
66−instance ToLuaStack Int where
67− push = push . (fromIntegral :: Int -> LuaInteger)
68−
69−instance ToLuaStack Format where
70− push (Format f) = push (map toLower f)
71−
72−#if MIN_VERSION_base(4,8,0)
73−instance {-# OVERLAPS #-} ToLuaStack [Inline] where
74−#else
75−instance ToLuaStack [Inline] where
76−#endif
77− push ils = push =<< inlineListToCustom ils
78−
79−#if MIN_VERSION_base(4,8,0)
80−instance {-# OVERLAPS #-} ToLuaStack [Block] where
81−#else
82−instance ToLuaStack [Block] where
83−#endif
84− push ils = push =<< blockListToCustom ils
85−
86−instance ToLuaStack MetaValue where
87− push (MetaMap m) = push m
88− push (MetaList xs) = push xs
89− push (MetaBool x) = push x
90− push (MetaString s) = push s
91− push (MetaInlines ils) = push ils
92− push (MetaBlocks bs) = push bs
93−
94−instance ToLuaStack Citation where
95− push cit = do
61+newtype Stringify a = Stringify a
62+
63+instance ToLuaStack (Stringify Format) where
64+ push (Stringify (Format f)) = push (map toLower f)
65+
66+instance ToLuaStack (Stringify [Inline]) where
67+ push (Stringify ils) = push =<< inlineListToCustom ils
68+
69+instance ToLuaStack (Stringify [Block]) where
70+ push (Stringify blks) = push =<< blockListToCustom blks
71+
72+instance ToLuaStack (Stringify MetaValue) where
73+ push (Stringify (MetaMap m)) = push (fmap Stringify m)
74+ push (Stringify (MetaList xs)) = push (map Stringify xs)
75+ push (Stringify (MetaBool x)) = push x
76+ push (Stringify (MetaString s)) = push s
77+ push (Stringify (MetaInlines ils)) = push (Stringify ils)
78+ push (Stringify (MetaBlocks bs)) = push (Stringify bs)
79+
80+instance ToLuaStack (Stringify Citation) where
81+ push (Stringify cit) = do
9682 createtable 6 0
9783 addValue "citationId" $ citationId cit
98− addValue "citationPrefix" $ citationPrefix cit
99− addValue "citationSuffix" $ citationSuffix cit
84+ addValue "citationPrefix" . Stringify $ citationPrefix cit
85+ addValue "citationSuffix" . Stringify $ citationSuffix cit
10086 addValue "citationMode" $ show (citationMode cit)
10187 addValue "citationNoteNum" $ citationNoteNum cit
10288 addValue "citationHash" $ citationHash cit
@@ -107,14 +93,11 @@ data PandocLuaException = PandocLuaException String
10793instance Exception PandocLuaException
10894
10995-- | Convert Pandoc to custom markup.
110−writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO Text
96+writeCustom :: FilePath -> WriterOptions -> Pandoc -> PandocIO Text
11197writeCustom luaFile opts doc@(Pandoc meta _) = do
112− luaScript <- UTF8.readFile luaFile
113− enc <- getForeignEncoding
114− setForeignEncoding utf8
115− (body, context) <- runLua $ do
116− openlibs
117− stat <- loadstring luaScript
98+ luaScript <- liftIO $ UTF8.readFile luaFile
99+ res <- runPandocLua $ do
100+ stat <- dostring' luaScript
118101 -- check for error in lua script (later we'll change the return type
119102 -- to handle this more gracefully):
120103 when (stat /= OK) $
@@ -127,7 +110,9 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
127110 inlineListToCustom
128111 meta
129112 return (rendered, context)
130− setForeignEncoding enc
113+ let (body, context) = case res of
114+ Left e -> throw (PandocLuaException (show e))
115+ Right x -> x
131116 case writerTemplate opts of
132117 Nothing -> return $ pack body
133118 Just tpl ->
@@ -138,7 +123,7 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
138123docToCustom :: WriterOptions -> Pandoc -> Lua String
139124docToCustom opts (Pandoc (Meta metamap) blocks) = do
140125 body <- blockListToCustom blocks
141− callFunc "Doc" body metamap (writerVariables opts)
126+ callFunc "Doc" body (fmap Stringify metamap) (writerVariables opts)
142127
143128-- | Convert Pandoc block element to Custom.
144129blockToCustom :: Block -- ^ Block element
@@ -146,41 +131,45 @@ blockToCustom :: Block -- ^ Block element
146131
147132blockToCustom Null = return ""
148133
149−blockToCustom (Plain inlines) = callFunc "Plain" inlines
134+blockToCustom (Plain inlines) = callFunc "Plain" (Stringify inlines)
150135
151136blockToCustom (Para [Image attr txt (src,tit)]) =
152− callFunc "CaptionedImage" src tit txt (attrToMap attr)
137+ callFunc "CaptionedImage" src tit (Stringify txt) (attrToMap attr)
153138
154−blockToCustom (Para inlines) = callFunc "Para" inlines
139+blockToCustom (Para inlines) = callFunc "Para" (Stringify inlines)
155140
156−blockToCustom (LineBlock linesList) = callFunc "LineBlock" linesList
141+blockToCustom (LineBlock linesList) = callFunc "LineBlock" (map Stringify linesList)
157142
158143blockToCustom (RawBlock format str) =
159− callFunc "RawBlock" format str
144+ callFunc "RawBlock" (Stringify format) str
160145
161146blockToCustom HorizontalRule = callFunc "HorizontalRule"
162147
163148blockToCustom (Header level attr inlines) =
164− callFunc "Header" level inlines (attrToMap attr)
149+ callFunc "Header" level (Stringify inlines) (attrToMap attr)
165150
166151blockToCustom (CodeBlock attr str) =
167152 callFunc "CodeBlock" str (attrToMap attr)
168153
169−blockToCustom (BlockQuote blocks) = callFunc "BlockQuote" blocks
154+blockToCustom (BlockQuote blocks) = callFunc "BlockQuote" (Stringify blocks)
170155
171−blockToCustom (Table capt aligns widths headers rows') =
172− callFunc "Table" capt (map show aligns) widths headers rows'
156+blockToCustom (Table capt aligns widths headers rows) =
157+ let aligns' = map show aligns
158+ capt' = Stringify capt
159+ headers' = map Stringify headers
160+ rows' = map (map Stringify) rows
161+ in callFunc "Table" capt' aligns' widths headers' rows'
173162
174−blockToCustom (BulletList items) = callFunc "BulletList" items
163+blockToCustom (BulletList items) = callFunc "BulletList" (map Stringify items)
175164
176165blockToCustom (OrderedList (num,sty,delim) items) =
177− callFunc "OrderedList" items num (show sty) (show delim)
166+ callFunc "OrderedList" (map Stringify items) num (show sty) (show delim)
178167
179168blockToCustom (DefinitionList items) =
180− callFunc "DefinitionList" items
169+ callFunc "DefinitionList" (map (Stringify *** map Stringify) items)
181170
182171blockToCustom (Div attr items) =
183− callFunc "Div" items (attrToMap attr)
172+ callFunc "Div" (Stringify items) (attrToMap attr)
184173
185174-- | Convert list of Pandoc block elements to Custom.
186175blockListToCustom :: [Block] -- ^ List of block elements
@@ -205,23 +194,23 @@ inlineToCustom Space = callFunc "Space"
205194
206195inlineToCustom SoftBreak = callFunc "SoftBreak"
207196
208−inlineToCustom (Emph lst) = callFunc "Emph" lst
197+inlineToCustom (Emph lst) = callFunc "Emph" (Stringify lst)
209198
210−inlineToCustom (Strong lst) = callFunc "Strong" lst
199+inlineToCustom (Strong lst) = callFunc "Strong" (Stringify lst)
211200
212−inlineToCustom (Strikeout lst) = callFunc "Strikeout" lst
201+inlineToCustom (Strikeout lst) = callFunc "Strikeout" (Stringify lst)
213202
214−inlineToCustom (Superscript lst) = callFunc "Superscript" lst
203+inlineToCustom (Superscript lst) = callFunc "Superscript" (Stringify lst)
215204
216−inlineToCustom (Subscript lst) = callFunc "Subscript" lst
205+inlineToCustom (Subscript lst) = callFunc "Subscript" (Stringify lst)
217206
218−inlineToCustom (SmallCaps lst) = callFunc "SmallCaps" lst
207+inlineToCustom (SmallCaps lst) = callFunc "SmallCaps" (Stringify lst)
219208
220−inlineToCustom (Quoted SingleQuote lst) = callFunc "SingleQuoted" lst
209+inlineToCustom (Quoted SingleQuote lst) = callFunc "SingleQuoted" (Stringify lst)
221210
222−inlineToCustom (Quoted DoubleQuote lst) = callFunc "DoubleQuoted" lst
211+inlineToCustom (Quoted DoubleQuote lst) = callFunc "DoubleQuoted" (Stringify lst)
223212
224−inlineToCustom (Cite cs lst) = callFunc "Cite" lst cs
213+inlineToCustom (Cite cs lst) = callFunc "Cite" (Stringify lst) (map Stringify cs)
225214
226215inlineToCustom (Code attr str) =
227216 callFunc "Code" str (attrToMap attr)
@@ -233,17 +222,17 @@ inlineToCustom (Math InlineMath str) =
233222 callFunc "InlineMath" str
234223
235224inlineToCustom (RawInline format str) =
236− callFunc "RawInline" format str
225+ callFunc "RawInline" (Stringify format) str
237226
238227inlineToCustom LineBreak = callFunc "LineBreak"
239228
240229inlineToCustom (Link attr txt (src,tit)) =
241− callFunc "Link" txt src tit (attrToMap attr)
230+ callFunc "Link" (Stringify txt) src tit (attrToMap attr)
242231
243232inlineToCustom (Image attr alt (src,tit)) =
244− callFunc "Image" alt src tit (attrToMap attr)
233+ callFunc "Image" (Stringify alt) src tit (attrToMap attr)
245234
246−inlineToCustom (Note contents) = callFunc "Note" contents
235+inlineToCustom (Note contents) = callFunc "Note" (Stringify contents)
247236
248237inlineToCustom (Span attr items) =
249− callFunc "Span" items (attrToMap attr)
238+ callFunc "Span" (Stringify items) (attrToMap attr)
test/Tests/Lua.hs+18−13
@@ -10,9 +10,9 @@ import Text.Pandoc.Arbitrary ()
1010import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
1111 header, linebreak, para, plain, rawBlock,
1212 singleQuoted, space, str, strong, (<>))
13−import Text.Pandoc.Class (runIOorExplode)
13+import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
1414import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
15−import Text.Pandoc.Lua (initLuaState, runLuaFilter, luaPackageParams)
15+import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
1616
1717import qualified Foreign.Lua as Lua
1818
@@ -95,8 +95,9 @@ tests = map (localOption (QuickCheckTests 20))
9595
9696assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
9797assertFilterConversion msg filterPath docIn docExpected = do
98− docEither <- runIOorExplode $
99− runLuaFilter (Just "../data") ("lua" </> filterPath) [] docIn
98+ docEither <- runIOorExplode $ do
99+ setUserDataDir (Just "../data")
100+ runLuaFilter ("lua" </> filterPath) [] docIn
100101 case docEither of
101102 Left _ -> fail "lua filter failed"
102103 Right docRes -> assertEqual msg docExpected docRes
@@ -105,14 +106,18 @@ roundtripEqual :: (Eq a, Lua.FromLuaStack a, Lua.ToLuaStack a) => a -> IO Bool
105106roundtripEqual x = (x ==) <$> roundtripped
106107 where
107108 roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
108− roundtripped = Lua.runLua $ do
109− initLuaState =<< Lua.liftIO (runIOorExplode (luaPackageParams (Just "../data")))
110− oldSize <- Lua.gettop
111− Lua.push x
112− size <- Lua.gettop
113− when (size - oldSize /= 1) $
114− error ("not exactly one additional element on the stack: " ++ show size)
115− res <- Lua.peekEither (-1)
109+ roundtripped = runIOorExplode $ do
110+ setUserDataDir (Just "../data")
111+ res <- runPandocLua $ do
112+ oldSize <- Lua.gettop
113+ Lua.push x
114+ size <- Lua.gettop
115+ when (size - oldSize /= 1) $
116+ error ("not exactly one additional element on the stack: " ++ show size)
117+ res <- Lua.peekEither (-1)
118+ case res of
119+ Left _ -> error "could not read from stack"
120+ Right y -> return y
116121 case res of
117− Left _ -> error "could not read from stack"
122+ Left e -> error (show e)
118123 Right y -> return y