File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -48,13 +48,16 @@ module Foreign.Lua.Module.DocLayout (
4848
4949 -- * Functions
5050 , render
51+ , concat
5152
5253 -- * Marshaling
5354 , peekDoc
5455 , pushDoc
5556 )
5657where
5758
59+ import Prelude hiding (concat )
60+ import Data.List (intersperse )
5861import Data.Text (Text )
5962import Foreign.Lua (Lua , NumResults (.. ), Optional ,
6063 Peekable , Pushable , StackIndex )
@@ -101,6 +104,7 @@ pushModule = do
101104 Lua. addfunction " rblock" rblock
102105 Lua. addfunction " vfill" vfill
103106 -- renderign
107+ Lua. addfunction " concat" concat
104108 Lua. addfunction " render" render
105109 return 1
106110
@@ -115,6 +119,13 @@ preloadModule = flip Lua.preloadhs pushModule
115119render :: Doc Text -> Optional Int -> Lua Text
116120render doc optLength = return $ Doc. render (Lua. fromOptional optLength) doc
117121
122+ -- | Concatenates a list of @'Doc'@s.
123+ concat :: [Doc Text ] -> Optional (Doc Text ) -> Lua (Doc Text )
124+ concat docs optSep = return $
125+ case Lua. fromOptional optSep of
126+ Nothing -> mconcat docs
127+ Just sep -> mconcat $ intersperse sep docs
128+
118129--
119130-- Constructors
120131--
Original file line number Diff line number Diff line change @@ -30,6 +30,28 @@ return {
3030 test (' empty doc' , function ()
3131 assert .are_equal (doclayout .render (doclayout .empty ), ' ' )
3232 end )
33+ },
34+
35+ group ' concat' {
36+ test (' without sep' , function ()
37+ local list = {
38+ doclayout .literal ' one' ,
39+ doclayout .literal ' two' ,
40+ doclayout .literal ' three'
41+ }
42+ local sep = doclayout .cr
43+ assert .are_equal (
44+ doclayout .concat (list , sep ),
45+ list [1 ] .. sep .. list [2 ] .. sep .. list [3 ])
46+ end ),
47+ test (' without sep' , function ()
48+ local list = {
49+ doclayout .literal ' one' ,
50+ doclayout .literal ' two' ,
51+ doclayout .literal ' three'
52+ }
53+ assert .are_equal (doclayout .concat (list ), list [1 ] .. list [2 ] .. list [3 ])
54+ end ),
3355 }
3456 },
3557
You can’t perform that action at this time.
0 commit comments