~alcinnz/rhapsode

9f81815facf331222276656692a6a67413d6c4fe — Adrian Cochrane 4 years ago 2d6a43f
CSS fixes, start implementing downloads.
3 files changed, 17 insertions(+), 7 deletions(-)

M rhapsode.cabal
M src/Input.hs
M src/Main.hs
M rhapsode.cabal => rhapsode.cabal +1 -1
@@ 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

M src/Input.hs => src/Input.hs +12 -0
@@ 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

M src/Main.hs => src/Main.hs +4 -6
@@ 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