~alcinnz/rhapsode

ec9a251c225a8a6bb25846b76fc8998ccf575cc4 — Adrian Cochrane 5 years ago 27745b9
Load userstyles.
2 files changed, 17 insertions(+), 2 deletions(-)

M rhapsode.cabal
M src/Main.hs
M rhapsode.cabal => rhapsode.cabal +1 -1
@@ 60,7 60,7 @@ executable rhapsode
  -- other-extensions:    
  
  -- Other library packages from which modules are imported.
  build-depends:       base >=4.9 && <4.10,
  build-depends:       base >=4.9 && <4.10, directory,
        http-client, http-client-tls, bytestring,
        html-conduit, xml-conduit, text, containers,
        network-uri,

M src/Main.hs => src/Main.hs +16 -1
@@ 24,6 24,7 @@ import qualified Data.List as L
import qualified Data.Map as M
import Data.Scientific (toRealFloat)
import Data.Maybe (fromJust)
import System.Directory as Dir

import DefaultCSS
import StyleTree


@@ 49,7 50,7 @@ renderElLBS el = XML.renderLBS XML.def $ XML.Document {

retreiveStyles html manager base = do
    style <- H2C.externalStylesForURL authorStyle testMedia html base loadURL
    return style
    loadUserStyles style
  where
    emptyStyle :: Style.QueryableStyleSheet StyleTree
    emptyStyle = Style.queryableStyleSheet


@@ 61,6 62,20 @@ retreiveStyles html manager base = do
        response <- HTTP.httpLbs request manager
        return $ Txt.pack $ C8.unpack $ HTTP.responseBody response

loadUserStyles styles = do
    dir <- Dir.getXdgDirectory Dir.XdgConfig "rhapsode"
    exists <- Dir.doesDirectoryExist dir
    loadDirectory dir exists
  where
    loadDirectory _ False = return styles
    loadDirectory dir True = do
        files <- Dir.listDirectory dir
        loadFiles (H2C.cssPriorityUser styles) files
    loadFiles style (file:files) = do
        source <- readFile file
        CSS.parse style (Txt.pack source) `loadFiles` files
    loadFiles style [] = return style

testMedia attrs = media == Nothing || media == Just "speech"
    where media = "media" `M.lookup` attrs