git.iko.soy/typeable/pandoc
on master help sign in
Lua filters: set global FORMAT instead of args.

This changes the type of runLuaFilter.

commit9a47c7863b7c9d23928e51fd23b8ebc7ac684d16 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 26 Sep 2017, 20:20 (9 years ago) parent3fe4aad
3 files changed+12−8
doc/lua-filters.md+4 src/Text/Pandoc/App.hs+4−4 src/Text/Pandoc/Lua.hs+4−4
doc/lua-filters.md+4
@@ -132,6 +132,10 @@ Elements without matching functions are left untouched.
132132See [module documentation](pandoc-module.html) for a list of pandoc
133133elements.
134134
135+The global `FORMAT` is set to the format of the pandoc writer
136+being used (`html5`, `latex`, etc.), so the behavior of a filter
137+can be made conditional on the eventual output format.
138+
135139# Pandoc Module
136140
137141The `pandoc` lua module is loaded into the filter's lua
src/Text/Pandoc/App.hs+4−4
@@ -514,7 +514,7 @@ convertWithOpts opts = do
514514 >=> maybe return extractMedia (optExtractMedia opts)
515515 >=> return . flip (foldr addMetadata) metadata
516516 >=> applyTransforms transforms
517− >=> applyLuaFilters datadir (optLuaFilters opts) [format]
517+ >=> applyLuaFilters datadir (optLuaFilters opts) format
518518 >=> applyFilters readerOpts datadir filters' [format]
519519 )
520520 media <- getMediaBag
@@ -850,12 +850,12 @@ expandFilterPath mbDatadir fp = liftIO $ do
850850 _ -> return fp
851851
852852applyLuaFilters :: MonadIO m
853− => Maybe FilePath -> [FilePath] -> [String] -> Pandoc
853+ => Maybe FilePath -> [FilePath] -> String -> Pandoc
854854 -> m Pandoc
855−applyLuaFilters mbDatadir filters args d = do
855+applyLuaFilters mbDatadir filters format d = do
856856 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
857857 let go f d' = liftIO $ do
858− res <- E.try (runLuaFilter mbDatadir f args d')
858+ res <- E.try (runLuaFilter mbDatadir f format d')
859859 case res of
860860 Right x -> return x
861861 Left (LuaException s) -> E.throw (PandocFilterError f s)
src/Text/Pandoc/Lua.hs+4−4
@@ -51,14 +51,14 @@ import qualified Data.Map as Map
5151import qualified Foreign.Lua as Lua
5252
5353runLuaFilter :: (MonadIO m)
54− => Maybe FilePath -> FilePath -> [String] -> Pandoc -> m Pandoc
55−runLuaFilter datadir filterPath args pd = liftIO . Lua.runLua $ do
54+ => Maybe FilePath -> FilePath -> String -> Pandoc -> m Pandoc
55+runLuaFilter datadir filterPath format pd = liftIO . Lua.runLua $ do
5656 Lua.openlibs
5757 -- store module in global "pandoc"
5858 pushPandocModule datadir
5959 Lua.setglobal "pandoc"
60− push args
61− Lua.setglobal "arg"
60+ push format
61+ Lua.setglobal "FORMAT"
6262 top <- Lua.gettop
6363 stat <- Lua.dofile filterPath
6464 if stat /= OK