~alcinnz/haskell-stylist

8f545827645a4969dbf88056251b04cd9fc1d687 — Adrian Cochrane 5 years ago 4a1689d
Implement @document rules (save regexp() conditions).

I've still got to select a regexp library to power it.
3 files changed, 49 insertions(+), 2 deletions(-)

A src/Data/CSS/Preprocessor/Conditions.hs
M src/Data/CSS/Syntax/StyleSheet.hs
M stylist.cabal
A src/Data/CSS/Preprocessor/Conditions.hs => src/Data/CSS/Preprocessor/Conditions.hs +44 -0
@@ 0,0 1,44 @@
{-# LANGUAGE OverloadedStrings #-}
module Data.CSS.Preprocessor.Conditions(
        ConditionalStyles(..)
    ) where

import Data.CSS.Syntax.StyleSheet
import Data.CSS.Syntax.Tokens(Token(..))

import Data.Text (unpack)
import Network.URI (URI(..), URIAuth(..))

data ConditionalStyles s = ConditionalStyles {
    hostURL :: URI,
    mediaDocument :: String,
    inner :: s
}

hostUrlS = show . hostURL

parseAtBlock self toks = let (block, toks') = scanBlock toks in (parse' self block, toks')

instance StyleSheet ConditionalStyles where
    setPriority x self = self {inner = setPriority x $ inner self}
    addRule self rule = self {inner = addRule (inner self) rule}

    addAtRule self "document" (Comma:toks) = addAtRule self "document" toks
    addAtRule self "document" (Url match:toks) | unpack match == hostUrlS self =
        addAtRule self "document" toks
    addAtRule self "document" (Function "url-prefix":String match:RightParen:toks) =
        | unpack match `isPrefixOf` hostUrlS self = addAtRule self "document" toks
    addAtRule self "document" (Function "domain":String match:RightParen:toks)
        | unpack match == domain || ('.':unpack match) `isSuffixOf` domain =
            addAtRule self "document" toks
        where
            domain | Just auth <- uriAuthority $ hostURL self = uriRegName auth
                | otherwise = ""
    addAtRule self "document" (Function "media-document":String match:RightParen:toks) =
        | unpack match == mediaDocument self = addAtRule self "document" toks
    -- TODO Support regexp() conditions, requires new dependency
    addAtRule self "document" (LeftCurlyBracket:toks) = parseAtBlock self toks
    addAtRule self "document" tokens = (self, skipAtRule tokens)

    addAtRule self rule tokens =
        let (self', tokens') = addAtRule (inner self) rule tokens in (self {inner = self'}, tokens')

M src/Data/CSS/Syntax/StyleSheet.hs => src/Data/CSS/Syntax/StyleSheet.hs +1 -1
@@ 1,7 1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Data.CSS.Syntax.StyleSheet (
        parse, parse', parseForURL, TrivialStyleSheet(..),
        StyleSheet(..), skipAtRule,
        StyleSheet(..), skipAtRule, scanBlock,
        StyleRule(..),
        -- For parsing at-rules, HTML "style" attribute, etc.
        parseProperties, parseProperties',

M stylist.cabal => stylist.cabal +4 -1
@@ 47,10 47,13 @@ extra-source-files:  ChangeLog.md, README.md
-- Constraint on the version of Cabal needed to build this package.
cabal-version:       >=1.10

source-repository head
    type: git
    location: https://git.nzoss.org.nz/alcinnz/stylish-haskell.git

library
  -- Modules exported by the library.
  exposed-modules:     Data.CSS.Syntax.StyleSheet, Data.CSS.Syntax.Selector, Data.CSS.Style
  exposed-modules:     Data.CSS.Syntax.StyleSheet, Data.CSS.Syntax.Selector, Data.CSS.Style, Data.CSS.Preprocessor.Conditions
  
  -- Modules included in this library but not exported.
  other-modules:       Data.CSS.Syntax.StylishUtil,