@@ -36,25 +36,17 @@ StackValue instances for pandoc types.
36module Text.Pandoc.Lua.StackInstances () where
36module Text.Pandoc.Lua.StackInstances () where
38import Control.Applicative ( (<|>) )
38import Control.Applicative ( (<|>) )
39−import Data.Aeson ( FromJSON(..), ToJSON(..), Result(..), Value, fromJSON )
40import Scripting.Lua
39import Scripting.Lua
41 ( LTYPE(..), LuaState, StackValue(..)
40 ( LTYPE(..), LuaState, StackValue(..)
42 , call, getglobal2, gettable, ltype, newtable, next, objlen
41 , call, getglobal2, gettable, ltype, newtable, next, objlen
43− , pop, pushnil, rawgeti, rawset, rawseti, settable
42+ , pop, pushnil, rawgeti, rawseti, settable
45import Scripting.Lua.Aeson ()
44import Scripting.Lua.Aeson ()
46import Text.Pandoc.Definition
45import Text.Pandoc.Definition
47− ( Block(..), Inline(..), Meta(..), MetaValue(..), Pandoc(..)
48− , Citation(..), CitationMode(..), Format(..), MathType(..), QuoteType(..) )
50import qualified Data.Map as M
47import qualified Data.Map as M
51import qualified Text.Pandoc.UTF8 as UTF8
48import qualified Text.Pandoc.UTF8 as UTF8
53−maybeFromJson :: (FromJSON a) => Maybe Value -> Maybe a
54−maybeFromJson mv = fromJSON <$> mv >>= \case
58instance StackValue Pandoc where
50instance StackValue Pandoc where
59 push lua (Pandoc meta blocks) = do
51 push lua (Pandoc meta blocks) = do
60 newtable lua
52 newtable lua
@@ -121,16 +113,22 @@ peekContent lua idx = do
122instance StackValue Block where
114instance StackValue Block where
123 push lua = \case
115 push lua = \case
124− BlockQuote blcks -> pushTagged lua "BlockQuote" blcks
116+ BlockQuote blcks -> pushViaConstructor lua "BlockQuote" blcks
125− BulletList items -> pushTagged lua "BulletList" items
117+ BulletList items -> pushViaConstructor lua "BulletList" items
118+ CodeBlock attr code -> pushViaConstructor lua "CodeBlock" code attr
119+ DefinitionList items -> pushViaConstructor lua "DefinitionList" items
120+ Div attr blcks -> pushViaConstructor lua "Div" blcks attr
121+ Header lvl attr inlns -> pushViaConstructor lua "Header" lvl attr inlns
126− HorizontalRule -> pushTagged' lua "HorizontalRule"
122+ HorizontalRule -> pushViaConstructor lua "HorizontalRule"
127− LineBlock blcks -> pushTagged lua "LineBlock" blcks
123+ LineBlock blcks -> pushViaConstructor lua "LineBlock" blcks
128− Null -> pushTagged' lua "Null"
124+ OrderedList lstAttr list -> pushViaConstructor lua "OrderedList" list lstAttr
129− Para blcks -> pushTagged lua "Para" blcks
125+ Null -> pushViaConstructor lua "Null"
130− Plain blcks -> pushTagged lua "Plain" blcks
126+ Para blcks -> pushViaConstructor lua "Para" blcks
131− RawBlock f cs -> pushTagged lua "RawBlock" (f, cs)
127+ Plain blcks -> pushViaConstructor lua "Plain" blcks
128+ RawBlock f cs -> pushViaConstructor lua "RawBlock" f cs
129+ Table capt aligns widths headers rows ->
130+ pushViaConstructor lua "Table" capt aligns widths headers rows
132 -- fall back to conversion via aeson's Value
131 -- fall back to conversion via aeson's Value
133− x -> push lua (toJSON x)
134 peek lua i = peekBlock lua i
132 peek lua i = peekBlock lua i
135 valuetype _ = TTABLE
133 valuetype _ = TTABLE
@@ -158,6 +156,22 @@ instance StackValue Inline where
158 peek = peekInline
156 peek = peekInline
159 valuetype _ = TTABLE
157 valuetype _ = TTABLE
159+instance StackValue Alignment where
161+ AlignLeft -> getglobal2 lua "pandoc.AlignLeft"
162+ AlignRight -> getglobal2 lua "pandoc.AlignRight"
163+ AlignCenter -> getglobal2 lua "pandoc.AlignCenter"
164+ AlignDefault -> getglobal2 lua "pandoc.AlignDefault"
166+ tag <- getField lua idx "t"
168+ Just "AlignLeft" -> return $ Just AlignLeft
169+ Just "AlignRight" -> return $ Just AlignRight
170+ Just "AlignCenter" -> return $ Just AlignCenter
171+ Just "AlignDefault" -> return $ Just AlignDefault
173+ valuetype _ = TSTRING
161instance StackValue Citation where
175instance StackValue Citation where
162 push lua (Citation cid prefix suffix mode noteNum hash) =
176 push lua (Citation cid prefix suffix mode noteNum hash) =
163 pushViaConstructor lua "Citation" cid mode prefix suffix noteNum hash
177 pushViaConstructor lua "Citation" cid mode prefix suffix noteNum hash
@@ -183,13 +197,51 @@ instance StackValue CitationMode where
183 Just "NormalCitation" -> return $ Just NormalCitation
197 Just "NormalCitation" -> return $ Just NormalCitation
184 Just "SuppressAuthor" -> return $ Just SuppressAuthor
198 Just "SuppressAuthor" -> return $ Just SuppressAuthor
185 _ -> return Nothing
199 _ -> return Nothing
186− valuetype _ = TSTRING
200+ valuetype _ = TTABLE
188instance StackValue Format where
202instance StackValue Format where
189 push lua (Format f) = push lua f
203 push lua (Format f) = push lua f
190 peek lua idx = fmap Format <$> peek lua idx
204 peek lua idx = fmap Format <$> peek lua idx
191 valuetype _ = TSTRING
205 valuetype _ = TSTRING
207+instance StackValue ListNumberDelim where
209+ DefaultDelim -> getglobal2 lua "pandoc.DefaultDelim"
210+ Period -> getglobal2 lua "pandoc.Period"
211+ OneParen -> getglobal2 lua "pandoc.OneParen"
212+ TwoParens -> getglobal2 lua "pandoc.TwoParens"
214+ tag <- getField lua idx "t"
216+ Just "DefaultDelim" -> return $ Just DefaultDelim
217+ Just "Period" -> return $ Just Period
218+ Just "OneParen" -> return $ Just OneParen
219+ Just "TwoParens" -> return $ Just TwoParens
221+ valuetype _ = TTABLE
223+instance StackValue ListNumberStyle where
225+ DefaultStyle -> getglobal2 lua "pandoc.DefaultStyle"
226+ LowerRoman -> getglobal2 lua "pandoc.LowerRoman"
227+ UpperRoman -> getglobal2 lua "pandoc.UpperRoman"
228+ LowerAlpha -> getglobal2 lua "pandoc.LowerAlpha"
229+ UpperAlpha -> getglobal2 lua "pandoc.UpperAlpha"
230+ Decimal -> getglobal2 lua "pandoc.Decimal"
231+ Example -> getglobal2 lua "pandoc.Example"
233+ tag <- getField lua idx "t"
235+ Just "DefaultStyle" -> return $ Just DefaultStyle
236+ Just "LowerRoman" -> return $ Just LowerRoman
237+ Just "UpperRoman" -> return $ Just UpperRoman
238+ Just "LowerAlpha" -> return $ Just LowerAlpha
239+ Just "UpperAlpha" -> return $ Just UpperAlpha
240+ Just "Decimal" -> return $ Just Decimal
241+ Just "Example" -> return $ Just Example
243+ valuetype _ = TTABLE
193instance StackValue MathType where
245instance StackValue MathType where
194 push lua = \case
246 push lua = \case
195 InlineMath -> getglobal2 lua "pandoc.InlineMath"
247 InlineMath -> getglobal2 lua "pandoc.InlineMath"
@@ -249,6 +301,26 @@ instance (StackValue a, StackValue b, StackValue c) =>
249 return $ (,,) <$> a <*> b <*> c
301 return $ (,,) <$> a <*> b <*> c
250 valuetype _ = TTABLE
302 valuetype _ = TTABLE
304+instance (StackValue a, StackValue b, StackValue c,
305+ StackValue d, StackValue e) =>
306+ StackValue (a, b, c, d, e)
308+ push lua (a, b, c, d, e) = do
310+ addIndexedValue lua 1 a
311+ addIndexedValue lua 2 b
312+ addIndexedValue lua 3 c
313+ addIndexedValue lua 4 d
314+ addIndexedValue lua 5 e
316+ a <- getIndexedValue lua idx 1
317+ b <- getIndexedValue lua idx 2
318+ c <- getIndexedValue lua idx 3
319+ d <- getIndexedValue lua idx 4
320+ e <- getIndexedValue lua idx 5
321+ return $ (,,,,) <$> a <*> b <*> c <*> d <*> e
322+ valuetype _ = TTABLE
252instance (Ord a, StackValue a, StackValue b) =>
324instance (Ord a, StackValue a, StackValue b) =>
253 StackValue (M.Map a b) where
325 StackValue (M.Map a b) where
254 push lua m = do
326 push lua m = do
@@ -307,22 +379,6 @@ pushViaCall lua fn = pushViaCall' lua fn (return ()) 0
307pushViaConstructor :: PushViaCall a => LuaState -> String -> a
379pushViaConstructor :: PushViaCall a => LuaState -> String -> a
308pushViaConstructor lua pandocFn = pushViaCall lua ("pandoc." ++ pandocFn)
380pushViaConstructor lua pandocFn = pushViaCall lua ("pandoc." ++ pandocFn)
310−-- | Push a value to the lua stack, tagged with a given string. This currently
311−-- creates a structure equivalent to what the JSONified value would look like
312−-- when pushed to lua.
313−pushTagged :: StackValue a => LuaState -> String -> a -> IO ()
314−pushTagged lua tag value = do
316− addKeyValue lua "t" tag
317− addKeyValue lua "c" value
319−pushTagged' :: LuaState -> String -> IO ()
320−pushTagged' lua tag = do
326-- | Return the value at the given index as inline if possible.
382-- | Return the value at the given index as inline if possible.
327peekInline :: LuaState -> Int -> IO (Maybe Inline)
383peekInline :: LuaState -> Int -> IO (Maybe Inline)
328peekInline lua idx = do
384peekInline lua idx = do
@@ -366,13 +422,22 @@ peekBlock lua idx = do
366 Just t -> case t of
422 Just t -> case t of
367 "BlockQuote" -> fmap BlockQuote <$> elementContent
423 "BlockQuote" -> fmap BlockQuote <$> elementContent
368 "BulletList" -> fmap BulletList <$> elementContent
424 "BulletList" -> fmap BulletList <$> elementContent
425+ "CodeBlock" -> fmap (uncurry CodeBlock) <$> elementContent
426+ "DefinitionList" -> fmap DefinitionList <$> elementContent
427+ "Div" -> fmap (uncurry Div) <$> elementContent
428+ "Header" -> fmap (\(lvl, attr, lst) -> Header lvl attr lst)
369 "HorizontalRule" -> return (Just HorizontalRule)
430 "HorizontalRule" -> return (Just HorizontalRule)
370 "LineBlock" -> fmap LineBlock <$> elementContent
431 "LineBlock" -> fmap LineBlock <$> elementContent
432+ "OrderedList" -> fmap (uncurry OrderedList) <$> elementContent
371 "Null" -> return (Just Null)
433 "Null" -> return (Just Null)
372 "Para" -> fmap Para <$> elementContent
434 "Para" -> fmap Para <$> elementContent
373 "Plain" -> fmap Plain <$> elementContent
435 "Plain" -> fmap Plain <$> elementContent
374− -- fall back to construction via aeson's Value
436+ "RawBlock" -> fmap (uncurry RawBlock) <$> elementContent
375− _ -> maybeFromJson <$> peek lua idx
437+ "Table" -> fmap (\(capt, aligns, widths, headers, body) ->
438+ Table capt aligns widths headers body)
377 -- Get the contents of an AST element.
442 -- Get the contents of an AST element.
378 elementContent :: StackValue a => IO (Maybe a)
443 elementContent :: StackValue a => IO (Maybe a)