@@ -29,22 +29,12 @@ module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
30import Data.ByteString.Char8 ( unpack )
30import Data.ByteString.Char8 ( unpack )
31import Data.Default ( Default(..) )
31import Data.Default ( Default(..) )
32−import Scripting.Lua ( LuaState, call, newtable, push, pushhsfunction, rawset)
32+import Scripting.Lua ( LuaState, call, push, pushhsfunction, rawset)
33import Text.Pandoc.Class hiding ( readDataFile )
33import Text.Pandoc.Class hiding ( readDataFile )
34−import Text.Pandoc.Definition ( Pandoc(..), Block(..) )
34+import Text.Pandoc.Definition ( Pandoc )
35import Text.Pandoc.Lua.Compat ( loadstring )
35import Text.Pandoc.Lua.Compat ( loadstring )
36import Text.Pandoc.Lua.StackInstances ()
36import Text.Pandoc.Lua.StackInstances ()
37−import Text.Pandoc.Readers.DocBook ( readDocBook )
37+import Text.Pandoc.Readers ( Reader(..), getReader )
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 )
48import Text.Pandoc.Shared ( readDataFile )
38import Text.Pandoc.Shared ( readDataFile )
50-- | Push the "pandoc" on the lua stack.
40-- | Push the "pandoc" on the lua stack.
@@ -56,63 +46,24 @@ pushPandocModule lua = do
56 then return ()
46 then return ()
58 call lua 0 1
48 call lua 0 1
59− push lua "reader"
49+ push lua "__read"
60− pushReadersModule lua readers
50+ pushhsfunction lua read_doc
61 rawset lua (-3)
51 rawset lua (-3)
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)
78-- | Get the string representation of the pandoc module
53-- | Get the string representation of the pandoc module
79pandocModuleScript :: IO String
54pandocModuleScript :: IO String
80pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
55pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
82−-- | Push a lua table containing readers of the given formats.
57+read_doc :: String -> String -> IO (Either String Pandoc)
83−pushReadersModule :: LuaState
58+read_doc formatSpec content = do
84− -> [(String, String -> PandocIO Pandoc)]
59+ case getReader formatSpec of
85− -> IO ()
60+ Left s -> return $ Left s
86−pushReadersModule lua readerFns = do
61+ Right reader ->
87− newtable lua
62+ case reader of
88− mapM_ (uncurry $ addReaderTable) readerFns
63+ StringReader r -> do
90− addReaderTable :: String
91− -> (String -> PandocIO Pandoc)
93− addReaderTable formatName readerFn = do
94− let readDoc :: String -> IO Pandoc
96− res <- runIO $ readerFn s
64+ res <- runIO $ r def content
97− case res of
65+ case res of
98− (Left x) -> error (show x)
66+ Left s -> return . Left $ show s
99− (Right x) -> return x
67+ Right pd -> return $ Right pd
100− let readBlock :: String -> IO Block
68+ _ -> return $ Left "Only string formats are supported at the moment."
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