From 5a8c31843e878dac2ad906a95ab2d564e62fae1c Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Thu, 27 Apr 2023 10:39:37 +1200 Subject: [PATCH] Simplify counterstyle API & implementation. --- .../CSS/Preprocessor/Text/CounterStyle.hs | 40 +++++++++---------- test/Test.hs | 8 ++-- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/Data/CSS/Preprocessor/Text/CounterStyle.hs b/src/Data/CSS/Preprocessor/Text/CounterStyle.hs index f432d28..c0d8bdd 100644 --- a/src/Data/CSS/Preprocessor/Text/CounterStyle.hs +++ b/src/Data/CSS/Preprocessor/Text/CounterStyle.hs @@ -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)] diff --git a/test/Test.hs b/test/Test.hs index 529db88..35d7042 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -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 -- 2.30.2