{-# LANGUAGE OverloadedStrings #-}
module Main where
import System.Environment
import Data.Char (isSpace)
import qualified Network.HTTP.Client as HTTP
import Network.HTTP.Client.Internal
import qualified Network.HTTP.Client.TLS as TLS
import Network.URI
import qualified Data.ByteString.Lazy.Char8 as C8
import qualified Text.HTML.DOM as HTML
import qualified Text.XML as XML
import Data.Text as Txt (pack, unpack, Text(..), append,
words, unwords, head, last, stripStart, stripEnd)
import qualified Data.CSS.Syntax.StyleSheet as CSS
import qualified Data.CSS.Style as Style
import qualified Data.CSS.Syntax.Tokens as CSSTok
import qualified Data.HTML2CSS as H2C
import qualified Data.List as L
import qualified Data.Map as M
import Data.Scientific (toRealFloat)
import Data.Maybe (fromJust)
import DefaultCSS
import StyleTree
import SSML
main :: IO ()
main = do
url:_ <- getArgs
-- TODO support more URI schemes, and do nonblocking networking. This could be it's own module.
request <- HTTP.parseRequest url
manager <- HTTP.newManager TLS.tlsManagerSettings
response <- HTTP.httpLbs request manager
let html = XML.documentRoot $ HTML.parseLBS $ HTTP.responseBody response
style <- retreiveStyles html manager $ fromJust $ parseURI url
let transcript = stylize style html
C8.putStrLn $ renderElLBS $ styleToSSML transcript
renderElLBS el = XML.renderLBS XML.def $ XML.Document {
XML.documentPrologue = XML.Prologue [] Nothing [],
XML.documentRoot = el,
XML.documentEpilogue = []
}
retreiveStyles html manager base = do
style <- H2C.externalStylesForURL authorStyle testMedia html base loadURL
return style
where
emptyStyle :: Style.QueryableStyleSheet StyleTree
emptyStyle = Style.queryableStyleSheet
agentStyle = H2C.cssPriorityAgent emptyStyle `CSS.parse` Txt.pack userAgentCSS
authorStyle = H2C.internalStylesForURL testMedia agentStyle base html
loadURL url = do -- TODO parallelise.
request <- requestFromURI url
response <- HTTP.httpLbs request manager
return $ Txt.pack $ C8.unpack $ HTTP.responseBody response
testMedia attrs = media == Nothing || media == Just "speech"
where media = "media" `M.lookup` attrs
stylize styles html = H2C.traversePrepopulatedStyles buildChild buildNode buildText styles html
where
buildChild self _ | content self == [] = Nothing
| otherwise = Just [Style.temp {content = content self}]
buildNode self children = self {children = children}
buildText _ txt = Style.temp {content = [Content txt]}