-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDocLayout.hs
More file actions
1331 lines (1187 loc) · 50.5 KB
/
DocLayout.hs
File metadata and controls
1331 lines (1187 loc) · 50.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Text.DocLayout
Copyright : Copyright (C) 2010-2019 John MacFarlane
License : BSD 3
Maintainer : John MacFarlane <jgm@berkeley.edu>
Stability : alpha
Portability : portable
A prettyprinting library for the production of text documents,
including wrapped text, indentation and other prefixes, and
blocks for tables.
-}
module Text.DocLayout (
-- * Rendering
render
, renderPlain
, renderANSI
-- * Doc constructors
, cr
, blankline
, blanklines
, space
, literal
, text
, char
, prefixed
, flush
, nest
, hang
, beforeNonBlank
, nowrap
, afterBreak
, lblock
, cblock
, rblock
, vfill
, nestle
, chomp
, inside
, braces
, brackets
, parens
, quotes
, doubleQuotes
, bold
, italic
, underlined
, strikeout
, fg
, bg
, Color
, black
, red
, green
, yellow
, blue
, magenta
, cyan
, white
, link
, empty
-- * Functions for concatenating documents
, (<+>)
, ($$)
, ($+$)
, hcat
, hsep
, vcat
, vsep
-- * Functions for querying documents
, isEmpty
, offset
, minOffset
, updateColumn
, height
, charWidth
, realLength
, realLengthNarrowContext
, realLengthWideContext
, realLengthNarrowContextNoShortcut
, realLengthWideContextNoShortcut
-- * Char properties
, isSkinToneModifier
, isEmojiVariation
, isZWJ
-- * Utility functions
, unfoldD
-- * Types
, Doc(..)
, HasChars(..)
, Attributed
)
where
import Prelude
import Data.Maybe (fromMaybe, isJust, mapMaybe)
import Safe (lastMay, initSafe)
import Control.Monad
import Control.Monad.State.Strict
import GHC.Generics
import Data.Bifunctor (second)
import Data.Char (isSpace, ord)
import Data.List (foldl', intersperse)
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Data.List.NonEmpty as N
import qualified Data.IntMap.Strict as IM
import qualified Data.Map.Strict as M
import qualified Data.Map.Internal as MInt
import Data.Data (Data, Typeable)
import Data.Foldable (toList)
import Data.String
import qualified Data.Text as T
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as B
import Text.DocLayout.HasChars
import Text.DocLayout.ANSIFont
import Text.DocLayout.Attributed
#if MIN_VERSION_base(4,11,0)
#else
import Data.Semigroup
#endif
import Text.Emoji (baseEmojis)
-- | Document, including structure relevant for layout.
data Doc a = Text Int a -- ^ Text with specified width.
| Block Int [Attributed a] -- ^ A block with a width and lines.
| VFill Int a -- ^ A vertically expandable block;
-- when concatenated with a block, expands to height
-- of block, with each line containing the specified text.
| CookedText Int (Attributed a) -- ^ Text which doesn't need further cooking
| Prefixed Text (Doc a) -- ^ Doc with each line prefixed with text.
-- Note that trailing blanks are omitted from the prefix
-- when the line after it is empty.
| BeforeNonBlank (Doc a) -- ^ Doc that renders only before nonblank.
| Flush (Doc a) -- ^ Doc laid out flush to left margin.
| BreakingSpace -- ^ A space or line break, in context.
| AfterBreak Text -- ^ Text printed only at start of line.
| CarriageReturn -- ^ Newline unless we're at start of line.
| NewLine -- ^ newline.
| BlankLines Int -- ^ Ensure a number of blank lines.
| Concat (Doc a) (Doc a) -- ^ Two documents concatenated.
| Styled StyleReq (Doc a)
| Linked Text (Doc a) -- ^ A hyperlink
| Empty
deriving (Show, Read, Eq, Ord, Functor, Foldable, Traversable,
Data, Typeable, Generic)
instance Semigroup (Doc a) where
x <> Empty = x
Empty <> x = x
x <> y = Concat x y
instance Monoid (Doc a) where
mappend = (<>)
mempty = Empty
instance HasChars a => IsString (Doc a) where
fromString = text
{-# DEPRECATED unfoldD "unfoldD will be removed from the API." #-}
-- | Unfold a 'Doc' into a flat list.
unfoldD :: Doc a -> [Doc a]
unfoldD Empty = []
unfoldD (Concat x@Concat{} y) = unfoldD x <> unfoldD y
unfoldD (Concat x y) = x : unfoldD y
unfoldD x = [x]
-- | True if the document is empty.
isEmpty :: Doc a -> Bool
isEmpty Empty = True
isEmpty _ = False
-- | The empty document.
empty :: Doc a
empty = mempty
-- | Concatenate documents horizontally.
hcat :: [Doc a] -> Doc a
hcat = mconcat
-- | Concatenate a list of 'Doc's, putting breakable spaces
-- between them.
infixr 6 <+>
(<+>) :: Doc a -> Doc a -> Doc a
(<+>) x y
| isEmpty x = y
| isEmpty y = x
| otherwise = x <> space <> y
-- | Same as 'hcat', but putting breakable spaces between the
-- 'Doc's.
hsep :: [Doc a] -> Doc a
hsep = foldr (<+>) empty
infixr 5 $$
-- | @a $$ b@ puts @a@ above @b@.
($$) :: Doc a -> Doc a -> Doc a
($$) x y
| isEmpty x = y
| isEmpty y = x
| otherwise = x <> cr <> y
infixr 5 $+$
-- | @a $+$ b@ puts @a@ above @b@, with a blank line between.
($+$) :: Doc a -> Doc a -> Doc a
($+$) x y
| isEmpty x = y
| isEmpty y = x
| otherwise = x <> blankline <> y
-- | List version of '$$'.
vcat :: [Doc a] -> Doc a
vcat = foldr ($$) empty
-- | List version of '$+$'.
vsep :: [Doc a] -> Doc a
vsep = foldr ($+$) empty
-- | Removes leading blank lines from a 'Doc'.
nestle :: Doc a -> Doc a
nestle d =
case d of
BlankLines _ -> Empty
NewLine -> Empty
Concat (Concat x y) z -> nestle (Concat x (Concat y z))
Concat BlankLines{} x -> nestle x
Concat NewLine x -> nestle x
Concat CarriageReturn x -> nestle x
_ -> d
-- | Chomps trailing blank space off of a 'Doc'.
chomp :: Doc a -> Doc a
chomp d =
case d of
BlankLines _ -> Empty
NewLine -> Empty
CarriageReturn -> Empty
BreakingSpace -> Empty
Prefixed s d' -> Prefixed s (chomp d')
Concat (Concat x y) z -> chomp (Concat x (Concat y z))
Concat x y ->
case chomp y of
Empty -> chomp x
z -> x <> z
_ -> d
-- Elements of a document with Styled and Linked subtrees flattened out into
-- a linear structure with open and close tags. An implementation detail of
-- the rendering process.
data FlatDoc a = FText Int a
| FBlock Int [Attributed a]
| FVFill Int a
| FCookedText Int (Attributed a)
| FPrefixed Text (NonEmpty (FlatDoc a))
| FBeforeNonBlank (NonEmpty (FlatDoc a))
| FFlush (NonEmpty (FlatDoc a))
| FBreakingSpace
| FAfterBreak (NonEmpty (FlatDoc a))
| FCarriageReturn
| FNewLine
| FBlankLines Int
| FStyleOpen StyleReq
| FStyleClose
| FLinkOpen Text
| FLinkClose
deriving (Show, Read, Eq, Ord, Functor, Foldable, Traversable,
Data, Typeable, Generic)
-- Given a Doc, return an equivalent list of FlatDocs, to be processed by
-- renderList. Worth noting:
-- * Treelike docs (Styled, and Linked) are turned into lists beginning
-- with an "open" tag and ending with a "close" tag, with the flattened
-- inner content in between.
-- * Other Docs with inner content are eliminated if the inner content is
-- empty, otherwise the inner content is itself flattened and made into
-- a NonEmpty.
flatten :: HasChars a => Doc a -> [FlatDoc a]
flatten (Text n a) = [FText n a]
flatten (Block n a) = [FBlock n a]
flatten (VFill n a) = [FVFill n a]
flatten (CookedText n a) = [FCookedText n a]
flatten (Prefixed p d) | null f = []
| otherwise = [FPrefixed p (N.fromList f)]
where f = (normalize . flatten) d
flatten (BeforeNonBlank d) | null f = []
| otherwise = [FBeforeNonBlank (N.fromList f)]
where f = flatten d
flatten (Flush d) | null f = []
| otherwise = [FFlush (N.fromList f)]
where f = flatten d
flatten BreakingSpace = [FBreakingSpace]
flatten CarriageReturn = [FCarriageReturn]
flatten (AfterBreak t) | null f = []
| otherwise = [FAfterBreak (N.fromList f)]
where f = flatten $ fromString $ T.unpack t
flatten NewLine = [FNewLine]
flatten (BlankLines n) = [FBlankLines n]
flatten Empty = []
flatten (Concat x y) = flatten x <> flatten y
flatten (Linked l x) = FLinkOpen l : flatten x <> [FLinkClose]
flatten (Styled f x) = FStyleOpen f : flatten x <> [FStyleClose]
type DocState a = State (RenderState a) ()
data RenderState a = RenderState{
output :: [Attr a] -- ^ In reverse order
, prefix :: Text
, usePrefix :: Bool
, lineLength :: Maybe Int -- ^ 'Nothing' means no wrapping
, column :: Int
, newlines :: Int -- ^ Number of preceding newlines
, fontStack :: [Font]
, linkTarget :: Maybe Text -- ^ Current link target
}
peekFont :: RenderState a -> Font
peekFont st = case fontStack st of
[] -> baseFont
x:_ -> x
newline :: HasChars a => DocState a
newline = do
st' <- get
let rawpref = prefix st'
when (column st' == 0 && usePrefix st' && not (T.null rawpref)) $ do
let pref = fromString $ T.unpack $ T.dropWhileEnd isSpace rawpref
modify $ \st -> st{ output = Attr Nothing baseFont pref : output st
, column = column st + realLength pref }
modify $ \st -> st { output = Attr Nothing baseFont "\n" : output st
, column = 0
, newlines = newlines st + 1
}
outp :: HasChars a => Int -> a -> DocState a
outp off s = do -- offset >= 0 (0 might be combining char)
st' <- get
let pref = if usePrefix st' then fromString $ T.unpack $ prefix st' else mempty
let font = peekFont st'
when (column st' == 0 && not (isNull pref && font == baseFont)) $
modify $ \st -> st{ output = Attr Nothing baseFont pref : output st
, column = column st + realLength pref }
modify $ \st -> st{ output = Attr (linkTarget st) font s : output st
, column = column st + off
, newlines = 0 }
-- | Synonym for 'renderPlain'.
render :: HasChars a => Maybe Int -> Doc a -> a
render = renderPlain
-- | Render a 'Doc' with ANSI escapes. @renderANSI (Just n)@ will use
-- a line length of @n@ to reflow text on breakable spaces.
-- @renderANSI Nothing@ will not reflow text.
renderANSI :: HasChars a => Maybe Int -> Doc a -> TL.Text
renderANSI n d = B.toLazyText $ go $ prerender n d where
go s = (\(_,_,o) -> o) (go' s) <> B.fromText (renderFont baseFont) <> B.fromText (renderOSC8 Nothing)
go' (Attributed s) = foldl attrRender (Nothing, baseFont, B.fromText "") s
-- | Render a 'Doc' without using ANSI escapes. @renderPlain (Just n)@ will use
-- a line length of @n@ to reflow text on breakable spaces.
-- @renderPlain Nothing@ will not reflow text.
renderPlain :: HasChars a => Maybe Int -> Doc a -> a
renderPlain n d = go $ prerender n d where
go (Attributed s) = foldMap attrStrip s
attrStrip :: HasChars a => Attr a -> a
attrStrip (Attr _ _ y) | isNull y = ""
| otherwise = y
attrRender :: HasChars a => (Link, Font, B.Builder) -> Attr a -> (Link, Font, B.Builder)
attrRender (l, f, acc) (Attr m g y)
| isNull y = (l, f, acc)
| otherwise = (m, g, acc <> B.fromText newFont <> B.fromText newLink <> build y)
where
newLink = if l == m then mempty else renderOSC8 m
newFont = if f == g then mempty else renderFont g
prerender :: HasChars a => Maybe Int -> Doc a -> Attributed a
prerender linelen doc = fromList . reverse . output $
execState (renderDoc doc) startingState
where startingState = RenderState{
output = mempty
, prefix = mempty
, usePrefix = True
, lineLength = linelen
, column = 0
, newlines = 2
, fontStack = []
, linkTarget = Nothing }
renderDoc :: HasChars a => Doc a -> DocState a
renderDoc = renderList . normalize . flatten
normalize :: HasChars a => [FlatDoc a] -> [FlatDoc a]
normalize [] = []
normalize [FNewLine] = normalize [FCarriageReturn]
normalize [FBlankLines _] = normalize [FCarriageReturn]
normalize [FBreakingSpace] = []
normalize (FBlankLines m : FBlankLines n : xs) =
normalize (FBlankLines (max m n) : xs)
normalize (FBlankLines num : FBreakingSpace : xs) =
normalize (FBlankLines num : xs)
normalize (FBlankLines m : FCarriageReturn : xs) = normalize (FBlankLines m : xs)
normalize (FBlankLines m : FNewLine : xs) = normalize (FBlankLines m : xs)
normalize (FNewLine : FBlankLines m : xs) = normalize (FBlankLines m : xs)
normalize (FNewLine : FBreakingSpace : xs) = normalize (FNewLine : xs)
normalize (FNewLine : FCarriageReturn : xs) = normalize (FNewLine : xs)
normalize (FCarriageReturn : FCarriageReturn : xs) =
normalize (FCarriageReturn : xs)
normalize (FCarriageReturn : FBlankLines m : xs) = normalize (FBlankLines m : xs)
normalize (FCarriageReturn : FBreakingSpace : xs) =
normalize (FCarriageReturn : xs)
normalize (FBreakingSpace : FCarriageReturn : xs) =
normalize (FCarriageReturn:xs)
normalize (FBreakingSpace : FNewLine : xs) = normalize (FNewLine:xs)
normalize (FBreakingSpace : FBlankLines n : xs) = normalize (FBlankLines n:xs)
normalize (FBreakingSpace : FBreakingSpace : xs) = normalize (FBreakingSpace:xs)
normalize (x:xs) = x : normalize xs
mergeBlocks :: HasChars a => Int -> (Int, [a]) -> (Int, [a]) -> (Int, [a])
mergeBlocks h (w1,lns1) (w2,lns2) =
(w, zipWith (\l1 l2 -> pad w1 l1 <> l2) lns1' lns2')
where
w = w1 + w2
len1 = length $ take h lns1 -- note lns1 might be infinite
len2 = length $ take h lns2
lns1' = if len1 < h
then lns1 ++ replicate (h - len1) mempty
else take h lns1
lns2' = if len2 < h
then lns2 ++ replicate (h - len2) mempty
else take h lns2
pad n s = s <> replicateChar (n - realLength s) ' '
renderList :: HasChars a => [FlatDoc a] -> DocState a
renderList [] = return ()
renderList (FText off s : xs) = do
outp off s
renderList xs
renderList (FCookedText off s : xs) = do
st' <- get
let pref = if usePrefix st' then fromString $ T.unpack $ prefix st' else mempty
let elems (Attributed x) = reverse $ toList x
when (column st' == 0 && not (isNull pref)) $
modify $ \st -> st{ output = Attr Nothing baseFont pref : output st
, column = column st + realLength pref }
modify $ \st -> st{ output = elems s ++ output st
, column = column st + off
, newlines = 0 }
renderList xs
-- FStyleOpen and FStyleClose are balanced by construction when we create
-- them in `flatten`, so we can just pop the stack when we encounter
-- FStyleClose
renderList (FStyleOpen style : xs) = do
st <- get
let prevFont = peekFont st
let nextFont = prevFont ~> style
modify $ \s -> s{fontStack = nextFont : fontStack s}
renderList xs
renderList (FStyleClose : xs) = do
modify $ \s -> s{fontStack = drop 1 $ fontStack s}
renderList xs
-- Nested links are nonsensical, we only handle the outermost and
-- silently ignore any attempts to have a link inside a link
-- Nested links are nonsensical, we only handle the outermost and
-- silently ignore any attempts to have a link inside a link
renderList (FLinkOpen target : xs) = do
st <- get
case linkTarget st of
Nothing -> do
modify $ \s -> s{linkTarget = Just target}
renderList xs
_ -> do
let (next, rest) = break isLinkClose xs
renderList (next <> drop 1 rest)
where
isLinkClose FLinkClose = True
isLinkClose _ = False
renderList (FLinkClose : xs) = do
modify $ \s -> s{linkTarget = Nothing}
renderList xs
renderList (FPrefixed pref d : xs) = do
st <- get
let oldPref = prefix st
put st{ prefix = prefix st <> pref }
renderList $ normalize $ N.toList d
modify $ \s -> s{ prefix = oldPref }
-- renderDoc CarriageReturn
renderList xs
renderList (FFlush d : xs) = do
st <- get
let oldUsePrefix = usePrefix st
put st{ usePrefix = False }
renderList $ normalize $ N.toList d
modify $ \s -> s{ usePrefix = oldUsePrefix }
renderList xs
renderList (FBeforeNonBlank d : xs) = do
let next = dropWhile (not . isPrintable) xs
case next of
(x:_) | startsBlank x -> renderList xs
| otherwise -> renderList (normalize $ N.toList d) >> renderList xs
[] -> renderList xs
renderList (FBlankLines num : xs) = do
st <- get
case output st of
_ | newlines st > num -> return ()
| otherwise -> replicateM_ (1 + num - newlines st) newline
renderList xs
renderList (FCarriageReturn : xs) = do
st <- get
if newlines st > 0
then renderList xs
else do
newline
renderList xs
renderList (FNewLine : xs) = do
newline
renderList xs
renderList (FBreakingSpace : xs) = do
let isBreakingSpace FBreakingSpace = True
isBreakingSpace _ = False
let xs' = dropWhile isBreakingSpace xs
let next = takeWhile (not . isBreakable) xs'
st <- get
let off = foldl' (\tot t -> tot + offsetOf t) 0 next
case lineLength st of
Just l | column st + 1 + off > l -> newline
_ -> when (column st > 0) $ outp 1 " "
renderList xs'
renderList (FAfterBreak t : xs) = do
st <- get
if newlines st > 0
then renderList (toList t <> xs)
else renderList xs
-- FBlock and FVFill are all that's left
renderList (b : xs) = do
st <- get
let font = peekFont st
let (bs, rest) = span isBlock xs
-- ensure we have right padding unless end of line
let heightOf (FBlock _ ls) = length ls
heightOf _ = 1
let maxheight = maximum $ map heightOf (b:bs)
let toBlockSpec (FBlock w ls) = (w, ls)
toBlockSpec (FVFill w t) = (w, map (singleton . (Attr (linkTarget st) font)) (take maxheight $ repeat t))
toBlockSpec _ = (0, [])
let (_, lns') = foldl (mergeBlocks maxheight) (toBlockSpec b)
(map toBlockSpec bs)
let oldPref = prefix st
case column st - realLength oldPref of
n | n > 0 -> modify $ \s -> s{ prefix = oldPref <> T.replicate n " " }
_ -> return ()
renderList $ intersperse FCarriageReturn (mapMaybe cook lns')
modify $ \s -> s{ prefix = oldPref }
renderList rest
isBreakable :: HasChars a => FlatDoc a -> Bool
isBreakable FBreakingSpace = True
isBreakable FCarriageReturn = True
isBreakable FNewLine = True
isBreakable (FBlankLines _) = True
isBreakable _ = False
startsBlank' :: HasChars a => a -> Bool
startsBlank' t = fromMaybe False $ foldlChar go Nothing t
where
go Nothing c = Just (isSpace c)
go (Just b) _ = Just b
startsBlank :: HasChars a => FlatDoc a -> Bool
startsBlank (FText _ t) = startsBlank' t
startsBlank (FCookedText _ t) = startsBlank' t
startsBlank (FBlock n ls) = n > 0 && all startsBlank' ls
startsBlank (FVFill n t) = n > 0 && startsBlank' t
startsBlank (FBeforeNonBlank (x :| _)) = startsBlank x
startsBlank (FPrefixed _ (x :| _)) = startsBlank x
startsBlank (FFlush (x :| _)) = startsBlank x
startsBlank FBreakingSpace = True
startsBlank (FAfterBreak (t :| _)) = startsBlank t
startsBlank FCarriageReturn = True
startsBlank FNewLine = True
startsBlank (FBlankLines _) = True
startsBlank (FStyleOpen _) = True
startsBlank (FLinkOpen _) = True
startsBlank FStyleClose = True
startsBlank FLinkClose = True
isPrintable :: FlatDoc a -> Bool
isPrintable FLinkOpen{} = False
isPrintable FLinkClose{} = False
isPrintable FStyleOpen{} = False
isPrintable FStyleClose{} = False
isPrintable _ = True
isBlock :: FlatDoc a -> Bool
isBlock FBlock{} = True
isBlock FVFill{} = True
isBlock _ = False
offsetOf :: FlatDoc a -> Int
offsetOf (FText o _) = o
offsetOf (FBlock w _) = w
offsetOf (FVFill w _) = w
offsetOf (FCookedText w _) = w
offsetOf FBreakingSpace = 1
offsetOf _ = 0
-- | Create a 'Doc' from a stringlike value.
literal :: HasChars a => a -> Doc a
literal x =
mconcat $
intersperse NewLine $
map (\s -> if isNull s
then Empty
else let !len = realLength s
in Text len s) $
splitLines x
{-# NOINLINE literal #-}
cook :: HasChars a => Attributed a -> Maybe (FlatDoc a)
cook x | isNull x = Nothing
| otherwise = let !len = realLength x in Just (FCookedText len x)
-- | A literal string. (Like 'literal', but restricted to String.)
text :: HasChars a => String -> Doc a
text = literal . fromString
-- | A character.
char :: HasChars a => Char -> Doc a
char c = text $ fromString [c]
-- | A breaking (reflowable) space.
space :: Doc a
space = BreakingSpace
-- | A carriage return. Does nothing if we're at the beginning of
-- a line; otherwise inserts a newline.
cr :: Doc a
cr = CarriageReturn
-- | Inserts a blank line unless one exists already.
-- (@blankline <> blankline@ has the same effect as @blankline@.
blankline :: Doc a
blankline = BlankLines 1
-- | Inserts blank lines unless they exist already.
-- (@blanklines m <> blanklines n@ has the same effect as @blanklines (max m n)@.
blanklines :: Int -> Doc a
blanklines = BlankLines
-- | 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 :: IsString a => String -> Doc a -> Doc a
prefixed pref doc
| isEmpty doc = Empty
| otherwise = Prefixed (fromString pref) doc
-- | Makes a 'Doc' flush against the left margin.
flush :: Doc a -> Doc a
flush doc
| isEmpty doc = Empty
| otherwise = Flush doc
-- | Indents a 'Doc' by the specified number of spaces.
nest :: IsString a => Int -> Doc a -> Doc a
nest ind = prefixed (replicate ind ' ')
-- | A hanging indent. @hang ind start doc@ prints @start@,
-- then @doc@, leaving an indent of @ind@ spaces on every
-- line but the first.
hang :: IsString a => Int -> Doc a -> Doc a -> Doc a
hang ind start doc = start <> nest ind doc
-- | @beforeNonBlank d@ conditionally includes @d@ unless it is
-- followed by blank space.
beforeNonBlank :: Doc a -> Doc a
beforeNonBlank = BeforeNonBlank
-- | Makes a 'Doc' non-reflowable.
nowrap :: IsString a => Doc a -> Doc a
nowrap = mconcat . map replaceSpace . unfoldD
where replaceSpace BreakingSpace = Text 1 $ fromString " "
replaceSpace x = x
-- | Content to print only if it comes at the beginning of a line,
-- to be used e.g. for escaping line-initial `.` in roff man.
afterBreak :: Text -> Doc a
afterBreak = AfterBreak
-- | Returns the width of a 'Doc'.
offset :: (IsString a, HasChars a) => Doc a -> Int
offset = uncurry max . getOffset (const False) (0, 0)
-- | Returns the minimal width of a 'Doc' when reflowed at breakable spaces.
minOffset :: HasChars a => Doc a -> Int
minOffset = uncurry max . getOffset (> 0) (0,0)
-- l = longest, c = current
getOffset :: (IsString a, HasChars a)
=> (Int -> Bool) -> (Int, Int) -> Doc a -> (Int, Int)
getOffset breakWhen (!l, !c) x =
case x of
Text n _ -> (l, c + n)
Block n _ -> (l, c + n)
VFill n _ -> (l, c + n)
CookedText n _ -> (l, c + n)
Empty -> (l, c)
Styled _ d -> getOffset breakWhen (l, c) d
Linked _ d -> getOffset breakWhen (l, c) d
CarriageReturn -> (max l c, 0)
NewLine -> (max l c, 0)
BlankLines _ -> (max l c, 0)
Prefixed t d ->
let (l',c') = getOffset breakWhen (0, 0) d
in (max l (l' + realLength t), c' + realLength t)
BeforeNonBlank _ -> (l, c)
Flush d -> getOffset breakWhen (l, c) d
BreakingSpace
| breakWhen c -> (max l c, 0)
| otherwise -> (l, c + 1)
AfterBreak t -> if c == 0
then (l, c + realLength t)
else (l, c)
Concat (Concat d y) z ->
getOffset breakWhen (l, c) (Concat d (Concat y z))
Concat (BeforeNonBlank d) y ->
if isNonBlank y
then getOffset breakWhen (l, c) (Concat d y)
else getOffset breakWhen (l, c) y
Concat d y ->
let (l', c') = getOffset breakWhen (l, c) d
in getOffset breakWhen (l', c') y
isNonBlank :: Doc a -> Bool
isNonBlank (Text _ _) = True
isNonBlank (BeforeNonBlank d) = isNonBlank d
isNonBlank (Flush d) = isNonBlank d
isNonBlank (Concat d _) = isNonBlank d
isNonBlank _ = False
-- | Returns the column that would be occupied by the last
-- laid out character (assuming no wrapping).
updateColumn :: HasChars a => Doc a -> Int -> Int
updateColumn d k = snd . getOffset (const False) (0,k) $ d
-- | @lblock n d@ is a block of width @n@ characters, with
-- text derived from @d@ and aligned to the left.
lblock :: HasChars a => Int -> Doc a -> Doc a
lblock = block id
-- | Like 'lblock' but aligned to the right.
rblock :: HasChars a => Int -> Doc a -> Doc a
rblock w = block (\s -> replicateChar (w - realLength s) ' ' <> s) w
-- | Like 'lblock' but aligned centered.
cblock :: HasChars a => Int -> Doc a -> Doc a
cblock w = block (\s -> replicateChar ((w - realLength s) `div` 2) ' ' <> s) w
-- | Returns the height of a block or other 'Doc'.
height :: HasChars a => Doc a -> Int
height = length . splitLines . render Nothing
block :: HasChars a => (Attributed a -> Attributed a) -> Int -> Doc a -> Doc a
block filler width d
| width < 1 && not (isEmpty d) = block filler 1 d
| otherwise = Block width ls
where
preimage = prerender (Just width) d
reboxed = chop width preimage
ls = map filler reboxed
-- | An expandable border that, when placed next to a box,
-- expands to the height of the box. Strings cycle through the
-- list provided.
vfill :: HasChars a => a -> Doc a
vfill t = VFill (realLength t) t
chop :: HasChars a => Int -> a -> [a]
chop n =
concatMap chopLine . removeFinalEmpty . map addRealLength . splitLines
where
removeFinalEmpty xs = case lastMay xs of
Just (0, _) -> initSafe xs
_ -> xs
addRealLength l = (realLength l, l)
chopLine (len, l)
| len <= n = [l]
| otherwise = map snd $
foldrChar
(\c ls ->
let clen = charWidth c
cs = replicateChar 1 c
in case ls of
(len', l'):rest
| len' + clen > n ->
(clen, cs):(len', l'):rest
| otherwise ->
(len' + clen, cs <> l'):rest
[] -> [(clen, cs)]) [] l
-- | Encloses a 'Doc' inside a start and end 'Doc'.
inside :: Doc a -> Doc a -> Doc a -> Doc a
inside start end contents =
start <> contents <> end
-- | Puts a 'Doc' in curly braces.
braces :: HasChars a => Doc a -> Doc a
braces = inside (char '{') (char '}')
-- | Puts a 'Doc' in square brackets.
brackets :: HasChars a => Doc a -> Doc a
brackets = inside (char '[') (char ']')
-- | Puts a 'Doc' in parentheses.
parens :: HasChars a => Doc a -> Doc a
parens = inside (char '(') (char ')')
-- | Wraps a 'Doc' in single quotes.
quotes :: HasChars a => Doc a -> Doc a
quotes = inside (char '\'') (char '\'')
-- | Wraps a 'Doc' in double quotes.
doubleQuotes :: HasChars a => Doc a -> Doc a
doubleQuotes = inside (char '"') (char '"')
styled :: HasChars a => StyleReq -> Doc a -> Doc a
styled _ Empty = Empty
styled s x = Styled s x
-- | Puts a 'Doc' in boldface.
bold :: HasChars a => Doc a -> Doc a
bold = styled (RWeight Bold)
-- | Puts a 'Doc' in italics.
italic :: HasChars a => Doc a -> Doc a
italic = styled (RShape Italic)
-- | Underlines a 'Doc'.
underlined :: HasChars a => Doc a -> Doc a
underlined = styled (RUnderline ULSingle)
-- | Puts a line through a 'Doc'.
strikeout :: HasChars a => Doc a -> Doc a
strikeout = styled (RStrikeout Struck)
-- The Color type is here as an opaque alias to Color8 for the public interface
-- and there's trivial smart constructors for the individual colors to
-- hopefully allow for easier extension to supporting indexed and rgb colors in
-- the future, without dramatically changing the public API.
type Color = Color8
-- | Set foreground color.
fg :: HasChars a => Color -> Doc a -> Doc a
fg = styled . RForeground . FG
-- | Set background color.
bg :: HasChars a => Color -> Doc a -> Doc a
bg = styled . RBackground . BG
blue :: Color
blue = Blue
black :: Color
black = Black
red :: Color
red = Red
green :: Color
green = Green
yellow :: Color
yellow = Yellow
magenta :: Color
magenta = Magenta
cyan :: Color
cyan = Cyan
white :: Color
white = White
-- | Make Doc a hyperlink.
link :: HasChars a => Text -> Doc a -> Doc a
link = Linked
-- | Returns width of a character in a monospace font: 0 for a combining
-- character, 1 for a regular character, 2 for an East Asian wide character.
-- Ambiguous characters are treated as width 1.
charWidth :: Char -> Int
charWidth = extractLength . updateMatchStateNarrow (MatchState False 0 ' ' 0)
-- | Get real length of string, taking into account combining and double-wide
-- characters. Ambiguous characters are treated as width 1.
realLength :: HasChars a => a -> Int
realLength = realLengthNarrowContext
-- | Get the real length of a string, taking into account combining and
-- double-wide characters. Ambiguous characters are treated as width 1.
realLengthNarrowContext :: HasChars a => a -> Int
realLengthNarrowContext = realLengthWith updateMatchStateNarrow
-- | Get the real length of a string, taking into account combining and
-- double-wide characters. Ambiguous characters are treated as width 2.
realLengthWideContext :: HasChars a => a -> Int
realLengthWideContext = realLengthWith updateMatchStateWide
-- | Like 'realLengthNarrowContext', but avoids optimizations (shortcuts).
-- This is exposed for testing, to ensure that the optimizations are safe.
realLengthNarrowContextNoShortcut :: HasChars a => a -> Int
realLengthNarrowContextNoShortcut = realLengthWith updateMatchStateNoShortcut
-- | Like 'realLengthWideContext', but avoids optimizations (shortcuts).
-- This is exposed for testing, to ensure that the optimizations are safe.
realLengthWideContextNoShortcut :: HasChars a => a -> Int
realLengthWideContextNoShortcut = realLengthWith updateMatchStateNoShortcutWide
-- | Get real length of string, taking into account combining and double-wide
-- characters, using the given accumulator. This is exposed for testing.
realLengthWith :: HasChars a => (MatchState -> Char -> MatchState) -> a -> Int
realLengthWith f = extractLength . foldlChar f (MatchState True 0 ' ' 0)
-- | Update a 'MatchState' by processing a character.
-- For efficiency, we isolate commonly used portions of the basic
-- multilingual plane that do not have emoji in them.
-- This works in a narrow context.
updateMatchStateNarrow :: MatchState -> Char -> MatchState
updateMatchStateNarrow (MatchState firstChar tot _ tentative) !c
-- Control characters have width 0: friends don't let friends use tabs
| c <= '\x001F' = controlState
-- ASCII
| c <= '\x007E' = narrowState
-- More control characters
| c <= '\x009F' = controlState
-- Extended Latin: Latin 1-supplement, Extended-A, Extended-B, IPA Extensions.
-- This block is full of ambiguous characters, so these shortcuts will not
-- work in a wide context.
| c == '\x00AD' = controlState -- Soft hyphen
| c <= '\x02FF' = narrowState
-- Combining diacritical marks used in Latin and other scripts
| c <= '\x036F' = combiningState
-- Han ideographs
| c >= '\x3250' && c <= '\xA4CF' =
if | c <= '\x4DBF' -> wideState -- Han ideographs
| c <= '\x4DFF' -> narrowState -- Hexagrams
| otherwise -> wideState -- More Han ideographs
-- Arabic
| c >= '\x0600' && c <= '\x06FF' =
if | c <= '\x0605' -> controlState -- Number marks
| c <= '\x060F' -> narrowState -- Punctuation and marks
| c <= '\x061A' -> combiningState -- Combining marks
| c == '\x061B' -> narrowState -- Arabic semicolon
| c <= '\x061C' -> controlState -- Letter mark
| c <= '\x064A' -> narrowState -- Main Arabic abjad
| c <= '\x065F' -> combiningState -- Arabic vowel markers
| c == '\x0670' -> combiningState -- Superscript alef
| c <= '\x06D5' -> narrowState -- Arabic digits and letters used in other languages
| c <= '\x06DC' -> combiningState -- Small high ligatures
| c == '\x06DD' -> controlState -- End of ayah
| c == '\x06DE' -> narrowState -- Start of rub el hizb
| c <= '\x06E4' -> combiningState -- More small high ligatures
| c <= '\x06E6' -> narrowState -- Small vowels
| c == '\x06E9' -> narrowState -- Place of sajdah
| c <= '\x06ED' -> combiningState -- More combining