/
lhs2hs.hs
33 lines (29 loc) · 879 Bytes
/
lhs2hs.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
module Main where
import Control.Monad
import Data.Maybe (mapMaybe)
import System.Directory (doesFileExist)
import System.Environment (getArgs)
import System.FilePath (addExtension, dropExtension, takeExtension)
main :: IO ()
main = do
infile <- parseArgs
indata <- readFile infile
let outfile = addExtension (dropExtension infile) "hs"
writeFile outfile (convert indata)
parseArgs :: IO FilePath
parseArgs = do
args <- getArgs
when (length args /= 1) die
let infile = head args
exists <- doesFileExist infile
when (takeExtension infile /= ".lhs" || not exists) die
return infile
where
die = error "Usage: lhs2hs <path-to-lhs-file>"
convert :: String -> String
convert = unlines . mapMaybe f . lines
where
f line = case line of
('>' : ' ' : rest) -> Just rest
('>' : rest) -> Just rest
_ -> Nothing