1 {-# LANGUAGE DoAndIfThenElse #-}
5 import Data.Maybe (fromJust)
6 import Prelude hiding (readFile)
7 import System.Directory (doesFileExist)
15 import System.IO.UTF8 (readFile)
16 import Text.XML.HXT.Core (
20 import CommandLine (show_help)
21 import Configuration (Cfg(..), get_cfg, use_account)
22 import LWN.Article (real_article_path)
23 import LWN.HTTP (get_page, log_in, make_cookie_jar)
24 import LWN.Page (epublish, parse)
25 import LWN.XHTML (parse_lwn)
29 -- | Try to parse the given article using HXT. We try a few different
30 -- methods; if none of them work, we return 'Nothing'.
31 get_xml_from_article :: Cfg -> IO (Maybe (IOStateArrow s b XmlTree))
32 get_xml_from_article cfg = do
33 my_article <- real_article_path (article cfg)
34 is_file <- doesFileExist my_article
37 contents <- readFile my_article
38 return $ Just $ parse_lwn contents
40 -- Download the URL and try to parse it.
41 if use_account cfg then do
42 -- use_account would be false if these fromJusts would fail.
44 li_result <- log_in cj
45 (fromJust $ username cfg)
46 (fromJust $ password cfg)
50 let msg = "Failed to log in. " ++ err
52 Right response_body -> do
53 hPutStrLn stderr response_body
55 html <- get_page (Just cj) my_article
59 let msg = "Failed to retrieve page. " ++ err
62 Right h -> return $ Just $ parse_lwn h
64 html <- get_page Nothing my_article
67 let msg = "Failed to retrieve page. " ++ err
70 Right h -> return $ Just $ parse_lwn h
73 -- | If we're given an empty path, return a handle to
74 -- 'stdout'. Otherwise, open the given file and return a read/write
76 get_output_handle :: FilePath -> IO Handle
77 get_output_handle path =
81 openBinaryFile path WriteMode
87 output_handle <- get_output_handle (output cfg)
88 maybe_html <- get_xml_from_article cfg
94 Just stuff -> epublish stuff output_handle