~alcinnz/haskell-stylist

216e4e39846280415775a5511e4755b94caf257e — Adrian Cochrane 4 years ago 8ffc0dd
Parse & prepare to handle white-space: pre;
1 files changed, 37 insertions(+), 10 deletions(-)

M src/Data/CSS/Preprocessor/Text.hs
M src/Data/CSS/Preprocessor/Text.hs => src/Data/CSS/Preprocessor/Text.hs +37 -10
@@ 20,7 20,10 @@ data TextStyle p = TextStyle {

    counterReset :: Counters,
    counterIncrement :: Counters,
    counterSet :: Counters
    counterSet :: Counters,

    whiteSpaceCollapse :: Bool,
    newlineCollapse :: Bool
}

instance PropertyParser p => PropertyParser (TextStyle p) where


@@ 29,26 32,41 @@ instance PropertyParser p => PropertyParser (TextStyle p) where
            content = [],
            counterReset = [],
            counterIncrement = [],
            counterSet = []
            counterSet = [],
            whiteSpaceCollapse = True,
            newlineCollapse = True
        }
    inherit parent = TextStyle {
            inner = inherit $ inner parent,
            content = [],
            counterReset = [],
            counterIncrement = [],
            counterSet = []
            counterSet = [],
            whiteSpaceCollapse = whiteSpaceCollapse parent,
            newlineCollapse = newlineCollapse parent
        }

    shorthand self "content" value = shorthand (inner self) "content" $ removeCounters value
    shorthand _ key value
        | key `elem` ["counter-reset", "counter-increment", "counter-set"],
            Just _ <- parseCounters 0 value = [(key, value)]
    shorthand self "white-space" [Ident val]
        | val `elem` ["normal", "pre", "pre-wrap", "pre-line"] = [("white-space", [Ident val])]
        | otherwise = shorthand (inner self) "white-space" [Ident val]
    shorthand self key value = shorthand (inner self) key value

    longhand _ self "content" value = Just $ self {content = value}
    longhand _ self "counter-reset" value = (\v -> self {counterReset = v}) <$> parseCounters 0 value
    longhand _ self "counter-increment" value = (\v -> self {counterIncrement = v}) <$> parseCounters 1 value
    longhand _ self "counter-set" value = (\v -> self {counterSet = v}) <$> parseCounters 0 value

    longhand p self "white-space" [Ident "initial"] = setWhiteSpace p self True True "normal"
    longhand p self "white-space" [Ident "normal"] = setWhiteSpace p self True True "normal"
    longhand p self "white-space" [Ident "pre"] = setWhiteSpace p self False False "nowrap"
    longhand p self "white-space" [Ident "nowrap"] = setWhiteSpace p self True True "nowrap"
    longhand p self "white-space" [Ident "pre-wrap"] = setWhiteSpace p self False False "normal"
    longhand p self "white-space" [Ident "pre-line"] = setWhiteSpace p self True False "normal"

    longhand _ self "content" value = Just $ self {content = value}
    longhand parent self key value = (\v -> self {inner = v}) <$> longhand (inner parent ) (inner self) key value

removeCounters :: [Token] -> [Token]


@@ 57,8 75,15 @@ removeCounters (Function "counters":Ident _:Comma:String _:toks) = String "" : r
removeCounters (tok:toks) = tok : removeCounters toks
removeCounters [] = []

setWhiteSpace :: PropertyParser p => TextStyle p -> TextStyle p -> Bool -> Bool -> Text -> Maybe (TextStyle p)
setWhiteSpace parent self collapse noNewlines lowered = Just $ self {
        inner = inner self `fromMaybe` longhand (inner parent) (inner self) "white-space" [Ident lowered],
        whiteSpaceCollapse = collapse,
        newlineCollapse = noNewlines
    }
parseCounters :: Integer -> [Token] -> Maybe [(Text, Integer)]
parseCounters _ [Ident "none"] = Just []
parseCounters _ [Ident "initial"] = Just []
parseCounters _ [] = Just []
parseCounters x (Ident counter : Number _ (NVInteger count') : toks) =
    (:) (counter, count') <$> parseCounters x toks


@@ 66,7 91,11 @@ parseCounters x (Ident counter : toks) = (:) (counter, x) <$> parseCounters x to
parseCounters _ _ = Nothing

resolve :: PropertyParser p => StyleTree (TextStyle p) -> StyleTree p
resolve = applyCounters
resolve = resolve' . applyCounters
resolve' :: PropertyParser p => StyleTree (TextStyle p) -> StyleTree p
resolve' = treeMap $ \TextStyle {inner = inner', content = content'} -> (
        fromMaybe inner' $ longhand temp inner' "content" content'
    )

--------
---- Counters


@@ 123,14 152,12 @@ renderCounters counters (Function "counters":Ident name:Comma:String sep:RightPa
renderCounters counters (tok:toks) = tok : renderCounters counters toks
renderCounters _ [] = []

applyCounters :: PropertyParser p => StyleTree (TextStyle p) -> StyleTree p
applyCounters :: StyleTree (TextStyle p) -> StyleTree (TextStyle p)
applyCounters = treeOrder applyCounters0 M.empty
applyCounters0 :: PropertyParser p => Context -> Context -> Path -> TextStyle p -> (Context, p)
applyCounters0 :: Context -> Context -> Path -> TextStyle p -> (Context, TextStyle p)
applyCounters0 counterSource valueSource path node =
    let counters = inheritCounters counterSource valueSource &
            instantiateCounters path (counterReset node) &
            incrementCounters path (counterIncrement node) &
            setCounters path (counterSet node)
    in let inner' = inner node
    in (counters,
        fromMaybe inner' $ longhand temp inner' "content" $ renderCounters counters $ content node)
    in (counters, node {content = renderCounters counters $ content node})