~alcinnz/haskell-stylist

41adcf9dadc941ca71322a1c4043a26e912e9d53 — Adrian Cochrane 4 years ago 950b7f7
Introduce simplified html2css API

Which, now that these are implemented, lowers <style> to @media & <link rel=stylesheet> to @import.
M src/Data/CSS/Preprocessor/Conditions.hs => src/Data/CSS/Preprocessor/Conditions.hs +1 -1
@@ 1,6 1,6 @@
{-# LANGUAGE OverloadedStrings #-}
module Data.CSS.Preprocessor.Conditions(
        ConditionalStyles(..), conditionalStyles,
        ConditionalStyles(..), conditionalStyles, ConditionalRule(..),
        extractImports, resolveImports, loadImports, resolve,
        Datum(..)
    ) where

M stylist.cabal => stylist.cabal +5 -6
@@ 10,7 10,7 @@ name:                stylist
-- PVP summary:      +-+------- breaking API changes
--                   | | +----- non-breaking API additions
--                   | | | +--- code changes with no API change
version:             1.1.0.0
version:             1.1.1.0

-- A short (one-line) description of the package.
synopsis:            Apply CSS styles to a document tree.


@@ 53,19 53,18 @@ source-repository head

library
  -- Modules exported by the library.
  exposed-modules:     Data.CSS.Syntax.StyleSheet, Data.CSS.Syntax.Selector, Data.CSS.Style, Data.CSS.Preprocessor.Conditions
  exposed-modules:     Data.CSS.Syntax.StyleSheet, Data.CSS.Syntax.Selector, Data.CSS.Style, Data.CSS.Preprocessor.Conditions, Data.CSS.Preprocessor.Conditions.Expr
  
  -- Modules included in this library but not exported.
  other-modules:       Data.CSS.Syntax.StylishUtil,
                       Data.CSS.Style.Importance, Data.CSS.Style.Common, Data.CSS.Style.Cascade,
                       Data.CSS.Style.Selector.Index, Data.CSS.Style.Selector.Interpret, Data.CSS.Style.Selector.Specificity,
                       Data.CSS.Preprocessor.Conditions.Expr
                       Data.CSS.Style.Selector.Index, Data.CSS.Style.Selector.Interpret, Data.CSS.Style.Selector.Specificity
  
  -- LANGUAGE extensions used by modules in this package.
  -- other-extensions:    
  
  -- Other library packages from which modules are imported.
  build-depends:       base >=4.9 && <4.10, css-syntax >=0.1 && <0.2, text,
  build-depends:       base >=4.9 && <=4.12, css-syntax >=0.1 && <0.2, text,
                        unordered-containers >= 0.2 && <0.3, hashable,
                        network-uri >= 2.6 && <2.7
  


@@ 83,7 82,7 @@ test-suite test-stylist
  type:     exitcode-stdio-1.0
  main-is:              Test.hs
  other-modules:        Data.CSS.Syntax.StyleSheet, Data.CSS.Syntax.Selector, Data.CSS.Style
  build-depends:       base >=4.9 && <4.10, css-syntax >=0.1 && <0.2, text,
  build-depends:       base >=4.9 && <=4.12, css-syntax >=0.1 && <0.2, text,
                        unordered-containers >= 0.2 && <0.3, hashable,
                        network-uri >= 2.6 && <2.7, hspec, QuickCheck,
                        scientific >= 0.3 && <1.0

M xml-conduit-stylist/src/Data/HTML2CSS.hs => xml-conduit-stylist/src/Data/HTML2CSS.hs +29 -3
@@ 1,8 1,8 @@
{-# LANGUAGE OverloadedStrings #-}
module Data.HTML2CSS(
        externalStyles, externalStylesForURL, internalStyles, internalStylesForURL,
        cssPriorityAgent, cssPriorityUser, cssPriorityAuthor,
        traverseStyles, traversePrepopulatedStyles, traverseStyles', elToStylish
        externalStyles, externalStylesForURL, internalStyles, internalStylesForURL, -- legacy
        html2css, cssPriorityAgent, cssPriorityUser, cssPriorityAuthor, -- parsing
        traverseStyles, traversePrepopulatedStyles, traverseStyles', elToStylish -- application
    ) where

import qualified Data.List as L


@@ 15,6 15,8 @@ import qualified Text.XML as XML
import Data.CSS.Syntax.StyleSheet
import Data.CSS.Style
import Data.CSS.Syntax.Tokens (tokenize)
import Data.CSS.Preprocessor.Conditions
import qualified Data.CSS.Preprocessor.Conditions.Expr as Query

import Network.URI



@@ 25,6 27,30 @@ cssPriorityUser = setPriority 2
cssPriorityAuthor = setPriority 3

---- Parsing
html2css :: PropertyParser p => XML.Document -> URI -> ConditionalStyles p
html2css xml url = ConditionalStyles {
    hostURL = url,
    mediaDocument = "document",
    rules = Priority 3 : html2css' (XML.documentRoot xml) (conditionalStyles url "document"),
    propertyParser = temp
}

html2css' :: PropertyParser p => XML.Element -> ConditionalStyles p -> [ConditionalRule p]
html2css' (XML.Element (XML.Name "style" _ _) attrs children) base =
    [Internal (parseMediaQuery attrs) (parseForURL base (hostURL base) $ strContent children)]
html2css' (XML.Element (XML.Name "link" _ _) attrs _) base
    | Just link <- "href" `M.lookup` attrs,
        Just "stylesheet" <- "rel" `M.lookup` attrs,
        Just uri <- parseURIReference $ Txt.unpack link =
            [External (parseMediaQuery attrs) (relativeTo uri $ hostURL base)]
html2css' (XML.Element _ _ children) base = concat [html2css' el base | XML.NodeElement el <- children]

parseMediaQuery :: M.Map XML.Name Txt.Text -> Query.Expr
parseMediaQuery attrs
    | Just text <- "media" `M.lookup` attrs = Query.parse' (tokenize text) []
    | otherwise = []

---- Parsing (legacy)
externalStyles :: StyleSheet s => s -> (M.Map XML.Name Txt.Text -> Bool) ->
        XML.Element -> (URI -> IO Txt.Text) -> IO s
externalStyles a b c d = externalStylesForURL a b c nullURI d

M xml-conduit-stylist/xml-conduit-stylist.cabal => xml-conduit-stylist/xml-conduit-stylist.cabal +2 -2
@@ 10,7 10,7 @@ name:                xml-conduit-stylist
-- PVP summary:      +-+------- breaking API changes
--                   | | +----- non-breaking API additions
--                   | | | +--- code changes with no API change
version:             1.0.0.0
version:             1.0.1.0

-- A short (one-line) description of the package.
synopsis:            Bridge between xml-conduit/html-conduit and stylist


@@ 61,7 61,7 @@ library
  
  -- Other library packages from which modules are imported.
  build-depends:       base >=4.9 && <4.10,
                       stylist >=1 && <2, css-syntax, unordered-containers,
                       stylist >=1.1.1 && <2, css-syntax, unordered-containers,
                       xml-conduit >=1.8 && < 1.9, text, containers,
                       network-uri