~alcinnz/haskell-stylist

5a8c31843e878dac2ad906a95ab2d564e62fae1c — Adrian Cochrane 1 year, 6 months ago fb3315b
Simplify counterstyle API & implementation.
2 files changed, 22 insertions(+), 26 deletions(-)

M src/Data/CSS/Preprocessor/Text/CounterStyle.hs
M test/Test.hs
M src/Data/CSS/Preprocessor/Text/CounterStyle.hs => src/Data/CSS/Preprocessor/Text/CounterStyle.hs +18 -22
@@ 297,33 297,29 @@ enumerate :: [a] -> [(Int, a)]
enumerate = zip $ enumFrom 0

counterRenderMarker :: CounterStyle -> Int -> Text
counterRenderMarker = counterRender True
counterRender :: Bool -> CounterStyle -> Int -> Text
counterRender isMarker self x
    | isMarker = Txt.concat [prefix self, inner self x, suffix self]
    | otherwise = inner self x
counterRenderMarker self x =
    Txt.concat [prefix self, counterRender self x, suffix self]
counterRender :: CounterStyle -> Int -> Text
counterRender self@CounterStyle { fallback = Just self' } x
    | not $ inRange x $ ranges' self = counterRender self' x
    | counterRenderCore self x == fallbackSym = counterRender self' x
  where
    inner :: CounterStyle -> Int -> Text
    inner this@CounterStyle { fallback = Just self' } x'
        | not $ inRange x $ ranges' this = inner self' x'
        | counterRenderCore this x == fallbackSym = inner self' x'
    inner self' x'
        | x' < 0 = Txt.concat [
            negativePrefix self',
            inner self' { fallback = Nothing } $ -x',
            negativeSuffix self'
          ]
        | text == fallbackSym = Txt.pack $ show x' -- NOTE: Shouldn't happen
        | n < padLength self' =
            Txt.replicate (padLength self' - n) (padChar self') `Txt.append` text
        | otherwise = text
      where
        text = counterRenderCore self' x'
        n = Txt.length text
    inRange y ((start, end):rest)
        | y >= start && y <= end = True
        | otherwise = inRange y rest
    inRange _ [] = False
counterRender self@CounterStyle { padLength = m, padChar = pad } x
    | x < 0 = Txt.concat [
        negativePrefix self,
        counterRender self { ranges = Just [(0, maxBound)] } $ -x, -- No fallback!
        negativeSuffix self
      ]
    | text == fallbackSym = Txt.pack $ show x -- NOTE: Shouldn't happen
    | n < m = Txt.replicate (m - n) pad `Txt.append` text
    | otherwise = text
  where
    text = counterRenderCore self x
    n = Txt.length text

infiniteRange :: [(Int, Int)]
infiniteRange = [(minBound, maxBound)]

M test/Test.hs => test/Test.hs +4 -4
@@ 588,7 588,7 @@ spec = do
            Ctr.counterRenderMarker counter 12 `shouldBe` "⚅⚅ "
            Ctr.counterRenderMarker counter 13 `shouldBe` "⚅⚅⚀ "
        it "Handles Chinese-specific numbering systems" $ do
            let counterRender = Ctr.counterRender False Ctr.simpChineseInformal
            let counterRender = Ctr.counterRender Ctr.simpChineseInformal
            counterRender 1 `shouldBe` "一"
            counterRender 2 `shouldBe` "二"
            counterRender 3 `shouldBe` "三"


@@ 710,9 710,9 @@ spec = do
            counterRender 119 `shouldBe` "一百一十九"
            counterRender 120 `shouldBe` "一百二十"
        it "Handles ethiopian numbering system" $ do
            Ctr.counterRender False Ctr.ethiopic 100 `shouldBe` "፻"
            Ctr.counterRender False Ctr.ethiopic 78010092 `shouldBe` "፸፰፻፩፼፺፪"
            Ctr.counterRender False Ctr.ethiopic 780100000092 `shouldBe` "፸፰፻፩፼፼፺፪"
            Ctr.counterRender Ctr.ethiopic 100 `shouldBe` "፻"
            Ctr.counterRender Ctr.ethiopic 78010092 `shouldBe` "፸፰፻፩፼፺፪"
            Ctr.counterRender Ctr.ethiopic 780100000092 `shouldBe` "፸፰፻፩፼፼፺፪"

styleIndex :: StyleIndex
styleIndex = new