git.iko.soy/typeable/pandoc
on master help sign in
Lua filters: test AST object equality via Haskell
c0d8b0aAlbert Krewinkel
data/pandoc.lua+4−1
@@ -25,6 +25,7 @@ THIS SOFTWARE.
25local M = {}
25local M = {}
26
26
27local List = require 'pandoc.List'
27local List = require 'pandoc.List'
28+local utils = require 'pandoc.utils'
28
29
29------------------------------------------------------------------------
30------------------------------------------------------------------------
30-- Accessor objects
31-- Accessor objects
@@ -119,6 +120,7 @@ local function create_accessor_behavior (tag, accessors)
119 'function (x, v) x.c%s = v end',
120 'function (x, v) x.c%s = v end',
120 accessors
121 accessors
121 )
122 )
123+ behavior.__eq = utils.equals
122 behavior.__index = function(t, k)
124 behavior.__index = function(t, k)
123 if getmetatable(t).getters[k] then
125 if getmetatable(t).getters[k] then
124 return getmetatable(t).getters[k](t)
126 return getmetatable(t).getters[k](t)
@@ -873,6 +875,7 @@ function M.Attr:new (identifier, classes, attributes)
873 return {identifier, classes, attributes}
875 return {identifier, classes, attributes}
874end
876end
875M.Attr.behavior._field_names = {identifier = 1, classes = 2, attributes = 3}
877M.Attr.behavior._field_names = {identifier = 1, classes = 2, attributes = 3}
878+M.Attr.behavior.__eq = utils.equals
876M.Attr.behavior.__index = function(t, k)
879M.Attr.behavior.__index = function(t, k)
877 return rawget(t, getmetatable(t)._field_names[k]) or
880 return rawget(t, getmetatable(t)._field_names[k]) or
878 getmetatable(t)[k]
881 getmetatable(t)[k]
@@ -931,6 +934,7 @@ function M.ListAttributes:new (start, style, delimiter)
931 return {start, style, delimiter}
934 return {start, style, delimiter}
932end
935end
933M.ListAttributes.behavior._field_names = {start = 1, style = 2, delimiter = 3}
936M.ListAttributes.behavior._field_names = {start = 1, style = 2, delimiter = 3}
937+M.ListAttributes.behavior.__eq = utils.equals
934M.ListAttributes.behavior.__index = function (t, k)
938M.ListAttributes.behavior.__index = function (t, k)
935 return rawget(t, getmetatable(t)._field_names[k]) or
939 return rawget(t, getmetatable(t)._field_names[k]) or
936 getmetatable(t)[k]
940 getmetatable(t)[k]
@@ -1033,7 +1037,6 @@ M.UpperAlpha = "UpperAlpha"
1033
1037
1034------------------------------------------------------------------------
1038------------------------------------------------------------------------
1035-- Functions which have moved to different modules
1039-- Functions which have moved to different modules
1036−local utils = require 'pandoc.utils'
1037M.sha1 = utils.sha1
1040M.sha1 = utils.sha1
1038
1041
1039return M
1042return M
doc/lua-filters.md+47−1
@@ -171,7 +171,7 @@ variables.
171: The name used to involve the filter. This value can be used
171: The name used to involve the filter. This value can be used
172 to find files relative to the script file. This variable is
172 to find files relative to the script file. This variable is
173 also set in custom writers.
173 also set in custom writers.
174−
174+
175`PANDOC_STATE`
175`PANDOC_STATE`
176: The state shared by all readers and writers. It is used by
176: The state shared by all readers and writers. It is used by
177 pandoc to collect and pass information. The value of this
177 pandoc to collect and pass information. The value of this
@@ -641,6 +641,9 @@ to create these objects.
641
641
642Pandoc document
642Pandoc document
643
643
644+Object equality is determined via
645+[`pandoc.utils.equals`](#utils-equals).
646+
644`blocks`
647`blocks`
645: document content ([List] of [Block]s)
648: document content ([List] of [Block]s)
646
649
@@ -653,10 +656,16 @@ Pandoc document
653Meta information on a document; string-indexed collection of
656Meta information on a document; string-indexed collection of
654[MetaValue]s.
657[MetaValue]s.
655
658
659+Object equality is determined via
660+[`pandoc.utils.equals`](#utils-equals).
661+
656## MetaValue {#type-ref-MetaValue}
662## MetaValue {#type-ref-MetaValue}
657
663
658Document meta information items.
664Document meta information items.
659
665
666+Object equality is determined via
667+[`pandoc.utils.equals`](#utils-equals).
668+
660### MetaBlocks {#type-ref-MetaBlocks}
669### MetaBlocks {#type-ref-MetaBlocks}
661
670
662A list of blocks usable as meta value ([List] of [Block]s)
671A list of blocks usable as meta value ([List] of [Block]s)
@@ -707,6 +716,9 @@ Plain Lua string value (string)
707
716
708## Block {#type-ref-Block}
717## Block {#type-ref-Block}
709
718
719+Object equality is determined via
720+[`pandoc.utils.equals`](#utils-equals).
721+
710### BlockQuote {#type-ref-BlockQuote}
722### BlockQuote {#type-ref-BlockQuote}
711
723
712A block quote element
724A block quote element
@@ -926,6 +938,9 @@ centered).
926
938
927## Inline {#type-ref-Inline}
939## Inline {#type-ref-Inline}
928
940
941+Object equality is determined via
942+[`pandoc.utils.equals`](#utils-equals).
943+
929### Cite {#type-ref-Cite}
944### Cite {#type-ref-Cite}
930Citation
945Citation
931
946
@@ -1166,6 +1181,9 @@ Superscripted text
1166
1181
1167A set of element attributes
1182A set of element attributes
1168
1183
1184+Object equality is determined via
1185+[`pandoc.utils.equals`](#utils-equals).
1186+
1169`identifier`
1187`identifier`
1170: element identifier (string)
1188: element identifier (string)
1171
1189
@@ -1184,6 +1202,9 @@ indices to the list table.
1184
1202
1185Single citation entry
1203Single citation entry
1186
1204
1205+Object equality is determined via
1206+[`pandoc.utils.equals`](#utils-equals).
1207+
1187`id`
1208`id`
1188: citation identifier, e.g., a bibtex key (string)
1209: citation identifier, e.g., a bibtex key (string)
1189
1210
@@ -1206,6 +1227,9 @@ Single citation entry
1206### ListAttributes {#type-ref-ListAttributes}
1227### ListAttributes {#type-ref-ListAttributes}
1207List attributes
1228List attributes
1208
1229
1230+Object equality is determined via
1231+[`pandoc.utils.equals`](#utils-equals).
1232+
1209`start`
1233`start`
1210: number of the first list item (integer)
1234: number of the first list item (integer)
1211
1235
@@ -2202,6 +2226,28 @@ functions.
2202 -- pandoc.Emph{ pandoc.Str 'Paragraph2' }
2226 -- pandoc.Emph{ pandoc.Str 'Paragraph2' }
2203 -- }
2227 -- }
2204
2228
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+
2205[`hierarchicalize (blocks)`]{#utils-hierarchicalize}
2251[`hierarchicalize (blocks)`]{#utils-hierarchicalize}
2206
2252
2207: Convert list of blocks into an hierarchical list. An
2253: 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 #-}
1{-
2{-
2Copyright © 2017-2018 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3Copyright © 2017-2018 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
3
4
@@ -15,7 +16,6 @@ You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17-}
18-}
18−{-# LANGUAGE NoImplicitPrelude #-}
19{- |
19{- |
20 Module : Text.Pandoc.Lua.Module.Utils
20 Module : Text.Pandoc.Lua.Module.Utils
21 Copyright : Copyright © 2017-2018 Albert Krewinkel
21 Copyright : Copyright © 2017-2018 Albert Krewinkel
@@ -36,7 +36,8 @@ import Data.Char (toLower)
36import Data.Default (def)
36import Data.Default (def)
37import Foreign.Lua (Peekable, Lua, NumResults)
37import Foreign.Lua (Peekable, Lua, NumResults)
38import Text.Pandoc.Class (runIO, setUserDataDir)
38import 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)
40import Text.Pandoc.Lua.StackInstances ()
41import Text.Pandoc.Lua.StackInstances ()
41import Text.Pandoc.Lua.Util (addFunction)
42import Text.Pandoc.Lua.Util (addFunction)
42
43
@@ -52,6 +53,7 @@ pushModule :: Maybe FilePath -> Lua NumResults
52pushModule mbDatadir = do
53pushModule mbDatadir = do
53 Lua.newtable
54 Lua.newtable
54 addFunction "blocks_to_inlines" blocksToInlines
55 addFunction "blocks_to_inlines" blocksToInlines
56+ addFunction "equals" equals
55 addFunction "hierarchicalize" hierarchicalize
57 addFunction "hierarchicalize" hierarchicalize
56 addFunction "normalize_date" normalizeDate
58 addFunction "normalize_date" normalizeDate
57 addFunction "run_json_filter" (runJSONFilter mbDatadir)
59 addFunction "run_json_filter" (runJSONFilter mbDatadir)
@@ -112,7 +114,9 @@ stringify el = return $ case el of
112 InlineElement i -> Shared.stringify i
114 InlineElement i -> Shared.stringify i
113 BlockElement b -> Shared.stringify b
115 BlockElement b -> Shared.stringify b
114 MetaElement m -> Shared.stringify m
116 MetaElement m -> Shared.stringify m
117+ CitationElement c -> Shared.stringify c
115 MetaValueElement m -> stringifyMetaValue m
118 MetaValueElement m -> stringifyMetaValue m
119+ _ -> ""
116
120
117stringifyMetaValue :: MetaValue -> String
121stringifyMetaValue :: MetaValue -> String
118stringifyMetaValue mv = case mv of
122stringifyMetaValue mv = case mv of
@@ -120,19 +124,27 @@ stringifyMetaValue mv = case mv of
120 MetaString s -> s
124 MetaString s -> s
121 _ -> Shared.stringify mv
125 _ -> Shared.stringify mv
122
126
127+equals :: AstElement -> AstElement -> Lua Bool
128+equals e1 e2 = return (e1 == e2)
129+
123data AstElement
130data AstElement
124 = PandocElement Pandoc
131 = PandocElement Pandoc
125 | MetaElement Meta
132 | MetaElement Meta
126 | BlockElement Block
133 | BlockElement Block
127 | InlineElement Inline
134 | InlineElement Inline
128 | MetaValueElement MetaValue
135 | MetaValueElement MetaValue
136+ | AttrElement Attr
137+ | ListAttributesElement ListAttributes
138+ | CitationElement Citation
129− deriving (Show)
139+ deriving (Eq, Show)
130
140
131instance Peekable AstElement where
141instance Peekable AstElement where
132 peek idx = do
142 peek idx = do
133 res <- Lua.try $ (PandocElement <$> Lua.peek idx)
143 res <- Lua.try $ (PandocElement <$> Lua.peek idx)
134 <|> (InlineElement <$> Lua.peek idx)
144 <|> (InlineElement <$> Lua.peek idx)
135 <|> (BlockElement <$> Lua.peek idx)
145 <|> (BlockElement <$> Lua.peek idx)
146+ <|> (AttrElement <$> Lua.peek idx)
147+ <|> (ListAttributesElement <$> Lua.peek idx)
136 <|> (MetaElement <$> Lua.peek idx)
148 <|> (MetaElement <$> Lua.peek idx)
137 <|> (MetaValueElement <$> Lua.peek idx)
149 <|> (MetaValueElement <$> Lua.peek idx)
138 case res of
150 case res of