1 {-# LANGUAGE ScopedTypeVariables, RecordWildCards #-}
5 import Prelude hiding (readFile)
6 import System.Directory(doesFileExist)
13 import System.IO.UTF8 (readFile)
14 import Text.XML.HXT.Core
16 import CommandLine (Args(..), apply_args, show_help)
20 my_read_opts :: SysConfigList
21 my_read_opts = [ withValidate no,
25 -- | My version of HandsomeSoup's parseHTML.
26 my_read :: String -> IOStateArrow s b XmlTree
27 my_read = readString my_read_opts
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 :: String -> IO (Maybe (IOStateArrow s b XmlTree))
32 get_xml_from_article s = do
33 article <- real_article_path s
34 is_file <- doesFileExist article
37 contents <- readFile article
38 return $ Just $ my_read contents
40 -- Download the URL and try to parse it.
43 -- | If we're given an empty path, return a handle to
44 -- 'stdout'. Otherwise, open the given file and return a read/write
46 get_output_handle :: FilePath -> IO Handle
47 get_output_handle path =
51 openBinaryFile path WriteMode
54 -- | Convert the given article to either a URL or a filesystem
55 -- path. If the given article exists on the filesystem, we assume
56 -- it's a file. Otherwise, we check to see if it's a URL. Failing
57 -- that, we try to construct a URL from what we're given and do our
59 real_article_path :: String -> IO String
60 real_article_path = return . id
64 Args{..} <- apply_args
65 output_handle <- get_output_handle output
66 maybe_html <- get_xml_from_article article
73 Just stuff -> epublish stuff output_handle