~alcinnz/rhapsode

911f85220fef2685f53ce240a436c6753de89d33 — Adrian Cochrane 3 years ago e7cfb21
Style visited links (dading quieter).
3 files changed, 20 insertions(+), 3 deletions(-)

M rhapsode.cabal
M src/Links.hs
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 >= 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
  

M src/Links.hs => src/Links.hs +2 -0
@@ 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,

M src/Render.hs => src/Render.hs +17 -2
@@ 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"