~jaro/balkon

ref: 18e994081f0919013b51e3d73cf58022fed3ee6c balkon/src/Data/Text/ParagraphLayout/Internal/ParagraphConstruction.hs -rw-r--r-- 2.0 KiB
18e99408Jaro Expose ParagraphConstruction. 1 year, 8 months ago
                                                                                
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
module Data.Text.ParagraphLayout.Internal.ParagraphConstruction
    ((>|)
    ,(>|<)
    ,(|<)
    ,(|<>|)
    ,(~)
    )
where

import Data.Text (pack)
import Data.Text.Foreign (lengthWord8)
import Data.Text.Internal (Text(Text))
import Data.Text.Internal.Lazy (chunk, empty)
import qualified Data.Text.Internal.Lazy as Lazy
import Data.Text.Lazy (toStrict)
import Data.Text.ParagraphLayout.Internal.Plain
    (Paragraph(Paragraph)
    ,ParagraphOptions
    )
import Data.Text.ParagraphLayout.Internal.Span (Span(Span))

-- | Create first span with optional ignored suffix.
infixr 5 >|
(>|) :: (String, String) -> String -> (Lazy.Text, [Span])
(spanLanguage, spanText) >| suffix = (newText, newSpans)
    where
        newSpans = [newSpan]
        newSpan = Span (lengthWord8 packedSpanText) spanLanguage
        newText = chunk packedSpanText (chunk packedSuffix empty)
        packedSpanText = pack spanText
        packedSuffix = pack suffix

-- | Create next span.
infixr 5 >|<
(>|<) :: (String, String) -> (Lazy.Text, [Span]) -> (Lazy.Text, [Span])
(spanLanguage, spanText) >|< (oldText, oldSpans) = (newText, newSpans)
    where
        newSpans = newSpan:oldSpans
        newSpan = Span (lengthWord8 packedSpanText) spanLanguage
        newText = chunk packedSpanText oldText
        packedSpanText = pack spanText

-- | Add optional ignored prefix and wrap in a `Paragraph`.
infixr 5 |<
(|<) :: String -> (Lazy.Text, [Span]) -> ParagraphOptions -> Paragraph
prefix |< (oldText, spans) = Paragraph arr afterPrefix spans
    where
        (Text arr beforePrefix _) = toStrict $ chunk packedPrefix oldText
        afterPrefix = beforePrefix + lengthWord8 packedPrefix
        packedPrefix = pack prefix

-- | Create a `Paragraph` with no spans, just two ignored texts.
infixr 5 |<>|
(|<>|) :: String -> String -> ParagraphOptions -> Paragraph
prefix |<>| suffix = prefix |< (chunk (pack suffix) empty, [])

-- | Combine language with text.
infix 6 ~
(~) :: String -> String -> (String, String)
lang ~ txt = (lang, txt)