~alcinnz/rhapsode

125865477e9e86029669719b1b3bb3e35c81ed2f — Adrian Cochrane 4 years ago 945a755
Take advantage of new Haskell Stylist assets & pseudoclass filters.
2 files changed, 7 insertions(+), 37 deletions(-)

M rhapsode.cabal
M src/Render.hs
M rhapsode.cabal => rhapsode.cabal +1 -1
@@ 63,7 63,7 @@ library
  build-depends:       base >=4.9 && <=4.12, directory, bytestring,
        html-conduit, xml-conduit, text, containers, data-default-class,
        network-uri,
        stylist >= 1.1, css-syntax, xml-conduit-stylist, scientific,
        stylist >= 2 && <3, css-syntax, xml-conduit-stylist >= 1.2 && <2, scientific,
        async, hurl >= 1.4.1.0, filepath, temporary,
        file-embed >= 0.0.9 && < 0.1
  

M src/Render.hs => src/Render.hs +6 -36
@@ 15,6 15,8 @@ import qualified Data.CSS.Syntax.StyleSheet as CSS
import qualified Data.CSS.Style as Style
import qualified Data.CSS.Syntax.Tokens as CSSTok
import qualified Data.CSS.Preprocessor.Conditions as CSSCond
import           Data.CSS.Preprocessor.Assets
import qualified Data.CSS.Preprocessor.PsuedoClasses as CSSPseudo
import qualified Data.HTML2CSS as H2C
import           Network.URI
import           Network.URI.Fetch


@@ 104,22 106,6 @@ stylize styles html = H2C.traversePrepopulatedStyles buildChild buildNode buildT
--------
---- Download assets
--------
-- TODO upstream into Haskell Stylist, or a new HURL Stylist hackage.

data StyleAssets = StyleAssets {
    filterProps :: [Txt.Text],
    assets :: [URI]
}

instance CSS.StyleSheet StyleAssets where
    addRule (StyleAssets filterProps self) (CSS.StyleRule _ props _) =
        StyleAssets filterProps $ nub (
            self ++ [uri | (prop, val) <- props,
                    prop `elem` filterProps,
                    CSSTok.Url text <- val,
                    Just uri <- [parseAbsoluteURI $ Txt.unpack text]]
            )

downloadAssets session mimes (StyleAssets _ assets) = do
    dir <- Dir.getXdgDirectory Dir.XdgCache "rhapsode"
    Dir.removeDirectoryRecursive dir `catch` ignoreError -- Clear cache.


@@ 134,23 120,6 @@ filterMIMEs mimes cb download@(_, mime, _)
    | mime `elem` mimes = cb download
    | otherwise = return nullURI

rewritePropertyVal rewrites (CSSTok.Url text:vals)
    | Just uri <- parseURIReference $ Txt.unpack text, Just rewrite <- uri `M.lookup` rewrites =
        CSSTok.Url (Txt.pack $ uriToString id rewrite "") : rewritePropertyVal rewrites vals
    | otherwise = CSSTok.Url "" : rewritePropertyVal rewrites vals
rewritePropertyVal rewrites (val:vals) = val:rewritePropertyVal rewrites vals
rewritePropertyVal _ [] = []

data URIRewriter s = URIRewriter (M.Map URI URI) s
instance CSS.StyleSheet s => CSS.StyleSheet (URIRewriter s) where
    setPriority p (URIRewriter r s) = URIRewriter r $ CSS.setPriority p s
    addRule (URIRewriter r s) (CSS.StyleRule sel props psuedo) =
        URIRewriter r $ CSS.addRule s $ CSS.StyleRule sel [
            (prop, rewritePropertyVal r val) | (prop, val) <- props
        ] psuedo
    addAtRule (URIRewriter r s) name toks =
        let (self', toks') = CSS.addAtRule s name toks in (URIRewriter r s, toks)

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


@@ 215,13 184,14 @@ c_renderDoc c_session c_page rewriteURLs = do
    session <- deRefStablePtr c_session
    page <- deRefStablePtr c_page
    css' <- retreiveStyles session $ css page
    let pseudoFilter = CSSPseudo.htmlPsuedoFilter Style.queryableStyleSheet
    qCSS <- if rewriteURLs then do
        assets <- downloadAssets session [
                "audio/vnd.wav"
            ] $ resolve' (StyleAssets ["cue-before", "cue-after", "cue"] []) css'
        let URIRewriter _ qCSS' =  resolve' (URIRewriter (M.fromList assets) Style.queryableStyleSheet) css'
        return qCSS'
        else return $ resolve' Style.queryableStyleSheet css'
        let URIRewriter _ qCSS' =  resolve' (URIRewriter assets pseudoFilter) css'
        return $ CSSPseudo.inner qCSS'
        else return $ CSSPseudo.inner $ resolve' pseudoFilter css'
    let ssml = renderDoc qCSS $ XML.documentRoot $ html page
    B.toStrict ssml `useAsCString` \cstr -> do
        str <- peekCString cstr