git.iko.soy/typeable/pandoc
on master help sign in
Lua filters: make PANDOC_READER_OPTIONS available

The options which were used to read the document are made available to Lua filters via the `PANDOC_READER_OPTIONS` global.

commit5b852f8d2ad6e2d9713e894bbb80489c1d383847 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 12 Jan 2018, 08:56 (9 years ago) parent624abee
4 files changed+63−11
src/Text/Pandoc/App.hs+2−2 src/Text/Pandoc/Lua.hs+11−6 src/Text/Pandoc/Lua/StackInstances.hs+48−2 test/Tests/Lua.hs+2−1
src/Text/Pandoc/App.hs+2−2
@@ -855,12 +855,12 @@ applyFilter :: ReaderOptions
855855 -> Filter
856856 -> Pandoc
857857 -> PandocIO Pandoc
858−applyFilter _ropts args (LuaFilter f) d = do
858+applyFilter ropts args (LuaFilter f) d = do
859859 f' <- expandFilterPath f
860860 let format = case args of
861861 (x:_) -> x
862862 _ -> error "Format not supplied for lua filter"
863− res <- runLuaFilter f' format d
863+ res <- runLuaFilter ropts f' format d
864864 case res of
865865 Right x -> return x
866866 Left (LuaException s) -> E.throw (PandocFilterError f s)
src/Text/Pandoc/Lua.hs+11−6
@@ -39,21 +39,22 @@ import Text.Pandoc.Definition (Pandoc)
3939import Text.Pandoc.Lua.Filter (LuaFilter, walkMWithLuaFilter)
4040import Text.Pandoc.Lua.Init (runPandocLua)
4141import Text.Pandoc.Lua.Util (popValue)
42+import Text.Pandoc.Options (ReaderOptions)
4243import qualified Foreign.Lua as Lua
4344
4445-- | Run the Lua filter in @filterPath@ for a transformation to target
4546-- format @format@. Pandoc uses Lua init files to setup the Lua
4647-- interpreter.
47−runLuaFilter :: FilePath -> String
48+runLuaFilter :: ReaderOptions -> FilePath -> String
4849 -> Pandoc -> PandocIO (Either LuaException Pandoc)
49−runLuaFilter filterPath format doc =
50− runPandocLua (runLuaFilter' filterPath format doc)
50+runLuaFilter ropts filterPath format doc =
51+ runPandocLua (runLuaFilter' ropts filterPath format doc)
5152
52−runLuaFilter' :: FilePath -> String
53+runLuaFilter' :: ReaderOptions -> FilePath -> String
5354 -> Pandoc -> Lua Pandoc
54−runLuaFilter' filterPath format pd = do
55− -- store module in global "pandoc"
55+runLuaFilter' ropts filterPath format pd = do
5656 registerFormat
57+ registerReaderOptions
5758 top <- Lua.gettop
5859 stat <- Lua.dofile filterPath
5960 if stat /= OK
@@ -73,5 +74,9 @@ runLuaFilter' filterPath format pd = do
7374 push format
7475 Lua.setglobal "FORMAT"
7576
77+ registerReaderOptions = do
78+ push ropts
79+ Lua.setglobal "PANDOC_READER_OPTIONS"
80+
7681runAll :: [LuaFilter] -> Pandoc -> Lua Pandoc
7782runAll = foldr ((>=>) . walkMWithLuaFilter) return
src/Text/Pandoc/Lua/StackInstances.hs+48−2
@@ -16,8 +16,9 @@ You should have received a copy of the GNU General Public License
1616along with this program; if not, write to the Free Software
1717Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818-}
19−{-# LANGUAGE FlexibleInstances #-}
20−{-# LANGUAGE LambdaCase #-}
19+{-# LANGUAGE FlexibleInstances #-}
20+{-# LANGUAGE LambdaCase #-}
21+{-# LANGUAGE ScopedTypeVariables #-}
2122{-# OPTIONS_GHC -fno-warn-orphans #-}
2223{- |
2324 Module : Text.Pandoc.Lua.StackInstances
@@ -34,13 +35,18 @@ module Text.Pandoc.Lua.StackInstances () where
3435
3536import Control.Applicative ((<|>))
3637import Control.Monad (when)
38+import Data.Data (showConstr, toConstr)
39+import Data.Foldable (forM_)
3740import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
3841 ToLuaStack (push), Type (..), throwLuaError, tryLua)
3942import Text.Pandoc.Definition
43+import Text.Pandoc.Extensions (Extensions)
4044import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor)
45+import Text.Pandoc.Options (ReaderOptions (..), TrackChanges)
4146import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
4247
4348import qualified Foreign.Lua as Lua
49+import qualified Data.Set as Set
4450import qualified Text.Pandoc.Lua.Util as LuaUtil
4551
4652instance ToLuaStack Pandoc where
@@ -332,3 +338,43 @@ instance ToLuaStack Element where
332338 Lua.push "__index"
333339 Lua.pushvalue (-2)
334340 Lua.rawset (-3)
341+
342+
343+--
344+-- Reader Options
345+--
346+instance ToLuaStack Extensions where
347+ push exts = push (show exts)
348+
349+instance ToLuaStack TrackChanges where
350+ push = push . showConstr . toConstr
351+
352+instance ToLuaStack a => ToLuaStack (Set.Set a) where
353+ push set = do
354+ Lua.newtable
355+ forM_ set (`LuaUtil.addValue` True)
356+
357+instance ToLuaStack ReaderOptions where
358+ push ro = do
359+ let ReaderOptions
360+ (extensions :: Extensions)
361+ (standalone :: Bool)
362+ (columns :: Int)
363+ (tabStop :: Int)
364+ (indentedCodeClasses :: [String])
365+ (abbreviations :: Set.Set String)
366+ (defaultImageExtension :: String)
367+ (trackChanges :: TrackChanges)
368+ (stripComments :: Bool)
369+ = ro
370+ Lua.newtable
371+ LuaUtil.addValue "extensions" extensions
372+ LuaUtil.addValue "standalone" standalone
373+ LuaUtil.addValue "columns" columns
374+ LuaUtil.addValue "tabStop" tabStop
375+ LuaUtil.addValue "indentedCodeClasses" indentedCodeClasses
376+ LuaUtil.addValue "abbreviations" abbreviations
377+ LuaUtil.addValue "defaultImageExtension" defaultImageExtension
378+ LuaUtil.addValue "trackChanges" trackChanges
379+ LuaUtil.addValue "stripComments" stripComments
380+
test/Tests/Lua.hs+2−1
@@ -14,6 +14,7 @@ import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
1414import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
1515import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc, pandocTypesVersion)
1616import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
17+import Text.Pandoc.Options (def)
1718import Text.Pandoc.Shared (pandocVersion)
1819
1920import qualified Foreign.Lua as Lua
@@ -128,7 +129,7 @@ assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
128129assertFilterConversion msg filterPath docIn docExpected = do
129130 docEither <- runIOorExplode $ do
130131 setUserDataDir (Just "../data")
131− runLuaFilter ("lua" </> filterPath) [] docIn
132+ runLuaFilter def ("lua" </> filterPath) [] docIn
132133 case docEither of
133134 Left _ -> fail "lua filter failed"
134135 Right docRes -> assertEqual msg docExpected docRes