git.iko.soy/typeable/pandoc
on master help sign in
Lua: add module for AST element sequence traversal

Lua filters must be able to traverse sequences of AST elements and to replace elements by splicing sequences back in their place. Special `Walkable` instances can be used for this; those are provided in a new module `Text.Pandoc.Lua.Walk`.

commit813e1fc7e0705f11ff374ffd525e8868edd0045a authorAlbert Krewinkel <albert@zeitkraut.de> dateThu 15 Aug 2019, 22:53 (7 years ago) parent903d2f9
4 files changed+120
cabal.project+5 pandoc.cabal+1 src/Text/Pandoc/Lua/Walk.hs+112 stack.yaml+2
cabal.project+5
@@ -12,3 +12,8 @@ source-repository-package
1212 type: git
1313 location: https://github.com/jgm/pandoc-citeproc
1414 tag: 6d62678ece91bbb4fe4f5a99695006e1d53c3bae
15+
16+source-repository-package
17+ type: git
18+ location: https://github.com/jgm/pandoc-types
19+ tag: 996a61018e406aa1e333e9085e84a04c83804c34
pandoc.cabal+1
@@ -606,6 +606,7 @@ library
606606 Text.Pandoc.Lua.Module.Utils,
607607 Text.Pandoc.Lua.Packages,
608608 Text.Pandoc.Lua.Util,
609+ Text.Pandoc.Lua.Walk,
609610 Text.Pandoc.CSS,
610611 Text.Pandoc.CSV,
611612 Text.Pandoc.RoffChar,
src/Text/Pandoc/Lua/Walk.hsnew file+112
@@ -0,0 +1,112 @@
1+{-# LANGUAGE DeriveTraversable #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE NoImplicitPrelude #-}
5+{-# LANGUAGE MultiParamTypeClasses #-}
6+{- |
7+Module : Text.Pandoc.Lua.Walk
8+Copyright : © 2012–2019 John MacFarlane,
9+ © 2017-2019 Albert Krewinkel
10+License : GNU GPL, version 2 or above
11+Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
12+Stability : alpha
13+
14+Walking documents in a filter-suitable way.
15+-}
16+module Text.Pandoc.Lua.Walk
17+ ( SingletonsList (..)
18+ )
19+where
20+
21+import Prelude
22+import Control.Monad ((<=<))
23+import Text.Pandoc.Definition
24+import Text.Pandoc.Walk
25+
26+-- | Helper type which allows to traverse trees in order, while splicing in
27+-- trees.
28+--
29+-- The only interesting use of this type is via it's '@Walkable@' instance. That
30+-- instance makes it possible to walk a Pandoc document (or a subset thereof),
31+-- while applying a function on each element of an AST element /list/, and have
32+-- the resulting list spliced back in place of the original element. This is the
33+-- traversal/splicing method used for Lua filters.
34+newtype SingletonsList a = SingletonsList { singletonsList :: [a] }
35+ deriving (Functor, Foldable, Traversable)
36+
37+--
38+-- SingletonsList Inline
39+--
40+instance {-# OVERLAPPING #-} Walkable (SingletonsList Inline) [Inline] where
41+ walkM = walkSingletonsListM
42+ query = querySingletonsList
43+
44+instance Walkable (SingletonsList Inline) Pandoc where
45+ walkM = walkPandocM
46+ query = queryPandoc
47+
48+instance Walkable (SingletonsList Inline) Citation where
49+ walkM = walkCitationM
50+ query = queryCitation
51+
52+instance Walkable (SingletonsList Inline) Inline where
53+ walkM = walkInlineM
54+ query = queryInline
55+
56+instance Walkable (SingletonsList Inline) Block where
57+ walkM = walkBlockM
58+ query = queryBlock
59+
60+instance Walkable (SingletonsList Inline) MetaValue where
61+ walkM = walkMetaValueM
62+ query = queryMetaValue
63+
64+instance Walkable (SingletonsList Inline) Meta where
65+ walkM f (Meta metamap) = Meta <$> walkM f metamap
66+ query f (Meta metamap) = query f metamap
67+
68+--
69+-- SingletonsList Block
70+--
71+instance {-# OVERLAPPING #-} Walkable (SingletonsList Block) [Block] where
72+ walkM = walkSingletonsListM
73+ query = querySingletonsList
74+
75+instance Walkable (SingletonsList Block) Pandoc where
76+ walkM = walkPandocM
77+ query = queryPandoc
78+
79+instance Walkable (SingletonsList Block) Citation where
80+ walkM = walkCitationM
81+ query = queryCitation
82+
83+instance Walkable (SingletonsList Block) Inline where
84+ walkM = walkInlineM
85+ query = queryInline
86+
87+instance Walkable (SingletonsList Block) Block where
88+ walkM = walkBlockM
89+ query = queryBlock
90+
91+instance Walkable (SingletonsList Block) MetaValue where
92+ walkM = walkMetaValueM
93+ query = queryMetaValue
94+
95+instance Walkable (SingletonsList Block) Meta where
96+ walkM f (Meta metamap) = Meta <$> walkM f metamap
97+ query f (Meta metamap) = query f metamap
98+
99+
100+walkSingletonsListM :: (Monad m, Walkable (SingletonsList a) a)
101+ => (SingletonsList a -> m (SingletonsList a))
102+ -> [a] -> m [a]
103+walkSingletonsListM f =
104+ let f' = fmap singletonsList . f . SingletonsList . (:[]) <=< walkM f
105+ in fmap mconcat . mapM f'
106+
107+querySingletonsList :: (Monoid c, Walkable (SingletonsList a) a)
108+ => (SingletonsList a -> c)
109+ -> [a] -> c
110+querySingletonsList f =
111+ let f' x = f (SingletonsList [x]) `mappend` query f x
112+ in mconcat . map f'
stack.yaml+2
@@ -14,6 +14,8 @@ extra-deps:
1414# - pandoc-citeproc-0.16.2
1515- git: https://github.com/jgm/pandoc-citeproc
1616 commit: 6d62678ece91bbb4fe4f5a99695006e1d53c3bae
17+- git: https://github.com/tarleb/pandoc-types
18+ commit: a087b0174a597b92c5fec4d633c46887c188b496
1719- ipynb-0.1
1820- cmark-gfm-0.2.0
1921- hslua-1.0.3.1