-
Notifications
You must be signed in to change notification settings - Fork 0
/
literate_converter.hs
39 lines (29 loc) · 1020 Bytes
/
literate_converter.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Main where
import Converter
import Data.Char (toLower)
import Control.Monad (when)
import System.Environment (getArgs)
import System.Exit (exitWith, ExitCode (..))
useage :: String
useage = "Useage: \
\lhsc (toLhs|toMd) file1 [file2] [...]"
exitErr :: String -> IO a
exitErr msg = putStrLn (msg ++ "\n" ++ useage) >> exitWith (ExitFailure 1)
main = do
args <- getArgs
when (length args < 2) $
exitErr "not enough arguments given."
let paths = tail args
files <- mapM (readFile) paths
let (func, ex) = case (map toLower $ head args) of
"tolhs" -> (convertToLhs, "lhs")
"tomd" -> (convertToMd, "md")
_ -> (id, "id")
when (ex == "id") $
exitErr $ "command " ++ head args ++ " not recongised"
-- convert
converted <- return $ map func files
-- write back
mapM_ (uncurry writeFile) $ zip (map (++ "." ++ ex) paths) converted
-- print finished
putStrLn "finished conversions"