@@ -28,11 +28,25 @@ Pandoc module for lua.
28module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
28module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
30import Data.ByteString.Char8 ( unpack )
30import Data.ByteString.Char8 ( unpack )
31+import Data.Default ( Default(..) )
31−import Scripting.Lua ( LuaState, call)
32+import Scripting.Lua ( LuaState, call, newtable, push, pushhsfunction, rawset)
33+import Text.Pandoc.Class hiding ( readDataFile )
34+import Text.Pandoc.Definition ( Pandoc(..), Block(..) )
32import Text.Pandoc.Lua.Compat ( loadstring )
35import 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 )
33import Text.Pandoc.Shared ( readDataFile )
48import Text.Pandoc.Shared ( readDataFile )
36-- | Push the "pandoc" on the lua stack.
50-- | Push the "pandoc" on the lua stack.
37pushPandocModule :: LuaState -> IO ()
51pushPandocModule :: LuaState -> IO ()
38pushPandocModule lua = do
52pushPandocModule lua = do
@@ -42,7 +56,63 @@ pushPandocModule lua = do
42 then return ()
56 then return ()
44 call lua 0 1
58 call lua 0 1
60+ pushReadersModule lua readers
63+readers :: [(String, String -> PandocIO Pandoc)]
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)
46-- | Get the string representation of the pandoc module
78-- | Get the string representation of the pandoc module
47pandocModuleScript :: IO String
79pandocModuleScript :: IO String
48pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
80pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
82+-- | Push a lua table containing readers of the given formats.
83+pushReadersModule :: LuaState
84+ -> [(String, String -> PandocIO Pandoc)]
86+pushReadersModule lua readerFns = do
88+ mapM_ (uncurry $ addReaderTable) readerFns
90+ addReaderTable :: String
91+ -> (String -> PandocIO Pandoc)
93+ addReaderTable formatName readerFn = do
94+ let readDoc :: String -> IO Pandoc
96+ res <- runIO $ readerFn s
98+ (Left x) -> error (show x)
99+ (Right x) -> return x
100+ let readBlock :: String -> IO Block
102+ Pandoc _ blks <- readDoc s
103+ return $ case blks of
106+ -- Push table containing all functions for this format
109+ -- set document-reading function
111+ pushhsfunction lua readDoc
113+ -- set block-reading function
114+ push lua "read_block"
115+ pushhsfunction lua readBlock
117+ -- store table in readers module