From 911f85220fef2685f53ce240a436c6753de89d33 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Thu, 31 Dec 2020 17:56:56 +1300 Subject: [PATCH] Style visited links (dading quieter). --- rhapsode.cabal | 2 +- src/Links.hs | 2 ++ src/Render.hs | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/rhapsode.cabal b/rhapsode.cabal index 10b31b5..2375ee6 100644 --- a/rhapsode.cabal +++ b/rhapsode.cabal @@ -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 >= 2.2 && <3, css-syntax, xml-conduit-stylist >= 2.2 && <3, scientific, + stylist >= 2.4 && <3, css-syntax, xml-conduit-stylist >= 2.3 && <3, scientific, async, hurl >= 1.5, filepath, temporary, file-embed >= 0.0.9 && < 0.1, time, text-trie >= 0.2.5, parallel >= 1 diff --git a/src/Links.hs b/src/Links.hs index 40191af..e0a95e3 100644 --- a/src/Links.hs +++ b/src/Links.hs @@ -19,10 +19,12 @@ import System.Directory -- For locating links.xml, suggestions.gmni import System.FilePath import System.IO (hPrint, stderr) -- For error reporting +-- For suggestions.gmni import Data.Trie.Text (Trie) import qualified Data.Trie.Text as Trie import Data.List (nub) import Control.Concurrent.MVar (readMVar) +import Control.Concurrent (forkIO) data Link = Link { label :: Text, diff --git a/src/Render.hs b/src/Render.hs index 009e6e8..750eb8a 100644 --- a/src/Render.hs +++ b/src/Render.hs @@ -34,6 +34,11 @@ import Control.Concurrent.Async (forConcurrently) import System.IO.Temp import Control.Exception (catch) +--- For psuedoclasses +import qualified Data.Trie.Text as Trie +import qualified Data.CSS.Syntax.Selector as CSSSel +import Control.Concurrent.MVar (readMVar) + -- Internal Rhapsode Subcomponents import SpeechStyle import SSML @@ -106,13 +111,22 @@ targetSel "#" = [CSSTok.Colon, CSSTok.Ident "root"] targetSel ('#':id) = [CSSTok.Hash CSSTok.HUnrestricted $ Txt.pack id] targetSel _ = [] -rhapsodePseudoFilter url = CSSPseudo.addRewrite "link" "[src], [href]" $ +testVisited :: Trie.Trie () -> URI -> String -> Bool +testVisited hist base val = uriToText url `Trie.member` hist + where + url = fromMaybe nullURI (parseURIReference val) `relativeTo` base + uriToText uri = pack $ uriToString id uri "" + +rhapsodePseudoFilter url hist = + CSSPseudo.addTest "visited" Nothing "href" (CSSSel.PropertyFunc $ testVisited hist url) $ + CSSPseudo.addRewrite "link" "[src], [href]" $ CSSPseudo.addRewrite' "target" (targetSel $ uriFragment url) $ CSSPseudo.htmlPsuedoFilter Style.queryableStyleSheet -------- ---- Download assets -------- + downloadAssets session mimes (StyleAssets _ assets) = do dir <- Dir.getXdgDirectory Dir.XdgCache "rhapsode" Dir.removeDirectoryRecursive dir `catch` ignoreError -- Clear cache. @@ -136,8 +150,9 @@ foreign export ccall c_renderDoc :: StablePtr Session -> StablePtr Page -> Bool c_renderDoc c_session c_page rewriteURLs = do session <- deRefStablePtr c_session page <- deRefStablePtr c_page + hist <- readMVar $ visitedURLs page css' <- retreiveStyles session $ css page - let pseudoFilter = rhapsodePseudoFilter $ Types.url page + let pseudoFilter = rhapsodePseudoFilter (Types.url page) hist qCSS <- if rewriteURLs then do assets <- downloadAssets session [ "audio/vnd.wav" -- 2.30.2