From 672a4bdd1d4a587feaa38613fce64335adaad76d Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Wed, 15 Jan 2020 23:26:00 +0100 Subject: Lua filters: allow filtering of element lists (#6040) Lists of Inline and Block elements can now be filtered via `Inlines` and `Blocks` functions, respectively. This is helpful if a filter conversion depends on the order of elements rather than a single element. For example, the following filter can be used to remove all spaces before a citation: function isSpaceBeforeCite (spc, cite) return spc and spc.t == 'Space' and cite and cite.t == 'Cite' end function Inlines (inlines) for i = #inlines-1,1,-1 do if isSpaceBeforeCite(inlines[i], inlines[i+1]) then inlines:remove(i) end end return inlines end Closes: #6038 --- src/Text/Pandoc/Lua/Marshaling/List.hs | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Text/Pandoc/Lua/Marshaling/List.hs (limited to 'src/Text/Pandoc/Lua/Marshaling/List.hs') diff --git a/src/Text/Pandoc/Lua/Marshaling/List.hs b/src/Text/Pandoc/Lua/Marshaling/List.hs new file mode 100644 index 000000000..e4fbfc200 --- /dev/null +++ b/src/Text/Pandoc/Lua/Marshaling/List.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE UndecidableInstances #-} +{- | +Module : Text.Pandoc.Lua.Marshaling.List +Copyright : © 2012-2020 John MacFarlane + © 2017-2020 Albert Krewinkel +License : GNU GPL, version 2 or above +Maintainer : Albert Krewinkel +Stability : alpha + +Marshaling/unmarshaling instances for @pandoc.List@s. +-} +module Text.Pandoc.Lua.Marshaling.List + ( List (..) + ) where + +import Prelude +import Data.Data (Data) +import Foreign.Lua (Peekable, Pushable) +import Text.Pandoc.Walk (Walkable (..)) +import Text.Pandoc.Lua.Util (defineHowTo, pushViaConstructor) + +import qualified Foreign.Lua as Lua + +-- | List wrapper which is marshalled as @pandoc.List@. +newtype List a = List { fromList :: [a] } + deriving (Data, Eq, Show) + +instance Pushable a => Pushable (List a) where + push (List xs) = + pushViaConstructor "List" xs + +instance Peekable a => Peekable (List a) where + peek idx = defineHowTo "get List" $ do + xs <- Lua.peek idx + return $ List xs + +-- List is just a wrapper, so we can reuse the walk instance for +-- unwrapped Hasekll lists. +instance Walkable [a] b => Walkable (List a) b where + walkM f = walkM (fmap fromList . f . List) + query f = query (f . List) -- cgit v1.2.3