@@ -28,11 +28,25 @@ Pandoc module for lua.
2828module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
2929
3030import Data.ByteString.Char8 ( unpack )
31−import Scripting.Lua ( LuaState, call)
31+import Data.Default ( Default(..) )
32+import Scripting.Lua ( LuaState, call, newtable, push, pushhsfunction, rawset)
33+import Text.Pandoc.Class hiding ( readDataFile )
34+import Text.Pandoc.Definition ( Pandoc(..), Block(..) )
3235import Text.Pandoc.Lua.Compat ( loadstring )
36+import Text.Pandoc.Lua.StackInstances ()
37+import Text.Pandoc.Readers.DocBook ( readDocBook )
38+import Text.Pandoc.Readers.HTML ( readHtml )
39+import Text.Pandoc.Readers.LaTeX ( readLaTeX )
40+import Text.Pandoc.Readers.Native ( readNative )
41+import Text.Pandoc.Readers.Markdown ( readMarkdown )
42+import Text.Pandoc.Readers.MediaWiki ( readMediaWiki )
43+import Text.Pandoc.Readers.Org ( readOrg )
44+import Text.Pandoc.Readers.RST ( readRST )
45+import Text.Pandoc.Readers.Textile ( readTextile )
46+import Text.Pandoc.Readers.TWiki ( readTWiki )
47+import Text.Pandoc.Readers.Txt2Tags ( readTxt2Tags )
3348import Text.Pandoc.Shared ( readDataFile )
3449
35−
3650-- | Push the "pandoc" on the lua stack.
3751pushPandocModule :: LuaState -> IO ()
3852pushPandocModule lua = do
@@ -42,7 +56,63 @@ pushPandocModule lua = do
4256 then return ()
4357 else do
4458 call lua 0 1
59+ push lua "reader"
60+ pushReadersModule lua readers
61+ rawset lua (-3)
62+
63+readers :: [(String, String -> PandocIO Pandoc)]
64+readers =
65+ [ ("docbook", readDocBook def)
66+ , ("html", readHtml def)
67+ , ("latex", readLaTeX def)
68+ , ("native", readNative def)
69+ , ("markdown", readMarkdown def)
70+ , ("mediawiki", readMediaWiki def)
71+ , ("org", readOrg def)
72+ , ("rst", readRST def)
73+ , ("textile", readTextile def)
74+ , ("twiki", readTWiki def)
75+ , ("txt2tags", readTxt2Tags def)
76+ ]
4577
4678-- | Get the string representation of the pandoc module
4779pandocModuleScript :: IO String
4880pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
81+
82+-- | Push a lua table containing readers of the given formats.
83+pushReadersModule :: LuaState
84+ -> [(String, String -> PandocIO Pandoc)]
85+ -> IO ()
86+pushReadersModule lua readerFns = do
87+ newtable lua
88+ mapM_ (uncurry $ addReaderTable) readerFns
89+ where
90+ addReaderTable :: String
91+ -> (String -> PandocIO Pandoc)
92+ -> IO ()
93+ addReaderTable formatName readerFn = do
94+ let readDoc :: String -> IO Pandoc
95+ readDoc s = do
96+ res <- runIO $ readerFn s
97+ case res of
98+ (Left x) -> error (show x)
99+ (Right x) -> return x
100+ let readBlock :: String -> IO Block
101+ readBlock s = do
102+ Pandoc _ blks <- readDoc s
103+ return $ case blks of
104+ x:_ -> x
105+ _ -> Null
106+ -- Push table containing all functions for this format
107+ push lua formatName
108+ newtable lua
109+ -- set document-reading function
110+ push lua "read_doc"
111+ pushhsfunction lua readDoc
112+ rawset lua (-3)
113+ -- set block-reading function
114+ push lua "read_block"
115+ pushhsfunction lua readBlock
116+ rawset lua (-3)
117+ -- store table in readers module
118+ rawset lua (-3)