From 7061161d3db1783671a9d594ac4974d11c182bdc Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Mon, 15 Jul 2019 12:36:24 +1200 Subject: [PATCH] Parse HTML 'style' attributes. --- src/Data/CSS/Syntax/StyleSheet.hs | 4 +++- stylish-haskell.cabal | 2 +- stylish-html-conduit/src/Data/HTML2CSS.hs | 7 +++++-- stylish-html-conduit/stylish-html-conduit.cabal | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Data/CSS/Syntax/StyleSheet.hs b/src/Data/CSS/Syntax/StyleSheet.hs index edb0cbe..9da707c 100644 --- a/src/Data/CSS/Syntax/StyleSheet.hs +++ b/src/Data/CSS/Syntax/StyleSheet.hs @@ -1,7 +1,9 @@ module Data.CSS.Syntax.StyleSheet ( - parse, TrivialStyleSheet(..), + parse, parse', TrivialStyleSheet(..), StyleSheet(..), skipAtRule, StyleRule(..), + -- For parsing at-rules, HTML "style" attribute, etc. + parseProperties, parseProperties', -- for testing scanValue ) where diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index 7ef776b..ec761b2 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -10,7 +10,7 @@ name: stylish-haskell -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change -version: 0.2.2 +version: 0.3.0 -- A short (one-line) description of the package. synopsis: Apply CSS styles to a document tree. diff --git a/stylish-html-conduit/src/Data/HTML2CSS.hs b/stylish-html-conduit/src/Data/HTML2CSS.hs index a045551..fa82bcd 100644 --- a/stylish-html-conduit/src/Data/HTML2CSS.hs +++ b/stylish-html-conduit/src/Data/HTML2CSS.hs @@ -12,6 +12,7 @@ import qualified Data.Text as Txt import qualified Text.XML as XML import Data.CSS.Syntax.StyleSheet import Data.CSS.Style +import Data.CSS.Syntax.Tokens (tokenize) import Network.URI @@ -64,13 +65,15 @@ traverseStyles :: PropertyParser s => (s -> [o] -> o) -> (s -> Txt.Text -> o) -> QueryableStyleSheet s -> XML.Element -> o traverseStyles = traverseStyles' Nothing temp Nothing traverseStyles' parent parentStyle previous builder textBuilder stylesheet el@( - XML.Element _ _ children + XML.Element _ attrs children ) = builder style $ traverseChildren Nothing children where stylishEl = elToStylish el parent previous maybeEl = Just stylishEl style = cascade stylesheet stylishEl overrides parentStyle - overrides = [] -- TODO + overrides | Just styleAttr <- "style" `M.lookup` attrs = + fst $ parseProperties' $ tokenize styleAttr + | otherwise = [] traverseChildren prev (XML.NodeContent txt:nodes) = textBuilder style txt : traverseChildren prev nodes diff --git a/stylish-html-conduit/stylish-html-conduit.cabal b/stylish-html-conduit/stylish-html-conduit.cabal index 697f9b4..8e32acd 100644 --- a/stylish-html-conduit/stylish-html-conduit.cabal +++ b/stylish-html-conduit/stylish-html-conduit.cabal @@ -10,7 +10,7 @@ name: stylish-html-conduit -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change -version: 0.1.0.0 +version: 0.1.0.1 -- A short (one-line) description of the package. synopsis: Bridge between html-conduit and stylish-haskell @@ -61,7 +61,7 @@ library -- Other library packages from which modules are imported. build-depends: base >=4.9 && <4.10, - stylish-haskell >= 0.2.2, + stylish-haskell >= 0.3.0, css-syntax, xml-conduit, text, containers, network-uri -- 2.30.2