~jaro/balkon

cfc868c1a40565206517ffe83f7cbb5ed8e41441 — Jaro 1 year, 1 month ago 024cdda
Expose text slicing functions.
M CHANGELOG.md => CHANGELOG.md +3 -0
@@ 15,6 15,9 @@

    * Explicit bidirectional formatting characters are ignored.

* Exposed functions to allow verification of correct input text slicing:
  `paragraphSpanBounds`, `paragraphSpanTexts`, and `paragraphText`.

## 0.2.1.0 -- 2023-04-04

* Added pagination.

M lib/Data/Text/ParagraphLayout.hs => lib/Data/Text/ParagraphLayout.hs +3 -0
@@ 36,6 36,9 @@ module Data.Text.ParagraphLayout
    , SpanOptions (SpanOptions, spanLanguage)
    , layoutPlain
    , paginate
    , paragraphSpanBounds
    , paragraphSpanTexts
    , paragraphText
    )
where


M src/Data/Text/ParagraphLayout/Internal/Paragraph.hs => src/Data/Text/ParagraphLayout/Internal/Paragraph.hs +9 -0
@@ 80,12 80,18 @@ data ParagraphOptions = ParagraphOptions
-- | Calculate the offsets into the `Paragraph`'s underlying `Data.Text.Array`
-- where each span starts and ends, in ascending order. The resulting list
-- will be one larger than the list of input spans.
--
-- You can use this function to verify that Balkón will slice the input text
-- correctly.
paragraphSpanBounds :: Paragraph -> NonEmpty Int
paragraphSpanBounds (Paragraph _ initialOffset spans _) =
    -- TODO: Consider adding checks for array bounds.
    NonEmpty.scanl (+) initialOffset (map spanLength spans)

-- | Turn each span of the input `Paragraph` into a `Text`.
--
-- You can use this function to verify that Balkón will slice the input text
-- correctly.
paragraphSpanTexts :: Paragraph -> [Text]
paragraphSpanTexts p@(Paragraph arr _ _ _) = zipWith toText sStarts sEnds
    where


@@ 95,6 101,9 @@ paragraphSpanTexts p@(Paragraph arr _ _ _) = zipWith toText sStarts sEnds
        sBounds = paragraphSpanBounds p

-- | Turn all spans of the input `Paragraph` into one combined `Text`.
--
-- You can use this function to verify that Balkón will slice the input text
-- correctly.
paragraphText :: Paragraph -> Text
paragraphText p@(Paragraph arr _ _ _) = Text arr start (end - start)
    where