]> gitweb.michael.orlitzky.com - dead/lwn-epub.git/blob - src/Main.hs
Combine ArticlePage and FullPage into one Page type.
[dead/lwn-epub.git] / src / Main.hs
1 {-# LANGUAGE ScopedTypeVariables, RecordWildCards #-}
2 module Main
3 where
4
5 import System.IO (
6 Handle,
7 IOMode (WriteMode),
8 openBinaryFile,
9 stdout
10 )
11 import Text.XML.HXT.Core
12
13 import CommandLine (Args(..), apply_args)
14 import LWN.Page
15
16 my_read :: String -> IOStateArrow s b XmlTree
17 my_read =
18 readDocument [ withValidate no,
19 withParseHTML yes,
20 withInputEncoding utf8,
21 withWarnings no ]
22
23 -- | If we're given an empty path, return a handle to
24 -- 'stdout'. Otherwise, open the given file and return a read/write
25 -- handle to that.
26 get_output_handle :: FilePath -> IO Handle
27 get_output_handle path =
28 if (null path) then
29 return stdout
30 else
31 openBinaryFile path WriteMode
32
33
34 -- | Convert the given article to either a URL or a filesystem
35 -- path. If the given article exists on the filesystem, we assume
36 -- it's a file. Otherwise, we check to see if it's a URL. Failing
37 -- that, we try to construct a URL from what we're given and do our
38 -- best.
39 real_article_path :: String -> IO String
40 real_article_path = return . id
41
42 main :: IO ()
43 main = do
44 Args{..} <- apply_args
45 output_handle <- get_output_handle output
46 input_path <- real_article_path article
47 let html = my_read input_path
48 result <- parse html
49
50 case result of
51 Just stuff -> epublish stuff output_handle
52 Nothing -> return ()
53
54 putStrLn "Done."