From 9f81815facf331222276656692a6a67413d6c4fe Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Thu, 12 Mar 2020 20:50:21 +1300 Subject: [PATCH] CSS fixes, start implementing downloads. --- rhapsode.cabal | 2 +- src/Input.hs | 12 ++++++++++++ src/Main.hs | 10 ++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/rhapsode.cabal b/rhapsode.cabal index 5639bc6..7acb91f 100644 --- a/rhapsode.cabal +++ b/rhapsode.cabal @@ -64,7 +64,7 @@ executable rhapsode html-conduit, xml-conduit, text, containers, data-default-class, network-uri, stylist >= 1.1, css-syntax, xml-conduit-stylist, scientific, - async, hurl + async, hurl >= 1.1.0.0, filepath -- Directories containing source files. hs-source-dirs: src diff --git a/src/Input.hs b/src/Input.hs index 491712b..6c12fe1 100644 --- a/src/Input.hs +++ b/src/Input.hs @@ -17,6 +17,8 @@ import System.Environment import System.Directory import Data.List import Data.Default.Class +import Data.Maybe (fromMaybe) +import System.FilePath --- Commandline arguments data ProgramCtl = ProgramCtl { @@ -92,3 +94,13 @@ docForText txt = XML.Document { }, XML.documentEpilogue = [] } + +newFilePath filepath count = do + let realpath = filepath ++ show count + exists <- doesPathExist realpath + if exists then newFilePath filepath $ count + 1 else return realpath + +downloadsDir = do + downloads <- lookupEnv "XDG_DOWNLOAD_DIR" + home <- getHomeDirectory + return $ fromMaybe (home "Downloads") downloads diff --git a/src/Main.hs b/src/Main.hs index f528f43..f214d8a 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -74,9 +74,10 @@ retreiveStyles uri html manager base = do loadURL url = do response <- fetchURL manager ["text/css"] url + let charsets' = map unpack charsets return $ case response of ("text/css", Left text) -> text - ("text/css", Right bytes) -> applyCSScharset charsets $ B.toStrict bytes + ("text/css", Right bytes) -> applyCSScharset charsets' $ B.toStrict bytes (_, _) -> "" lowerVars "speech" = CSSCond.B True @@ -84,14 +85,11 @@ retreiveStyles uri html manager base = do lowerVars _ = CSSCond.B False lowerToks _ = CSSCond.B False -applyCSScharset (charset:charsets) bytes | cssCharset (CSSTok.tokenize text') == charset = text +applyCSScharset (charset:charsets) bytes + | cssCharset (CSSTok.tokenize text) == Txt.pack charset = text | otherwise = applyCSScharset charsets bytes where text = convertCharset charset bytes - -- I don't know how better to handle these errors in the APIs I'm using... - text' = unsafePerformIO $ catch (evaluate text) handleDecodeError - handleDecodeError :: UnicodeException -> IO Text -- Type signature REQUIRED - handleDecodeError _ = return "" applyCSScharset _ bytes = convertCharset "utf-8" bytes cssCharset toks | (CSSTok.AtKeyword "charset":toks') <- skipCSSspace toks, (CSSTok.String charset:_) <- skipCSSspace toks' = charset -- 2.30.2