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.ParagraphOptions
import Data.Text.ParagraphLayout.Internal.Plain.Paragraph
import Data.Text.ParagraphLayout.Internal.Span (Span (Span), SpanOptions)
-- | Create first span with optional ignored suffix.
infixr 5 >|
(>|) :: (SpanOptions, 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 >|<
(>|<) :: (SpanOptions, 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, [])