git.iko.soy/typeable/pandoc
on master help sign in
Lua filters: set global FORMAT instead of args.
9a47c78John MacFarlane
doc/lua-filters.md+4
@@ -132,6 +132,10 @@ Elements without matching functions are left untouched.
132See [module documentation](pandoc-module.html) for a list of pandoc
132See [module documentation](pandoc-module.html) for a list of pandoc
133elements.
133elements.
134
134
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+
135# Pandoc Module
139# Pandoc Module
136
140
137The `pandoc` lua module is loaded into the filter's lua
141The `pandoc` lua module is loaded into the filter's lua
src/Text/Pandoc/App.hs+4−4
@@ -514,7 +514,7 @@ convertWithOpts opts = do
514 >=> maybe return extractMedia (optExtractMedia opts)
514 >=> maybe return extractMedia (optExtractMedia opts)
515 >=> return . flip (foldr addMetadata) metadata
515 >=> return . flip (foldr addMetadata) metadata
516 >=> applyTransforms transforms
516 >=> applyTransforms transforms
517− >=> applyLuaFilters datadir (optLuaFilters opts) [format]
517+ >=> applyLuaFilters datadir (optLuaFilters opts) format
518 >=> applyFilters readerOpts datadir filters' [format]
518 >=> applyFilters readerOpts datadir filters' [format]
519 )
519 )
520 media <- getMediaBag
520 media <- getMediaBag
@@ -850,12 +850,12 @@ expandFilterPath mbDatadir fp = liftIO $ do
850 _ -> return fp
850 _ -> return fp
851
851
852applyLuaFilters :: MonadIO m
852applyLuaFilters :: MonadIO m
853− => Maybe FilePath -> [FilePath] -> [String] -> Pandoc
853+ => Maybe FilePath -> [FilePath] -> String -> Pandoc
854 -> m Pandoc
854 -> m Pandoc
855−applyLuaFilters mbDatadir filters args d = do
855+applyLuaFilters mbDatadir filters format d = do
856 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
856 expandedFilters <- mapM (expandFilterPath mbDatadir) filters
857 let go f d' = liftIO $ do
857 let go f d' = liftIO $ do
858− res <- E.try (runLuaFilter mbDatadir f args d')
858+ res <- E.try (runLuaFilter mbDatadir f format d')
859 case res of
859 case res of
860 Right x -> return x
860 Right x -> return x
861 Left (LuaException s) -> E.throw (PandocFilterError f s)
861 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
51import qualified Foreign.Lua as Lua
51import qualified Foreign.Lua as Lua
52
52
53runLuaFilter :: (MonadIO m)
53runLuaFilter :: (MonadIO m)
54− => Maybe FilePath -> FilePath -> [String] -> Pandoc -> m Pandoc
54+ => Maybe FilePath -> FilePath -> String -> Pandoc -> m Pandoc
55−runLuaFilter datadir filterPath args pd = liftIO . Lua.runLua $ do
55+runLuaFilter datadir filterPath format pd = liftIO . Lua.runLua $ do
56 Lua.openlibs
56 Lua.openlibs
57 -- store module in global "pandoc"
57 -- store module in global "pandoc"
58 pushPandocModule datadir
58 pushPandocModule datadir
59 Lua.setglobal "pandoc"
59 Lua.setglobal "pandoc"
60− push args
60+ push format
61− Lua.setglobal "arg"
61+ Lua.setglobal "FORMAT"
62 top <- Lua.gettop
62 top <- Lua.gettop
63 stat <- Lua.dofile filterPath
63 stat <- Lua.dofile filterPath
64 if stat /= OK
64 if stat /= OK