git.iko.soy/typeable/pandoc
on master help sign in
T.P.Lua: merge runLuaFilter into T.P.Filter.Lua (API change)

The function `runLuaFilter` was only used in Text.Pandoc.Filter.Lua, use apply from the that module instead.

commit7f54f76e8b5a7b45cd61a354980ef77f65baba20 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 26 Oct 2018, 23:21 (8 years ago) parent0531a46
4 files changed+49−60
src/Text/Pandoc/Filter.hs+3−4 src/Text/Pandoc/Filter/Lua.hs+39−8 src/Text/Pandoc/Lua.hs−40 test/Tests/Lua.hs+7−8
src/Text/Pandoc/Filter.hs+3−4
@@ -1,6 +1,7 @@
11{-# LANGUAGE NoImplicitPrelude #-}
2+{-# LANGUAGE TemplateHaskell #-}
23{-
3−Copyright (C) 2006-2017 John MacFarlane <jgm@berkeley.edu>
4+Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
45
56This program is free software; you can redistribute it and/or modify
67it 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
1617along with this program; if not, write to the Free Software
1718Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1819-}
19−{-# LANGUAGE TemplateHaskell #-}
20−
2120{- |
2221 Module : Text.Pandoc.Filter
23− Copyright : Copyright (C) 2006-2017 John MacFarlane
22+ Copyright : Copyright (C) 2006-2018 John MacFarlane
2423 License : GNU GPL, version 2 or above
2524
2625 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
3232
3333import Prelude
3434import Control.Exception (throw)
35+import Control.Monad ((>=>))
36+import Foreign.Lua (Lua)
3537import Text.Pandoc.Class (PandocIO)
3638import Text.Pandoc.Definition (Pandoc)
3739import Text.Pandoc.Error (PandocError (PandocFilterError))
3840import 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)
4045import Text.Pandoc.Options (ReaderOptions)
4146
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.
4252apply :: ReaderOptions
4353 -> [String]
4454 -> FilePath
4555 -> Pandoc
4656 -> PandocIO Pandoc
47−apply ropts args f d = do
48− f' <- expandFilterPath f
57+apply ropts args f doc = do
58+ filterPath <- expandFilterPath f
4959 let format = case args of
5060 (x:_) -> x
51− _ -> error "Format not supplied for lua filter"
52− res <- runLuaFilter ropts f' format d
53− case res of
54− Right x -> return x
55− Left (LuaException s) -> throw (PandocFilterError f s)
61+ _ -> error "Format not supplied for Lua filter"
62+ runPandocLua >=> forceResult filterPath $ do
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
82+ Right x -> return x
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.
2828-}
2929module Text.Pandoc.Lua
3030 ( LuaException (..)
31− , runLuaFilter
3231 , runPandocLua
3332 ) where
3433
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)
4234import Text.Pandoc.Lua.Init (LuaException (..), runPandocLua)
43−import Text.Pandoc.Lua.Util (dofileWithTraceback)
44−import Text.Pandoc.Options (ReaderOptions)
4535
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)
77import Data.Version (Version (versionBranch))
88import System.FilePath ((</>))
99import Test.Tasty (TestTree, localOption)
10−import Test.Tasty.HUnit (Assertion, assertEqual, assertFailure, testCase)
10+import Test.Tasty.HUnit (Assertion, assertEqual, testCase)
1111import Test.Tasty.QuickCheck (QuickCheckTests (..), ioProperty, testProperty)
1212import Text.Pandoc.Arbitrary ()
1313import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
@@ -17,7 +17,8 @@ import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
1717import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
1818import Text.Pandoc.Definition (Block (BlockQuote, Div, Para), Inline (Emph, Str),
1919 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)
2122import Text.Pandoc.Options (def)
2223import Text.Pandoc.Shared (pandocVersion)
2324
@@ -174,13 +175,11 @@ tests = map (localOption (QuickCheckTests 20))
174175 ]
175176
176177assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
177−assertFilterConversion msg filterPath docIn docExpected = do
178− docEither <- runIOorExplode $ do
178+assertFilterConversion msg filterPath docIn expectedDoc = do
179+ actualDoc <- runIOorExplode $ do
179180 setUserDataDir (Just "../data")
180− runLuaFilter def ("lua" </> filterPath) [] docIn
181− case docEither of
182− Left exception -> assertFailure (show exception)
183− Right docRes -> assertEqual msg docExpected docRes
181+ applyFilters def [LuaFilter ("lua" </> filterPath)] ["HTML"] docIn
182+ assertEqual msg expectedDoc actualDoc
184183
185184roundtripEqual :: (Eq a, Lua.Peekable a, Lua.Pushable a) => a -> IO Bool
186185roundtripEqual x = (x ==) <$> roundtripped