{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE BlockArguments #-}
module Main where
import qualified Data.Text.Lazy.IO as TL
import Network.HTTP.Simple
import Options.Applicative
import qualified Readability as R
import Text.XML
data Source = File FilePath | Web Request deriving (Show)
data Opts = Opts {optInput :: Source} deriving (Show)
main :: IO ()
main = do
(Opts src) <- execParser opts
article <- case src of
File f -> R.fromFile f
Web req -> R.fromByteString . getResponseBody <$> httpLBS req
case R.summary <$> article of
Just doc -> TL.putStrLn $ renderText def {rsPretty = True, rsXMLDeclaration = True} doc
Nothing -> return ()
opts :: ParserInfo Opts
opts =
info
(options <**> helper)
( fullDesc
<> progDesc "Provide FILE or URL as source"
<> header "readability - extract article from HTML"
)
options :: Parser Opts
options = do
inf <- argument source (metavar "SOURCE")
pure $ Opts inf
source :: ReadM Source
source = maybeReader \s -> Web <$> parseRequest s <|> Just (File s)