git.iko.soy/typeable/pandoc
on master help sign in
T.P.Lua: merge runLuaFilter into T.P.Filter.Lua (API change)
7f54f76Albert Krewinkel
src/Text/Pandoc/Filter.hs+3−4
@@ -1,6 +1,7 @@
1{-# LANGUAGE NoImplicitPrelude #-}
1{-# LANGUAGE NoImplicitPrelude #-}
2+{-# LANGUAGE TemplateHaskell #-}
2{-
3{-
3−Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
4+Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
4
5
5This program is free software; you can redistribute it and/or modify
6This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7it under the terms of the GNU General Public License as published by
@@ -16,11 +17,9 @@ You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18-}
19-}
19−{-# LANGUAGE TemplateHaskell #-}
20−
21{- |
20{- |
22 Module : Text.Pandoc.Filter
21 Module : Text.Pandoc.Filter
23− Copyright : Copyright (C) 2006-2017 John MacFarlane
22+ Copyright : Copyright (C) 2006-2018 John MacFarlane
24 License : GNU GPL, version 2 or above
23 License : GNU GPL, version 2 or above
25
24
26 Maintainer : John MacFarlane <jgm@berkeley@edu>
25 Maintainer : John MacFarlane <jgm@berkeley@edu>
src/Text/Pandoc/Filter/Lua.hs+39−8
@@ -32,24 +32,55 @@ module Text.Pandoc.Filter.Lua (apply) where
32
32
33import Prelude
33import Prelude
34import Control.Exception (throw)
34import Control.Exception (throw)
35+import Control.Monad ((>=>))
36+import Foreign.Lua (Lua)
35import Text.Pandoc.Class (PandocIO)
37import Text.Pandoc.Class (PandocIO)
36import Text.Pandoc.Definition (Pandoc)
38import Text.Pandoc.Definition (Pandoc)
37import Text.Pandoc.Error (PandocError (PandocFilterError))
39import Text.Pandoc.Error (PandocError (PandocFilterError))
38import Text.Pandoc.Filter.Path (expandFilterPath)
40import Text.Pandoc.Filter.Path (expandFilterPath)
39−import Text.Pandoc.Lua (LuaException (..), runLuaFilter)
41+import Text.Pandoc.Lua (LuaException (..), runPandocLua)
42+import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
43+import Text.Pandoc.Lua.Global (Global (..), setGlobals)
44+import Text.Pandoc.Lua.Util (dofileWithTraceback)
40import Text.Pandoc.Options (ReaderOptions)
45import Text.Pandoc.Options (ReaderOptions)
41
46
47+import qualified Foreign.Lua as Lua
48+
49+-- | Run the Lua filter in @filterPath@ for a transformation to the
50+-- target format (first element in args). Pandoc uses Lua init files to
51+-- setup the Lua interpreter.
42apply :: ReaderOptions
52apply :: ReaderOptions
43 -> [String]
53 -> [String]
44 -> FilePath
54 -> FilePath
45 -> Pandoc
55 -> Pandoc
46 -> PandocIO Pandoc
56 -> PandocIO Pandoc
47−apply ropts args f d = do
57+apply ropts args f doc = do
48− f' <- expandFilterPath f
58+ filterPath <- expandFilterPath f
49 let format = case args of
59 let format = case args of
50 (x:_) -> x
60 (x:_) -> x
51− _ -> error "Format not supplied for lua filter"
61+ _ -> error "Format not supplied for Lua filter"
52− res <- runLuaFilter ropts f' format d
62+ runPandocLua >=> forceResult filterPath $ do
53− case res of
63+ setGlobals [ FORMAT format
64+ , PANDOC_READER_OPTIONS ropts
65+ , PANDOC_SCRIPT_FILE filterPath
66+ ]
67+ top <- Lua.gettop
68+ stat <- dofileWithTraceback filterPath
69+ if stat /= Lua.OK
70+ then Lua.throwTopMessage
71+ else do
72+ newtop <- Lua.gettop
73+ -- Use the returned filters, or the implicitly defined global
74+ -- filter if nothing was returned.
75+ luaFilters <- if newtop - top >= 1
76+ then Lua.peek Lua.stackTop
77+ else Lua.pushglobaltable *> fmap (:[]) Lua.popValue
78+ runAll luaFilters doc
79+
80+forceResult :: FilePath -> Either LuaException Pandoc -> PandocIO Pandoc
81+forceResult fp eitherResult = case eitherResult of
54− Right x -> return x
82+ Right x -> return x
55− Left (LuaException s) -> throw (PandocFilterError f s)
83+ Left (LuaException s) -> throw (PandocFilterError fp s)
84+
85+runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
86+runAll = foldr ((>=>) . walkMWithLuaFilter) return
src/Text/Pandoc/Lua.hs−40
@@ -28,48 +28,8 @@ Running pandoc Lua filters.
28-}
28-}
29module Text.Pandoc.Lua
29module Text.Pandoc.Lua
30 ( LuaException (..)
30 ( LuaException (..)
31− , runLuaFilter
32 , runPandocLua
31 , runPandocLua
33 ) where
32 ) where
34
33
35−import Prelude
36−import Control.Monad ((>=>))
37−import Foreign.Lua (Lua)
38−import Text.Pandoc.Class (PandocIO)
39−import Text.Pandoc.Definition (Pandoc)
40−import Text.Pandoc.Lua.Global (Global (..), setGlobals)
41−import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
42import Text.Pandoc.Lua.Init (LuaException (..), runPandocLua)
34import Text.Pandoc.Lua.Init (LuaException (..), runPandocLua)
43−import Text.Pandoc.Lua.Util (dofileWithTraceback)
44−import Text.Pandoc.Options (ReaderOptions)
45
35
46−import qualified Foreign.Lua as Lua
47−
48−-- | Run the Lua filter in @filterPath@ for a transformation to target
49−-- format @format@. Pandoc uses Lua init files to setup the Lua
50−-- interpreter.
51−runLuaFilter :: ReaderOptions -> FilePath -> String
52− -> Pandoc -> PandocIO (Either LuaException Pandoc)
53−runLuaFilter ropts filterPath format doc = runPandocLua $ do
54− setGlobals globals
55− top <- Lua.gettop
56− stat <- dofileWithTraceback filterPath
57− if stat /= Lua.OK
58− then Lua.throwTopMessage
59− else do
60− newtop <- Lua.gettop
61− -- Use the returned filters, or the implicitly defined global filter if
62− -- nothing was returned.
63− luaFilters <- if newtop - top >= 1
64− then Lua.peek Lua.stackTop
65− else Lua.pushglobaltable *> fmap (:[]) Lua.popValue
66− runAll luaFilters doc
67−
68− where
69− globals = [ FORMAT format
70− , PANDOC_READER_OPTIONS ropts
71− , PANDOC_SCRIPT_FILE filterPath
72− ]
73−
74−runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
75−runAll = foldr ((>=>) . walkMWithLuaFilter) return
test/Tests/Lua.hs+7−8
@@ -7,7 +7,7 @@ import Control.Monad (when)
7import Data.Version (Version (versionBranch))
7import Data.Version (Version (versionBranch))
8import System.FilePath ((</>))
8import System.FilePath ((</>))
9import Test.Tasty (TestTree, localOption)
9import Test.Tasty (TestTree, localOption)
10−import Test.Tasty.HUnit (Assertion, assertEqual, assertFailure, testCase)
10+import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
11import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
11import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
12import Text.Pandoc.Arbitrary ()
12import Text.Pandoc.Arbitrary ()
13import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
13import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
@@ -17,7 +17,8 @@ import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
17import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
17import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
18import Text.Pandoc.Definition (Block (BlockQuote, Div, Para), Inline (Emph, Str),
18import Text.Pandoc.Definition (Block (BlockQuote, Div, Para), Inline (Emph, Str),
19 Attr, Meta, Pandoc, pandocTypesVersion)
19 Attr, Meta, Pandoc, pandocTypesVersion)
20−import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
20+import Text.Pandoc.Filter (Filter (LuaFilter), applyFilters)
21+import Text.Pandoc.Lua (runPandocLua)
21import Text.Pandoc.Options (def)
22import Text.Pandoc.Options (def)
22import Text.Pandoc.Shared (pandocVersion)
23import Text.Pandoc.Shared (pandocVersion)
23
24
@@ -174,13 +175,11 @@ tests = map (localOption (QuickCheckTests 20))
174 ]
175 ]
175
176
176assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
177assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
177−assertFilterConversion msg filterPath docIn docExpected = do
178+assertFilterConversion msg filterPath docIn expectedDoc = do
178− docEither <- runIOorExplode $ do
179+ actualDoc <- runIOorExplode $ do
179 setUserDataDir (Just "../data")
180 setUserDataDir (Just "../data")
180− runLuaFilter def ("lua" </> filterPath) [] docIn
181+ applyFilters def [LuaFilter ("lua" </> filterPath)] ["HTML"] docIn
181− case docEither of
182+ assertEqual msg expectedDoc actualDoc
182− Left exception -> assertFailure (show exception)
183− Right docRes -> assertEqual msg docExpected docRes
184
183
185roundtripEqual :: (Eq a, Lua.Peekable a, Lua.Pushable a) => a -> IO Bool
184roundtripEqual :: (Eq a, Lua.Peekable a, Lua.Pushable a) => a -> IO Bool
186roundtripEqual x = (x ==) <$> roundtripped
185roundtripEqual x = (x ==) <$> roundtripped