Add basic option-handling for telml2html
Getty Ritter
6 years ago
4 | 4 | import qualified Data.TeLML as TeLML |
5 | 5 | import qualified Data.TeLML.Markup as TeLML |
6 | 6 | import qualified Text.Blaze.Renderer.String as B |
7 | import qualified System.Console.GetOpt as Opt | |
7 | 8 | import qualified System.Exit as Sys |
9 | import qualified System.Environment as Env | |
10 | ||
11 | data Options = Options | |
12 | { optInput :: Maybe FilePath | |
13 | , optOutput :: Maybe FilePath | |
14 | } | |
15 | ||
16 | defaultOptions :: Options | |
17 | defaultOptions = Options | |
18 | { optInput = Nothing | |
19 | , optOutput = Nothing | |
20 | } | |
21 | ||
22 | options :: [Opt.OptDescr (Options -> Options)] | |
23 | options = | |
24 | [ Opt.Option ['i'] ["input"] | |
25 | (Opt.ReqArg (\ path opt -> opt { optInput = Just path }) "file") | |
26 | "Read input from this file" | |
27 | , Opt.Option ['o'] ["output"] | |
28 | (Opt.ReqArg (\ path opt -> opt { optOutput = Just path }) "file") | |
29 | "Read input from this file" | |
30 | ] | |
8 | 31 | |
9 | 32 | doRender :: String -> Either String String |
10 | 33 | doRender = TeLML.parse >=> TeLML.render >=> return . B.renderMarkup |
11 | 34 | |
35 | runPipeline :: Maybe FilePath -> Maybe FilePath -> IO () | |
36 | runPipeline inF outF = do | |
37 | cs <- case inF of | |
38 | Nothing -> getContents | |
39 | Just f -> readFile f | |
40 | case doRender cs of | |
41 | Left err -> Sys.die err | |
42 | Right rs -> case outF of | |
43 | Nothing -> putStrLn rs | |
44 | Just f -> writeFile f rs | |
45 | ||
12 | 46 | main :: IO () |
13 | 47 | main = do |
14 | cs <- getContents | |
15 | case doRender cs of | |
16 | Left err -> Sys.die err | |
17 | Right x -> putStrLn x | |
48 | opts <- Opt.getOpt Opt.Permute options `fmap` Env.getArgs | |
49 | case opts of | |
50 | (fs, [], []) -> do | |
51 | let Options | |
52 | { optInput = inF | |
53 | , optOutput = outF | |
54 | } = foldr id defaultOptions fs | |
55 | runPipeline inF outF | |
56 | (_, _, _) -> do | |
57 | Sys.die (Opt.usageInfo "telml2html" options) |