git.iko.soy/typeable/pandoc
on master help sign in
Lua module: provide simple `read` format parser
24ef672Albert Krewinkel
data/pandoc.lua+14
@@ -796,6 +796,20 @@ M.UpperAlpha = "UpperAlpha"
796-- Helper Functions
796-- Helper Functions
797-- @section helpers
797-- @section helpers
798
798
799+--- Parse the given string into a Pandoc document.
800+-- @tparam string markup the markup to be parsed
801+-- @tparam[opt] string format format specification, defaults to "markdown".
802+-- @return Doc pandoc document
803+function M.read(markup, format)
804+ format = format or "markdown"
805+ local pd = pandoc.__read(format, markup)
806+ if type(pd) == "string" then
807+ error(pd)
808+ else
809+ return pd
810+ end
811+end
812+
799--- Use functions defined in the global namespace to create a pandoc filter.
813--- Use functions defined in the global namespace to create a pandoc filter.
800-- All globally defined functions which have names of pandoc elements are
814-- All globally defined functions which have names of pandoc elements are
801-- collected into a new table.
815-- collected into a new table.
src/Text/Pandoc/Lua/PandocModule.hs+18−67
@@ -29,22 +29,12 @@ module Text.Pandoc.Lua.PandocModule ( pushPandocModule ) where
29
29
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 )
49
39
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 ()
57 else do
47 else do
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)
62
52
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−
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"
81
56
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
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
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."
101− readBlock s = do
69+
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)
src/Text/Pandoc/Lua/SharedInstances.hs+10
@@ -104,3 +104,13 @@ instance (Ord a, StackValue a, StackValue b) =>
104 mapM_ (uncurry $ addValue lua) $ M.toList m
104 mapM_ (uncurry $ addValue lua) $ M.toList m
105 peek lua idx = fmap M.fromList <$> keyValuePairs lua idx
105 peek lua idx = fmap M.fromList <$> keyValuePairs lua idx
106 valuetype _ = TTABLE
106 valuetype _ = TTABLE
107+
108+instance (StackValue a, StackValue b) => StackValue (Either a b) where
109+ push lua = \case
110+ Left x -> push lua x
111+ Right x -> push lua x
112+ peek lua idx = peek lua idx >>= \case
113+ Just left -> return . Just $ Left left
114+ Nothing -> fmap Right <$> peek lua idx
115+ valuetype (Left x) = valuetype x
116+ valuetype (Right x) = valuetype x
test/lua/markdown-reader.lua+2−1
@@ -2,7 +2,8 @@ return {
2 {
2 {
3 RawBlock = function (elem)
3 RawBlock = function (elem)
4 if elem.format == "markdown" then
4 if elem.format == "markdown" then
5− return pandoc.reader.markdown.read_block(elem.text)
5+ local pd = pandoc.read(elem.text, "markdown")
6+ return pd.blocks[1]
6 else
7 else
7 return elem
8 return elem
8 end
9 end