@@ -34,10 +34,11 @@ module Text.Pandoc.Lua.StackInstances () where
3434
3535import Control.Applicative ( (<|>) )
3636import Scripting.Lua
37− ( LTYPE(..), LuaState, StackValue(..), getglobal2, ltype, newtable, objlen )
37+ ( LTYPE(..), LuaState, StackValue(..), ltype, newtable, objlen )
3838import Text.Pandoc.Definition
3939import Text.Pandoc.Lua.SharedInstances ()
4040import Text.Pandoc.Lua.Util ( addValue, getTable, pushViaConstructor )
41+import Text.Pandoc.Shared ( safeRead )
4142
4243instance StackValue Pandoc where
4344 push lua (Pandoc meta blocks) = do
@@ -143,19 +144,8 @@ instance StackValue Inline where
143144 valuetype _ = TTABLE
144145
145146instance StackValue Alignment where
146− push lua = \case
147− AlignLeft -> getglobal2 lua "pandoc.AlignLeft"
148− AlignRight -> getglobal2 lua "pandoc.AlignRight"
149− AlignCenter -> getglobal2 lua "pandoc.AlignCenter"
150− AlignDefault -> getglobal2 lua "pandoc.AlignDefault"
151− peek lua idx = do
152− tag <- getTable lua idx "t"
153− case tag of
154− Just "AlignLeft" -> return $ Just AlignLeft
155− Just "AlignRight" -> return $ Just AlignRight
156− Just "AlignCenter" -> return $ Just AlignCenter
157− Just "AlignDefault" -> return $ Just AlignDefault
158− _ -> return Nothing
147+ push lua = push lua . show
148+ peek lua idx = (>>= safeRead) <$> peek lua idx
159149 valuetype _ = TSTRING
160150
161151instance StackValue Citation where
@@ -172,18 +162,9 @@ instance StackValue Citation where
172162 valuetype _ = TTABLE
173163
174164instance StackValue CitationMode where
175− push lua = \case
176− AuthorInText -> getglobal2 lua "pandoc.AuthorInText"
177− NormalCitation -> getglobal2 lua "pandoc.NormalCitation"
178− SuppressAuthor -> getglobal2 lua "pandoc.SuppressAuthor"
179− peek lua idx = do
180− tag <- getTable lua idx "t"
181− case tag of
182− Just "AuthorInText" -> return $ Just AuthorInText
183− Just "NormalCitation" -> return $ Just NormalCitation
184− Just "SuppressAuthor" -> return $ Just SuppressAuthor
185− _ -> return Nothing
186− valuetype _ = TTABLE
165+ push lua = push lua . show
166+ peek lua idx = (>>= safeRead) <$> peek lua idx
167+ valuetype _ = TSTRING
187168
188169instance StackValue Format where
189170 push lua (Format f) = push lua f
@@ -191,66 +172,24 @@ instance StackValue Format where
191172 valuetype _ = TSTRING
192173
193174instance StackValue ListNumberDelim where
194− push lua = \case
195− DefaultDelim -> getglobal2 lua "pandoc.DefaultDelim"
196− Period -> getglobal2 lua "pandoc.Period"
197− OneParen -> getglobal2 lua "pandoc.OneParen"
198− TwoParens -> getglobal2 lua "pandoc.TwoParens"
199− peek lua idx = do
200− tag <- getTable lua idx "t"
201− case tag of
202− Just "DefaultDelim" -> return $ Just DefaultDelim
203− Just "Period" -> return $ Just Period
204− Just "OneParen" -> return $ Just OneParen
205− Just "TwoParens" -> return $ Just TwoParens
206− _ -> return Nothing
207− valuetype _ = TTABLE
175+ push lua = push lua . show
176+ peek lua idx = (>>= safeRead) <$> peek lua idx
177+ valuetype _ = TSTRING
208178
209179instance StackValue ListNumberStyle where
210− push lua = \case
211− DefaultStyle -> getglobal2 lua "pandoc.DefaultStyle"
212− LowerRoman -> getglobal2 lua "pandoc.LowerRoman"
213− UpperRoman -> getglobal2 lua "pandoc.UpperRoman"
214− LowerAlpha -> getglobal2 lua "pandoc.LowerAlpha"
215− UpperAlpha -> getglobal2 lua "pandoc.UpperAlpha"
216− Decimal -> getglobal2 lua "pandoc.Decimal"
217− Example -> getglobal2 lua "pandoc.Example"
218− peek lua idx = do
219− tag <- getTable lua idx "t"
220− case tag of
221− Just "DefaultStyle" -> return $ Just DefaultStyle
222− Just "LowerRoman" -> return $ Just LowerRoman
223− Just "UpperRoman" -> return $ Just UpperRoman
224− Just "LowerAlpha" -> return $ Just LowerAlpha
225− Just "UpperAlpha" -> return $ Just UpperAlpha
226− Just "Decimal" -> return $ Just Decimal
227− Just "Example" -> return $ Just Example
228− _ -> return Nothing
229− valuetype _ = TTABLE
180+ push lua = push lua . show
181+ peek lua idx = (>>= safeRead) <$> peek lua idx
182+ valuetype _ = TSTRING
230183
231184instance StackValue MathType where
232− push lua = \case
233− InlineMath -> getglobal2 lua "pandoc.InlineMath"
234− DisplayMath -> getglobal2 lua "pandoc.DisplayMath"
235− peek lua idx = do
236− res <- getTable lua idx "t"
237− case res of
238− Just "InlineMath" -> return $ Just InlineMath
239− Just "DisplayMath" -> return $ Just DisplayMath
240− _ -> return Nothing
241− valuetype _ = TTABLE
185+ push lua = push lua . show
186+ peek lua idx = (>>= safeRead) <$> peek lua idx
187+ valuetype _ = TSTRING
242188
243189instance StackValue QuoteType where
244− push lua = \case
245− SingleQuote -> getglobal2 lua "pandoc.SingleQuote"
246− DoubleQuote -> getglobal2 lua "pandoc.DoubleQuote"
247− peek lua idx = do
248− res <- getTable lua idx "t"
249− case res of
250− Just "SingleQuote" -> return $ Just SingleQuote
251− Just "DoubleQuote" -> return $ Just DoubleQuote
252− _ -> return Nothing
253− valuetype _ = TTABLE
190+ push lua = push lua . show
191+ peek lua idx = (>>= safeRead) <$> peek lua idx
192+ valuetype _ = TSTRING
254193
255194-- | Return the value at the given index as inline if possible.
256195peekInline :: LuaState -> Int -> IO (Maybe Inline)