From 95737cbd3bde394a9fc65dc0a2979f85a98d119a Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Wed, 3 May 2023 16:10:40 +1200 Subject: [PATCH] Add speak-as support in counter-styles, some codestyle improvements. --- src/Data/CSS/Preprocessor/Text.hs | 55 +++++++++++++++++-------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Data/CSS/Preprocessor/Text.hs b/src/Data/CSS/Preprocessor/Text.hs index 7b3eea1..b2da5e5 100644 --- a/src/Data/CSS/Preprocessor/Text.hs +++ b/src/Data/CSS/Preprocessor/Text.hs @@ -11,7 +11,7 @@ import qualified Data.Text as Txt import Data.Text (Text) import Data.CSS.Preprocessor.Text.CounterStyle (parseCounter, counterRender, CounterStore'(..), decimalCounter, - defaultCounterStore, CounterStore, CounterStyle(..)) + defaultCounterStore, CounterStore, CounterStyle(..), speakAs') import Data.Maybe (fromMaybe, isJust) import qualified Data.HashMap.Lazy as M @@ -231,19 +231,21 @@ insertPseudos :: PropertyParser p => CounterStore -> StyleTree (TextStyle p) -> StyleTree (TextStyle p) insertPseudos s (StyleTree self@TextStyle { afterPseudo = Just child } childs) = insertPseudos s $ - StyleTree self { afterPseudo = Nothing } (childs ++ [t child]) + StyleTree self { afterPseudo = Nothing } (childs ++ [t Nothing child]) insertPseudos s (StyleTree self@TextStyle { beforePseudo = Just child } childs) = - insertPseudos s $ StyleTree self { beforePseudo = Nothing } (t child:childs) + insertPseudos s $ StyleTree self { beforePseudo = Nothing } + (t Nothing child:childs) insertPseudos s (StyleTree self@TextStyle { markerPseudo = Nothing } childs) = insertPseudos s $ StyleTree self { markerPseudo = Just temp } childs insertPseudos s self@(StyleTree TextStyle { markerPseudo = Just child} _) | Just text <- lookup "content" $ counterProps child = - addBullet self s text + addBullet self s Nothing text insertPseudos s self@(StyleTree TextStyle { listStyleImage = bullet@(_:_) } _) = - addBullet self s (bullet ++ [String " "]) + addBullet self s Nothing (bullet ++ [String " "]) insertPseudos s self@(StyleTree TextStyle { listStyleType = bullet@(_:_) } _) - | Just (cstyle, _) <- parseCounter s bullet = addBullet self s $ text cstyle + | Just (cstyle, _) <- parseCounter s bullet = + addBullet self s (Just cstyle) $ text cstyle where text counter = String (prefix counter): Function "counter":Ident "list-item":Comma:bullet ++ @@ -252,51 +254,54 @@ insertPseudos s self@(StyleTree TextStyle { listStyleType = bullet@(_:_) } _) insertPseudos store (StyleTree self childs) = StyleTree self $ map (insertPseudos store) childs -addBullet :: PropertyParser p => - StyleTree (TextStyle p) -> CounterStore -> [Token] -> StyleTree (TextStyle p) +addBullet :: PropertyParser p => StyleTree (TextStyle p) -> + CounterStore -> Maybe CounterStyle -> [Token] -> StyleTree (TextStyle p) addBullet (StyleTree self@TextStyle { isListItem = True, listPosInside = True, markerPseudo = Just child - } childs) store txt = insertPseudos store $ - StyleTree self { isListItem = False } (t child { + } childs) store cstyle txt = insertPseudos store $ + StyleTree self { isListItem = False } (t cstyle child { counterProps = insertList "content" txt $ counterProps child } : childs) -addBullet (StyleTree self@TextStyle { markerIsRight = Nothing } childs) store txt - = addBullet (StyleTree self { markerIsRight = Just $ isRTL self } childs) - store txt +addBullet (StyleTree s@TextStyle {markerIsRight=Nothing} childs) store cstyle txt + = addBullet (StyleTree s { markerIsRight = Just $ isRTL s } childs) + store cstyle txt addBullet (StyleTree self@TextStyle { markerIsRight = Just False, isListItem = True, listPosInside = False, markerPseudo = Just child - } childs) store txt = insertPseudos store $ + } childs) store cstyle txt = insertPseudos store $ StyleTree self { isListItem = False, -- Flex lays out children horizontally at min size. counterProps=insertList "display" [Ident "flex"] $ counterProps child } [ - t child { - counterProps= insertList "content" txt $ counterProps child + t cstyle child { + counterProps = insertList "content" txt $ counterProps child }, -- Generate a new layout box for the bullet to sit outside of. StyleTree temp childs ] addBullet (StyleTree self@TextStyle { markerIsRight = Just True, isListItem = True, listPosInside = False, markerPseudo = Just child - } childs) store txt = insertPseudos store $ + } childs) store cstyle txt = insertPseudos store $ StyleTree self { isListItem = False, counterProps=insertList "display" [Ident "flex"] $ counterProps child } [ StyleTree temp childs, - t child { - counterProps= insertList "content" txt $ counterProps child + t cstyle child { + counterProps = insertList "content" txt $ counterProps child } ] -addBullet (StyleTree self childs) store _ = +addBullet (StyleTree self childs) store _ _ = insertPseudos store $ StyleTree self { isListItem = False, listStyleImage = [], listStyleType = [] } childs -t :: p -> StyleTree p -t = flip StyleTree [] +t :: Maybe CounterStyle -> TextStyle p -> StyleTree (TextStyle p) +t (Just cstyle) self = StyleTree self { + counterProps = insertList "speak-as" [Ident $ speakAs' cstyle] $ + counterProps self } [] +t Nothing self = StyleTree self [] -------- ---- Counters @@ -309,7 +314,8 @@ inheritCounters counterSource valueSource = M.unionWith cb valueSource counterSo path `elem` [p | (p, _) <- source]] instantiateCounter :: Context -> Path -> Text -> Int -> Context -instantiateCounter counters path name val = M.insertWith appendCounter name [(path, val)] counters +instantiateCounter counters path name val = + M.insertWith appendCounter name [(path, val)] counters where appendCounter new (old@((_:oldPath), _):olds) | oldPath == tail path = new ++ olds @@ -321,7 +327,8 @@ instantiateCounters path instruct counters = foldl cb counters instruct where cb counters' (name, value) = instantiateCounter counters' path name value incrementCounter :: Context -> Path -> Text -> Int -> Context -incrementCounter counters path name val = M.insertWith addCounter name [(path, val)] counters +incrementCounter counters path name val = + M.insertWith addCounter name [(path, val)] counters where addCounter ((_, new):_) ((path', old):rest) = (path', new + old):rest addCounter [] old = old -- 2.30.2