git.iko.soy/typeable/pandoc
on master help sign in
Lua filters: test AST object equality via Haskell

Equality of Lua objects representing pandoc AST elements is tested by unmarshalling the objects and comparing the result in Haskell. A new function `equals` which performs this test has been added to the `pandoc.utils` module. Closes: #5092

commitc0d8b0abcb99adf5f7d2ddac3ad343d48da94910 authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 19 Nov 2018, 21:36 (8 years ago) parente80bcb9
3 files changed+66−5
data/pandoc.lua+4−1 doc/lua-filters.md+47−1 src/Text/Pandoc/Lua/Module/Utils.hs+15−3
data/pandoc.lua+4−1
@@ -25,6 +25,7 @@ THIS SOFTWARE.
2525local M = {}
2626
2727local List = require 'pandoc.List'
28+local utils = require 'pandoc.utils'
2829
2930------------------------------------------------------------------------
3031-- Accessor objects
@@ -119,6 +120,7 @@ local function create_accessor_behavior (tag, accessors)
119120 'function (x, v) x.c%s = v end',
120121 accessors
121122 )
123+ behavior.__eq = utils.equals
122124 behavior.__index = function(t, k)
123125 if getmetatable(t).getters[k] then
124126 return getmetatable(t).getters[k](t)
@@ -873,6 +875,7 @@ function M.Attr:new (identifier, classes, attributes)
873875 return {identifier, classes, attributes}
874876end
875877M.Attr.behavior._field_names = {identifier = 1, classes = 2, attributes = 3}
878+M.Attr.behavior.__eq = utils.equals
876879M.Attr.behavior.__index = function(t, k)
877880 return rawget(t, getmetatable(t)._field_names[k]) or
878881 getmetatable(t)[k]
@@ -931,6 +934,7 @@ function M.ListAttributes:new (start, style, delimiter)
931934 return {start, style, delimiter}
932935end
933936M.ListAttributes.behavior._field_names = {start = 1, style = 2, delimiter = 3}
937+M.ListAttributes.behavior.__eq = utils.equals
934938M.ListAttributes.behavior.__index = function (t, k)
935939 return rawget(t, getmetatable(t)._field_names[k]) or
936940 getmetatable(t)[k]
@@ -1033,7 +1037,6 @@ M.UpperAlpha = "UpperAlpha"
10331037
10341038------------------------------------------------------------------------
10351039-- Functions which have moved to different modules
1036−local utils = require 'pandoc.utils'
10371040M.sha1 = utils.sha1
10381041
10391042return M
doc/lua-filters.md+47−1
@@ -171,7 +171,7 @@ variables.
171171: The name used to involve the filter. This value can be used
172172 to find files relative to the script file. This variable is
173173 also set in custom writers.
174−
174+
175175`PANDOC_STATE`
176176: The state shared by all readers and writers. It is used by
177177 pandoc to collect and pass information. The value of this
@@ -641,6 +641,9 @@ to create these objects.
641641
642642Pandoc document
643643
644+Object equality is determined via
645+[`pandoc.utils.equals`](#utils-equals).
646+
644647`blocks`
645648: document content ([List] of [Block]s)
646649
@@ -653,10 +656,16 @@ Pandoc document
653656Meta information on a document; string-indexed collection of
654657[MetaValue]s.
655658
659+Object equality is determined via
660+[`pandoc.utils.equals`](#utils-equals).
661+
656662## MetaValue {#type-ref-MetaValue}
657663
658664Document meta information items.
659665
666+Object equality is determined via
667+[`pandoc.utils.equals`](#utils-equals).
668+
660669### MetaBlocks {#type-ref-MetaBlocks}
661670
662671A list of blocks usable as meta value ([List] of [Block]s)
@@ -707,6 +716,9 @@ Plain Lua string value (string)
707716
708717## Block {#type-ref-Block}
709718
719+Object equality is determined via
720+[`pandoc.utils.equals`](#utils-equals).
721+
710722### BlockQuote {#type-ref-BlockQuote}
711723
712724A block quote element
@@ -926,6 +938,9 @@ centered).
926938
927939## Inline {#type-ref-Inline}
928940
941+Object equality is determined via
942+[`pandoc.utils.equals`](#utils-equals).
943+
929944### Cite {#type-ref-Cite}
930945Citation
931946
@@ -1166,6 +1181,9 @@ Superscripted text
11661181
11671182A set of element attributes
11681183
1184+Object equality is determined via
1185+[`pandoc.utils.equals`](#utils-equals).
1186+
11691187`identifier`
11701188: element identifier (string)
11711189
@@ -1184,6 +1202,9 @@ indices to the list table.
11841202
11851203Single citation entry
11861204
1205+Object equality is determined via
1206+[`pandoc.utils.equals`](#utils-equals).
1207+
11871208`id`
11881209: citation identifier, e.g., a bibtex key (string)
11891210
@@ -1206,6 +1227,9 @@ Single citation entry
12061227### ListAttributes {#type-ref-ListAttributes}
12071228List attributes
12081229
1230+Object equality is determined via
1231+[`pandoc.utils.equals`](#utils-equals).
1232+
12091233`start`
12101234: number of the first list item (integer)
12111235
@@ -2202,6 +2226,28 @@ functions.
22022226 -- pandoc.Emph{ pandoc.Str 'Paragraph2' }
22032227 -- }
22042228
2229+[`equals (element1, element2)`]{#utils-equals}
2230+
2231+: Test equality of AST elements. Elements in Lua are considered
2232+ equal if and only if the objects obtained by unmarshaling are
2233+ equal.
2234+
2235+ Parameters:
2236+
2237+ `element1`, `element2`:
2238+ : Objects to be compared. Acceptable input types are
2239+ [Pandoc](#type-ref-pandoc), [Meta](#type-ref-meta),
2240+ [MetaValue](#type-ref-MetaValue),
2241+ [Block](#type-ref-Block), [Inline](#type-ref-Inline),
2242+ [Attr](#type-ref-Attr),
2243+ [ListAttributes](#type-ref-ListAttributes), and
2244+ [Citation](#type-ref-Citation).
2245+
2246+ Returns:
2247+
2248+ - Whether the two objects represent the same element
2249+ (boolean)
2250+
22052251[`hierarchicalize (blocks)`]{#utils-hierarchicalize}
22062252
22072253: Convert list of blocks into an hierarchical list. An
src/Text/Pandoc/Lua/Module/Utils.hs+15−3
@@ -1,3 +1,4 @@
1+{-# LANGUAGE NoImplicitPrelude #-}
12{-
23Copyright © 2017-2018 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
34
@@ -15,7 +16,6 @@ You should have received a copy of the GNU General Public License
1516along with this program; if not, write to the Free Software
1617Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1718-}
18−{-# LANGUAGE NoImplicitPrelude #-}
1919{- |
2020 Module : Text.Pandoc.Lua.Module.Utils
2121 Copyright : Copyright © 2017-2018 Albert Krewinkel
@@ -36,7 +36,8 @@ import Data.Char (toLower)
3636import Data.Default (def)
3737import Foreign.Lua (Peekable, Lua, NumResults)
3838import Text.Pandoc.Class (runIO, setUserDataDir)
39−import Text.Pandoc.Definition (Pandoc, Meta, MetaValue (..), Block, Inline)
39+import Text.Pandoc.Definition ( Pandoc, Meta, MetaValue (..), Block, Inline
40+ , Citation, Attr, ListAttributes)
4041import Text.Pandoc.Lua.StackInstances ()
4142import Text.Pandoc.Lua.Util (addFunction)
4243
@@ -52,6 +53,7 @@ pushModule :: Maybe FilePath -> Lua NumResults
5253pushModule mbDatadir = do
5354 Lua.newtable
5455 addFunction "blocks_to_inlines" blocksToInlines
56+ addFunction "equals" equals
5557 addFunction "hierarchicalize" hierarchicalize
5658 addFunction "normalize_date" normalizeDate
5759 addFunction "run_json_filter" (runJSONFilter mbDatadir)
@@ -112,7 +114,9 @@ stringify el = return $ case el of
112114 InlineElement i -> Shared.stringify i
113115 BlockElement b -> Shared.stringify b
114116 MetaElement m -> Shared.stringify m
117+ CitationElement c -> Shared.stringify c
115118 MetaValueElement m -> stringifyMetaValue m
119+ _ -> ""
116120
117121stringifyMetaValue :: MetaValue -> String
118122stringifyMetaValue mv = case mv of
@@ -120,19 +124,27 @@ stringifyMetaValue mv = case mv of
120124 MetaString s -> s
121125 _ -> Shared.stringify mv
122126
127+equals :: AstElement -> AstElement -> Lua Bool
128+equals e1 e2 = return (e1 == e2)
129+
123130data AstElement
124131 = PandocElement Pandoc
125132 | MetaElement Meta
126133 | BlockElement Block
127134 | InlineElement Inline
128135 | MetaValueElement MetaValue
129− deriving (Show)
136+ | AttrElement Attr
137+ | ListAttributesElement ListAttributes
138+ | CitationElement Citation
139+ deriving (Eq, Show)
130140
131141instance Peekable AstElement where
132142 peek idx = do
133143 res <- Lua.try $ (PandocElement <$> Lua.peek idx)
134144 <|> (InlineElement <$> Lua.peek idx)
135145 <|> (BlockElement <$> Lua.peek idx)
146+ <|> (AttrElement <$> Lua.peek idx)
147+ <|> (ListAttributesElement <$> Lua.peek idx)
136148 <|> (MetaElement <$> Lua.peek idx)
137149 <|> (MetaValueElement <$> Lua.peek idx)
138150 case res of