@@ -36,7 +36,7 @@ StackValue instances for pandoc types.
36module Text.Pandoc.Lua.StackInstances () where
36module Text.Pandoc.Lua.StackInstances () where
38import Data.Aeson ( FromJSON(..), ToJSON(..), Result(..), Value, fromJSON )
38import Data.Aeson ( FromJSON(..), ToJSON(..), Result(..), Value, fromJSON )
39−import Scripting.Lua ( StackValue(..) )
39+import Scripting.Lua ( LuaState, StackValue(..), newtable, pop, rawget, rawset )
40import Scripting.Lua.Aeson ()
40import Scripting.Lua.Aeson ()
41import Text.Pandoc.Definition ( Block(..), Inline(..), Pandoc(..) )
41import Text.Pandoc.Definition ( Block(..), Inline(..), Pandoc(..) )
@@ -59,8 +59,20 @@ instance StackValue Block where
59 valuetype _ = Lua.TTABLE
59 valuetype _ = Lua.TTABLE
61instance StackValue Inline where
61instance StackValue Inline where
62− push lua = Lua.push lua . toJSON
62+ push lua = \case
63− peek lua i = maybeFromJson <$> peek lua i
63+ Emph inlns -> pushTagged lua "Emph" inlns
64+ LineBreak -> pushTagged' lua "LineBreak"
65+ Note blcks -> pushTagged lua "Note" blcks
66+ SmallCaps inlns -> pushTagged lua "SmallCaps" inlns
67+ SoftBreak -> pushTagged' lua "SoftBreak"
68+ Space -> pushTagged' lua "Space"
69+ Str s -> pushTagged lua "Str" s
70+ Strikeout inlns -> pushTagged lua "Strikeout" inlns
71+ Strong inlns -> pushTagged lua "Strong" inlns
72+ Subscript inlns -> pushTagged lua "Subscript" inlns
73+ Superscript inlns -> pushTagged lua "Superscript" inlns
74+ x -> push lua (toJSON x)
64 valuetype _ = Lua.TTABLE
76 valuetype _ = Lua.TTABLE
66#if MIN_VERSION_base(4,8,0)
78#if MIN_VERSION_base(4,8,0)
@@ -68,8 +80,62 @@ instance {-# OVERLAPS #-} StackValue [Char] where
69instance StackValue [Char] where
81instance StackValue [Char] where
71− push lua cs = Lua.push lua (UTF8.fromString cs)
83+ push lua cs = push lua (UTF8.fromString cs)
72− peek lua i = do
84+ peek lua i = fmap UTF8.toString <$> peek lua i
73− res <- Lua.peek lua i
74− return $ UTF8.toString `fmap` res
75 valuetype _ = Lua.TSTRING
85 valuetype _ = Lua.TSTRING
87+-- | Push a value to the lua stack, tagged with a given string. This currently
88+-- creates a structure equivalent to what the JSONified value would look like
89+-- when pushed to lua.
90+pushTagged :: StackValue a => LuaState -> String -> a -> IO ()
91+pushTagged lua tag value = do
100+pushTagged' :: LuaState -> String -> IO ()
101+pushTagged' lua tag = do
107+-- | Return the value at the given index as inline if possible.
108+peekInline :: LuaState -> Int -> IO (Maybe Inline)
109+peekInline lua idx = do
111+ rawget lua (idx `adjustIndexBy` 1)
112+ tag <- peek lua (-1) <* pop lua 1
114+ Nothing -> return Nothing
116+ "Emph" -> fmap Emph <$> elementContent
117+ "LineBreak" -> return (Just LineBreak)
118+ "Note" -> fmap Note <$> elementContent
119+ "SmallCaps" -> fmap SmallCaps <$> elementContent
120+ "SoftBreak" -> return (Just SoftBreak)
121+ "Space" -> return (Just Space)
122+ "Str" -> fmap Str <$> elementContent
123+ "Strikeout" -> fmap Strikeout <$> elementContent
124+ "Strong" -> fmap Strong <$> elementContent
125+ "Subscript" -> fmap Subscript <$> elementContent
126+ "Superscript"-> fmap Superscript <$> elementContent
127+ _ -> maybeFromJson <$> peek lua idx
129+ elementContent :: StackValue a => IO (Maybe a)
132+ rawget lua (idx `adjustIndexBy` 1)
133+ peek lua (-1) <* pop lua 1
135+-- | Adjust the stack index, assuming that @n@ new elements have been pushed on
137+adjustIndexBy :: Int -> Int -> Int
138+adjustIndexBy idx n =