mirror of
https://github.com/jgm/pandoc.git
synced 2026-09-23 07:55:45 +00:00
Export applyFilter from T.P.Lua.Filter.
Motivation: now we need not have any logic referring to hslua types (LuaE) in T.P.Filter.Lua. This restricts the parts of Pandoc that deal directly with hslua to T.P.Lua (and in tests, Tests.Lua). [API changes] T.P.Lua now exports `applyFilter`, `readCustom`, and `writeCustom`. The lower-level function `runFilterFile` is no longer exported.
This commit is contained in:
@@ -11,15 +11,12 @@ Apply Lua filters to modify a pandoc documents programmatically.
|
||||
-}
|
||||
module Text.Pandoc.Filter.Lua (apply) where
|
||||
|
||||
import Control.Exception (throw)
|
||||
import Control.Monad ((>=>))
|
||||
import qualified Data.Text as T
|
||||
import Text.Pandoc.Class (PandocMonad)
|
||||
import Control.Monad.Trans (MonadIO)
|
||||
import Text.Pandoc.Definition (Pandoc)
|
||||
import Text.Pandoc.Error (PandocError (PandocFilterError, PandocLuaError))
|
||||
import Text.Pandoc.Filter.Environment (Environment (..))
|
||||
import Text.Pandoc.Lua (Global (..), runLua, runFilterFile, setGlobals)
|
||||
import Text.Pandoc.Lua (Global (..), applyFilter)
|
||||
|
||||
-- | Run the Lua filter in @filterPath@ for a transformation to the
|
||||
-- target format (first element in args). Pandoc uses Lua init files to
|
||||
@@ -34,18 +31,8 @@ apply fenv args fp doc = do
|
||||
let format = case args of
|
||||
(x:_) -> x
|
||||
_ -> error "Format not supplied for Lua filter"
|
||||
runLua >=> forceResult fp $ do
|
||||
setGlobals [ FORMAT $ T.pack format
|
||||
, PANDOC_READER_OPTIONS (envReaderOptions fenv)
|
||||
, PANDOC_WRITER_OPTIONS (envWriterOptions fenv)
|
||||
, PANDOC_SCRIPT_FILE fp
|
||||
]
|
||||
runFilterFile fp doc
|
||||
|
||||
forceResult :: (PandocMonad m, MonadIO m)
|
||||
=> FilePath -> Either PandocError Pandoc -> m Pandoc
|
||||
forceResult fp eitherResult = case eitherResult of
|
||||
Right x -> return x
|
||||
Left err -> throw . PandocFilterError (T.pack fp) $ case err of
|
||||
PandocLuaError msg -> msg
|
||||
_ -> T.pack $ show err
|
||||
applyFilter [ FORMAT $ T.pack format
|
||||
, PANDOC_READER_OPTIONS (envReaderOptions fenv)
|
||||
, PANDOC_WRITER_OPTIONS (envWriterOptions fenv)
|
||||
, PANDOC_SCRIPT_FILE fp
|
||||
] fp doc
|
||||
|
||||
+10
-6
@@ -9,15 +9,19 @@
|
||||
Running pandoc Lua filters.
|
||||
-}
|
||||
module Text.Pandoc.Lua
|
||||
( runLua
|
||||
-- * Lua globals
|
||||
, Global (..)
|
||||
( -- * High-level functions
|
||||
applyFilter
|
||||
, readCustom
|
||||
, writeCustom
|
||||
-- * Low-level functions
|
||||
, Global(..)
|
||||
, setGlobals
|
||||
-- * Filters
|
||||
, runFilterFile
|
||||
, runLua
|
||||
) where
|
||||
|
||||
import Text.Pandoc.Lua.Filter (runFilterFile)
|
||||
import Text.Pandoc.Lua.Filter (applyFilter)
|
||||
import Text.Pandoc.Lua.Global (Global (..), setGlobals)
|
||||
import Text.Pandoc.Lua.Init (runLua)
|
||||
import Text.Pandoc.Lua.Reader (readCustom)
|
||||
import Text.Pandoc.Lua.Writer (writeCustom)
|
||||
import Text.Pandoc.Lua.Orphans ()
|
||||
|
||||
@@ -13,15 +13,22 @@ Stability : alpha
|
||||
Types and functions for running Lua filters.
|
||||
-}
|
||||
module Text.Pandoc.Lua.Filter
|
||||
( runFilterFile
|
||||
( applyFilter
|
||||
) where
|
||||
import Control.Monad ((>=>), (<$!>))
|
||||
import HsLua as Lua
|
||||
import Text.Pandoc.Definition
|
||||
import Text.Pandoc.Error (PandocError)
|
||||
import Text.Pandoc.Lua.ErrorConversion ()
|
||||
import Text.Pandoc.Lua.Marshal.AST
|
||||
import Text.Pandoc.Lua.Marshal.Filter
|
||||
import Text.Pandoc.Lua.Global (Global (..), setGlobals)
|
||||
import Text.Pandoc.Lua.Init (runLua)
|
||||
import Control.Exception (throw)
|
||||
import qualified Data.Text as T
|
||||
import Text.Pandoc.Class (PandocMonad)
|
||||
import Control.Monad.Trans (MonadIO)
|
||||
import Text.Pandoc.Error (PandocError (PandocFilterError, PandocLuaError))
|
||||
|
||||
|
||||
|
||||
-- | Transform document using the filter defined in the given file.
|
||||
@@ -44,3 +51,24 @@ runFilterFile filterPath doc = do
|
||||
|
||||
runAll :: [Filter] -> Pandoc -> LuaE PandocError Pandoc
|
||||
runAll = foldr ((>=>) . applyFully) return
|
||||
|
||||
-- | Run the Lua filter in @filterPath@ for a transformation to the
|
||||
-- target format (first element in args). Pandoc uses Lua init files to
|
||||
-- setup the Lua interpreter.
|
||||
applyFilter :: (PandocMonad m, MonadIO m)
|
||||
=> [Global]
|
||||
-> FilePath
|
||||
-> Pandoc
|
||||
-> m Pandoc
|
||||
applyFilter globals fp doc = do
|
||||
runLua >=> forceResult fp $ do
|
||||
setGlobals globals
|
||||
runFilterFile fp doc
|
||||
|
||||
forceResult :: (PandocMonad m, MonadIO m)
|
||||
=> FilePath -> Either PandocError Pandoc -> m Pandoc
|
||||
forceResult fp eitherResult = case eitherResult of
|
||||
Right x -> return x
|
||||
Left err -> throw . PandocFilterError (T.pack fp) $ case err of
|
||||
PandocLuaError msg -> msg
|
||||
_ -> T.pack $ show err
|
||||
|
||||
Reference in New Issue
Block a user