@@ -29,22 +29,12 @@ module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
2929
3030import Data.ByteString.Char8 ( unpack )
3131import Data.Default ( Default(..) )
32−import Scripting.Lua ( LuaState, call, newtable, push, pushhsfunction, rawset)
32+import Scripting.Lua ( LuaState, call, push, pushhsfunction, rawset)
3333import Text.Pandoc.Class hiding ( readDataFile )
34−import Text.Pandoc.Definition ( Pandoc(..), Block(..) )
34+import Text.Pandoc.Definition ( Pandoc )
3535import Text.Pandoc.Lua.Compat ( loadstring )
3636import 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 )
37+import Text.Pandoc.Readers ( Reader(..), getReader )
4838import Text.Pandoc.Shared ( readDataFile )
4939
5040-- | Push the "pandoc" on the lua stack.
@@ -56,63 +46,24 @@ pushPandocModule lua = do
5646 then return ()
5747 else do
5848 call lua 0 1
59− push lua "reader"
60− pushReadersModule lua readers
49+ push lua "__read"
50+ pushhsfunction lua read_doc
6151 rawset lua (-3)
6252
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− ]
77−
7853-- | Get the string representation of the pandoc module
7954pandocModuleScript :: IO String
8055pandocModuleScript = unpack <$> readDataFile Nothing "pandoc.lua"
8156
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)
57+read_doc :: String -> String -> IO (Either String Pandoc)
58+read_doc formatSpec content = do
59+ case getReader formatSpec of
60+ Left s -> return $ Left s
61+ Right reader ->
62+ case reader of
63+ StringReader r -> do
64+ res <- runIO $ r def content
65+ case res of
66+ Left s -> return . Left $ show s
67+ Right pd -> return $ Right pd
68+ _ -> return $ Left "Only string formats are supported at the moment."
69+