-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDocLayout.hs
More file actions
658 lines (587 loc) · 18.5 KB
/
DocLayout.hs
File metadata and controls
658 lines (587 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-|
Module : HsLua.Module.DocLayout
Copyright : © 2020-2026 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <albert+hslua@zeitkraut.de>
Provides a Lua module which wraps @'Text.DocLayout'@. The @'Doc'@
type is specialized to @'Text'@.
This module defines orphan instances for @Doc Text@.
-}
module HsLua.Module.DocLayout (
-- * Module
documentedModule
, pushModule
, preloadModule
, description
, fields
, functions
-- * Doc constructors and combinators
, after_break
, before_non_blank
, blankline
, blanklines
, braces
, brackets
, cblock
, chomp
, concat
, cr
, double_quotes
, empty
, flush
, hang
, inside
, lblock
, literal
, nest
, nestle
, nowrap
, parens
, prefixed
, quotes
, rblock
, space
, vfill
-- * Rendering
, render
-- * Document Querying
, is_empty
, height
, min_offset
, offset
, real_length
, update_column
-- * Styling
, bold
, italic
, underlined
, strikeout
, fg
, bg
-- * Marshaling
, peekDoc
, pushDoc
)
where
import Prelude hiding (concat)
import Data.List (intersperse)
import Data.Text (Text)
import HsLua as Lua hiding (concat)
import Text.DocLayout (Doc, (<+>), ($$), ($+$))
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Text.DocLayout as Doc
--
-- Module
--
-- | Textual description of the "doclayout" module.
description :: Text
description = "Plain-text document layouting."
-- | Self-documenting module.
documentedModule :: LuaError e => Module e
documentedModule = defmodule "doclayout"
`withFields` fields
`withDescription` description
`withFunctions` functions
`associateType` typeDoc
--
-- Fields
--
-- | Exposed fields.
fields :: forall e. LuaError e => [Field e]
fields =
[ blankline
, cr
, empty
, space
]
-- | Wrapped and documented 'Doc.blankline' value.
blankline :: forall e. LuaError e => Field e
blankline = deffield "blankline"
`withDescription` "Inserts a blank line unless one exists already."
`withType` udTypeSpec @e typeDoc
`withValue` pushDoc Doc.blankline
-- | Wrapped and documented 'Doc.cr' value.
cr :: forall e. LuaError e => Field e
cr = deffield "cr"
`withDescription`
"A carriage return. Does nothing if we're at " <>
"the beginning of a line; otherwise inserts a newline."
`withType` udTypeSpec @e typeDoc
`withValue` pushDoc Doc.cr
-- | Wrapped and documented 'Doc.empty' value.
empty :: forall e. LuaError e => Field e
empty = deffield "empty"
`withDescription` "The empty document."
`withType` udTypeSpec @e typeDoc
`withValue` pushDoc Doc.empty
-- | Wrapped and documented 'Doc.space' value.
space :: forall e. LuaError e => Field e
space = deffield "space"
`withDescription` "A breaking (reflowable) space."
`withType` udTypeSpec @e typeDoc
`withValue` pushDoc Doc.space
--
-- Functions
--
-- | Exposed module functions.
functions :: LuaError e => [DocumentedFunction e]
functions =
[ -- Constructors
after_break
, before_non_blank
, blanklines
, braces
, brackets
, cblock
, chomp
, concat
, double_quotes
, flush
, hang
, inside
, lblock
, literal
, nest
, nestle
, nowrap
, parens
, prefixed
, quotes
, rblock
, vfill
-- rendering
, render
-- querying
, is_empty
, height
, min_offset
, offset
, real_length
, update_column
-- styling
, bold
, italic
, underlined
, strikeout
, fg
, bg
]
typeDoc :: LuaError e => DocumentedType e (Doc Text)
typeDoc = deftype "Doc"
[ operation Add $ binaryOp (<+>)
"Concatenated docs, with breakable space between them."
, operation Concat $ binaryOp (<>) "Concatenation of the input docs"
, operation Div $ binaryOp ($$) "Puts a above b"
, operation Eq $ lambda
### liftPure2 (==)
<#> docParam "a"
<#> docParam "b"
=#> boolResult "whether the two Docs are equal"
, operation Idiv $ binaryOp ($+$) "Puts a above b"
, operation Tostring $ lambda
### liftPure (Doc.render Nothing)
<#> docParam "doc"
=#> textResult "Rendered Doc without reflowing."
]
[ method before_non_blank
, method braces
, method brackets
, method cblock
, method chomp
, method double_quotes
, method is_empty
, method flush
, method hang
, method height
, method inside
, method lblock
, method min_offset
, method nest
, method nestle
, method nowrap
, method offset
, method parens
, method prefixed
, method quotes
, method rblock
, method render
, method update_column
, method vfill
]
where
binaryOp op descr = lambda
### liftPure2 op
<#> docParam "a"
<#> docParam "b"
=#> docResult descr
-- | Render a @'Doc'@. The text is reflowed on breakable spaces
-- to match the given line length. Text is not reflowed if the
-- line length parameter is omitted or nil.
render :: LuaError e => DocumentedFunction e
render = defun "render"
### (\doc mbcolwidth useAnsi ->
if useAnsi == Just True
then pure $ TL.toStrict $ Doc.renderANSI mbcolwidth doc
else pure $ Doc.renderPlain mbcolwidth doc)
<#> docParam "doc"
<#> opt (integralParam "colwidth" $
"Maximum number of characters per line.\n" <>
"A value of `nil`, the default, means that the text " <>
"is not reflown.")
<#> opt (parameter peekRenderStyle "string" "style" $
"Whether to generate plain text or ANSI terminal output.\n" <>
"Must be either `'plain'` or `'ansi'`.\n" <>
"Defaults to `'plain'`.")
=#> functionResult pushText "string" "rendered doc"
#? T.unlines
[ "Render a [[Doc]]. The text is reflowed on breakable spaces to"
, "match the given line length. Text is not reflowed if the line"
, "line length parameter is omitted or nil."
]
where
peekRenderStyle idx = peekByteString idx >>= \case
"ansi" -> pure True
"ANSI" -> pure True
"plain" -> pure False
style -> failPeek $ "Unknown rendering style: " <> style
--
-- Querying
--
-- | @True@ iff the document is empty.
is_empty :: LuaError e => DocumentedFunction e
is_empty = defun "is_empty"
### liftPure Doc.isEmpty
<#> docParam "doc"
=#> boolResult "`true` iff `doc` is the empty document, `false` otherwise."
#? "Checks whether a doc is empty."
-- | Returns the width of a @'Doc'@.
offset :: LuaError e => DocumentedFunction e
offset = defun "offset"
### liftPure Doc.offset
<#> docParam "doc"
=#> integralResult "doc width"
#? "Returns the width of a [[Doc]] as number of characters."
-- | Returns the minimal width of a @'Doc'@ when reflowed at
-- breakable spaces.
min_offset :: LuaError e => DocumentedFunction e
min_offset = defun "min_offset"
### liftPure Doc.minOffset
<#> docParam "doc"
=#> integralResult "minimal possible width"
#? ("Returns the minimal width of a [[Doc]] when reflowed at " <>
"breakable spaces.")
-- | Returns the column that would be occupied by the last laid
-- out character.
update_column :: LuaError e => DocumentedFunction e
update_column = defun "update_column"
### liftPure2 Doc.updateColumn
<#> docParam "doc"
<#> integralParam "i" "start column"
=#> integralResult "column number"
#? ("Returns the column that would be occupied by the last " <>
"laid out character.")
-- | Returns the height of a block or other Doc.
height :: LuaError e => DocumentedFunction e
height = defun "height"
### liftPure Doc.height
<#> docParam "doc"
=#> integralResult "doc height"
#? "Returns the height of a block or other Doc."
-- | Returns the real length of a string in a monospace font: 0
-- for a combining character, 1, for a regular character, 2 for
-- an East Asian wide character.
real_length :: DocumentedFunction e
real_length = defun "real_length"
### liftPure Doc.realLength
<#> textParam "str" "UTF-8 string to measure"
=#> integralResult "text length"
#? ("Returns the real length of a string in a monospace font: " <>
"0 for a combining character, 1 for a regular character, " <>
"2 for an East Asian wide character.")
--
-- Constructors
--
-- | Creates a @'Doc'@ which is conditionally included only if it
-- comes at the beginning of a line.
after_break :: LuaError e => DocumentedFunction e
after_break = defun "after_break"
### liftPure Doc.afterBreak
<#> textParam "text" "content to include when placed after a break"
=#> docResult "new doc"
#? ("Creates a [[Doc]] which is conditionally included only if it " <>
"comes at the beginning of a line.\n\n" <>
"An example where this is useful is for escaping line-initial " <>
"`.` in roff man.")
-- | Conditionally includes the given @'Doc'@ unless it is
-- followed by a blank space.
before_non_blank :: LuaError e => DocumentedFunction e
before_non_blank = defun "before_non_blank"
### liftPure Doc.beforeNonBlank
<#> docParam "doc"
=#> docResult "conditional doc"
#? ("Conditionally includes the given `doc` unless it is " <>
"followed by a blank space.")
-- | Insert blank lines unless they exist already.
blanklines :: LuaError e => DocumentedFunction e
blanklines = defun "blanklines"
### liftPure Doc.blanklines
<#> integralParam "n" "number of blank lines"
=#> docResult "conditional blank lines"
#? "Inserts blank lines unless they exist already."
-- | Puts a @'Doc'@ in curly braces.
braces :: LuaError e => DocumentedFunction e
braces = defun "braces"
### liftPure Doc.braces
<#> docParam "doc"
=#> docResult "`doc` enclosed by {}."
#? "Puts the `doc` in curly braces."
-- | Puts a @'Doc'@ in square brackets.
brackets :: LuaError e => DocumentedFunction e
brackets = defun "brackets"
### liftPure Doc.brackets
<#> docParam "doc"
=#> docResult "doc enclosed by []."
#? "Puts the `doc` in square brackets"
-- | Like @'lblock'@ but aligned centered.
cblock :: LuaError e => DocumentedFunction e
cblock = defun "cblock"
### liftPure2 (flip Doc.cblock)
<#> docParam "doc"
<#> integralParam "width" "block width in chars"
=#> docResult ("doc, aligned centered in a block with max " <>
"`width` chars per line.")
#? ("Creates a block with the given width and content, " <>
"aligned centered.")
-- | Chomps trailing blank space off of a @'Doc'@.
chomp :: LuaError e => DocumentedFunction e
chomp = defun "chomp"
### liftPure Doc.chomp
<#> docParam "doc"
=#> docResult "`doc` without trailing blanks"
#? "Chomps trailing blank space off of the `doc`."
-- | Concatenates a list of @'Doc'@s.
concat :: LuaError e => DocumentedFunction e
concat = defun "concat"
### liftPure2 (\docs optSep -> mconcat $
maybe docs (`intersperse` docs) optSep)
<#> parameter (peekList peekDoc) "`{Doc,...}`" "docs" "list of Docs"
<#> opt (parameter peekDoc "Doc" "sep" "separator (default: none)")
=#> docResult "concatenated doc"
#? "Concatenates a list of `Doc`s."
-- | Wraps a @'Doc'@ in double quotes
double_quotes :: LuaError e => DocumentedFunction e
double_quotes = defun "double_quotes"
### liftPure Doc.doubleQuotes
<#> docParam "doc"
=#> docResult "`doc` enclosed by `\"` chars"
#? "Wraps a `Doc` in double quotes."
-- | Makes a @'Doc'@ flush against the left margin.
flush :: LuaError e => DocumentedFunction e
flush = defun "flush"
### liftPure Doc.flush
<#> docParam "doc"
=#> docResult "flushed `doc`"
#? "Makes a `Doc` flush against the left margin."
-- | Creates a hanging indent.
hang :: LuaError e => DocumentedFunction e
hang = defun "hang"
### liftPure3 (\doc ind start -> Doc.hang ind start doc)
<#> docParam "doc"
<#> integralParam "ind" "indentation width"
<#> docParam "start"
=#> docResult ("`doc` prefixed by `start` on the first line, " <>
"subsequent lines indented by `ind` spaces.")
#? "Creates a hanging indent."
-- | Encloses a @'Doc'@ inside a start and end @'Doc'@.
inside :: LuaError e => DocumentedFunction e
inside = defun "inside"
### liftPure3 (\contents start end -> Doc.inside start end contents)
<#> docParam "contents"
<#> docParam "start"
<#> docParam "end"
=#> docResult "enclosed contents"
#? "Encloses a [[Doc]] inside a start and end [[Doc]]."
-- | Creates a block with the given width and content, aligned to
-- the left.
lblock :: LuaError e => DocumentedFunction e
lblock = defun "lblock"
### liftPure2 (flip Doc.lblock)
<#> docParam "doc"
<#> integralParam "width" "block width in chars"
=#> docResult "doc put into block with max `width` chars per line."
#? ("Creates a block with the given width and content, " <>
"aligned to the left.")
-- | Creates a @'Doc'@ from a string.
literal :: LuaError e => DocumentedFunction e
literal = defun "literal"
### liftPure Doc.literal
<#> textParam "text" "literal value"
=#> docResult "doc contatining just the literal string"
#? "Creates a `Doc` from a string."
-- | Indents a @'Doc'@ by the specified number of spaces.
nest :: LuaError e => DocumentedFunction e
nest = defun "nest"
### liftPure2 (flip Doc.nest)
<#> docParam "doc"
<#> integralParam "ind" "indentation size"
=#> docResult "`doc` indented by `ind` spaces"
#? "Indents a `Doc` by the specified number of spaces."
-- | Removes leading blank lines from a @'Doc'@.
nestle :: LuaError e => DocumentedFunction e
nestle = defun "nestle"
### liftPure Doc.nestle
<#> docParam "doc"
=#> docResult "`doc` with leading blanks removed"
#? "Removes leading blank lines from a `Doc`."
-- | Makes a @'Doc'@ non-reflowable.
nowrap :: LuaError e => DocumentedFunction e
nowrap = defun "nowrap"
### liftPure Doc.nowrap
<#> docParam "doc"
=#> docResult "same as input, but non-reflowable"
#? "Makes a `Doc` non-reflowable."
-- | Puts a @'Doc'@ in parentheses.
parens :: LuaError e => DocumentedFunction e
parens = defun "parens"
### liftPure Doc.parens
<#> docParam "doc"
=#> docResult "doc enclosed by ()."
#? "Puts the `doc` in parentheses."
-- | Uses the specified string as a prefix for every line of the
-- inside document (except the first, if not at the beginning of
-- the line).
prefixed :: LuaError e => DocumentedFunction e
prefixed = defun "prefixed"
### liftPure2 (flip Doc.prefixed)
<#> docParam "doc"
<#> stringParam "prefix" "prefix for each line"
=#> docResult "prefixed `doc`"
#? ("Uses the specified string as a prefix for every line of " <>
"the inside document (except the first, if not at the " <>
"beginning of the line).")
-- | Wraps a @'Doc'@ in single quotes.
quotes :: LuaError e => DocumentedFunction e
quotes = defun "quotes"
### liftPure Doc.quotes
<#> docParam "doc"
=#> docResult "doc enclosed in `'`."
#? "Wraps a `Doc` in single quotes."
-- | Like @'rblock'@ but aligned to the right.
rblock :: LuaError e => DocumentedFunction e
rblock = defun "rblock"
### liftPure2 (flip Doc.rblock)
<#> docParam "doc"
<#> integralParam "width" "block width in chars"
=#> docResult ("doc, right aligned in a block with max " <>
"`width` chars per line.")
#? ("Creates a block with the given width and content, " <>
"aligned to the right.")
-- | An expandable border that, when placed next to a box,
-- expands to the height of the box. Strings cycle through the
-- list provided.
vfill :: LuaError e => DocumentedFunction e
vfill = defun "vfill"
### liftPure Doc.vfill
<#> textParam "border" "vertically expanded characters"
=#> docResult "automatically expanding border Doc"
#? ("An expandable border that, when placed next to a box, " <>
"expands to the height of the box. Strings cycle through the " <>
"list provided.")
--
-- Styling
--
bold :: LuaError e => DocumentedFunction e
bold = defun "bold"
### liftPure Doc.bold
<#> docParam "doc"
=#> docResult "bolded Doc"
#? "Puts a [[Doc]] in boldface."
italic :: LuaError e => DocumentedFunction e
italic = defun "italic"
### liftPure Doc.italic
<#> docParam "doc"
=#> docResult "styled Doc"
#? "Puts a [[Doc]] in italics."
underlined :: LuaError e => DocumentedFunction e
underlined = defun "underlined"
### liftPure Doc.underlined
<#> docParam "doc"
=#> docResult "styled Doc"
#? "Underlines a [[Doc]]."
strikeout :: LuaError e => DocumentedFunction e
strikeout = defun "strikeout"
### liftPure Doc.strikeout
<#> docParam "doc"
=#> docResult "styled Doc"
#? "Puts a line through the [[Doc]]."
fg :: LuaError e => DocumentedFunction e
fg = defun "fg"
### liftPure2 (flip Doc.fg)
<#> docParam "doc"
<#> colorParam
=#> docResult "styled Doc"
#? "Set the foreground color."
bg :: LuaError e => DocumentedFunction e
bg = defun "bg"
### liftPure2 (flip Doc.bg)
<#> docParam "doc"
<#> colorParam
=#> docResult "styled Doc"
#? "Set the background color."
--
-- Marshaling
--
-- | Retrieve a @Doc Text@ value from the Lua stack. Strings are
-- converted to plain @'Doc'@ values.
peekDoc :: LuaError e => Peeker e (Doc Text)
peekDoc idx = liftLua (Lua.ltype idx) >>= \case
Lua.TypeString -> let stringToDoc s = if T.null s
then Doc.empty
else Doc.literal s
in stringToDoc <$> Lua.peekText idx
Lua.TypeNumber -> Doc.literal <$> Lua.peekText idx
_ -> peekUD typeDoc idx
-- | Push a @Doc Text@ value to the Lua stack.
pushDoc :: LuaError e => Pusher e (Doc Text)
pushDoc = pushUD typeDoc
instance Peekable (Doc Text) where
safepeek = peekDoc
instance Pushable (Doc Text) where
push = pushDoc
--
-- Parameters
--
-- | @Doc@ typed function parameter.
docParam :: LuaError e => Text -> Parameter e (Doc Text)
docParam name = parameter peekDoc "Doc" name "document"
-- | @Color@ function parameter
colorParam :: Parameter e Doc.Color
colorParam = parameter peekColor "string" "color"
("One of 'black', 'red', 'green', 'yellow', 'blue', 'magenta' " <>
"'cyan', or 'white'.")
where
peekColor idx = peekByteString idx >>= \case
"black" -> pure Doc.black
"red" -> pure Doc.red
"green" -> pure Doc.green
"yellow" -> pure Doc.yellow
"blue" -> pure Doc.blue
"magenta" -> pure Doc.magenta
"cyan" -> pure Doc.cyan
"white" -> pure Doc.white
color -> failPeek $ "Unknown color: " <> color
--
-- Results
--
-- | Function result of type @'Doc'@.
docResult :: LuaError e
=> Text -- ^ Description
-> FunctionResults e (Doc Text)
docResult = functionResult pushDoc "Doc"