~jaro/balkon

ref: 0f5dfa9a2b6865dc46ef926983c64b2ab11fa3d1 balkon/test/Data/Text/ParagraphLayout/ParagraphConstruction.hs -rw-r--r-- 2.1 KiB
0f5dfa9a β€” Jaro Use infix operators for constructing test data. 1 year, 6 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
61
62
63
64
-- | Infix operators for readable construction of paragraphs as testing input.
--
-- Example construction:
-- @"ignored prefix" |< "en"~"one two " >|< "ja"~"δΈ‰ε››" >| "ignored suffix"@
--
-- Special syntax for paragraphs with no contents:
-- @"ignored prefix" |<>| "ignored suffix"@
--
-- Please note that this form of construction is inefficient for longer text.
module Data.Text.ParagraphLayout.ParagraphConstruction
    ((>|)
    ,(>|<)
    ,(|<)
    ,(|<>|)
    ,(~)
    )
where

import Data.Text (append, pack)
import Data.Text.Foreign (lengthWord8)
import Data.Text.Internal (Text(Text))
import Data.Text.ParagraphLayout.Plain
    (Paragraph(Paragraph)
    ,ParagraphOptions()
    ,Span(Span)
    )

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

-- Create next span.
infixr 5 >|<
(>|<) :: (String, String) -> (Text, [Span]) -> (Text, [Span])
(spanText, spanLanguage) >|< (oldText, oldSpans) = (newText, newSpans)
    where
        newSpans = Span (fromIntegral $ lengthWord8 packedText) spanLanguage : oldSpans
        newText = append packedText oldText
        packedText = pack spanText

-- Add optional ignored prefix and wrap in a `Paragraph`.
infixr 5 |<
(|<) :: String -> (Text, [Span]) -> ParagraphOptions -> Paragraph
ignoredPrefix |< (oldText, spans) = Paragraph arr (fromIntegral off) spans
    where
        (Text arr offPrefix _) = append packedPrefix oldText
        off = offPrefix + (lengthWord8 packedPrefix)
        packedPrefix = pack ignoredPrefix

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

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