-
Notifications
You must be signed in to change notification settings - Fork 19
/
illiterator.hs
46 lines (34 loc) · 1.33 KB
/
illiterator.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
40
41
42
43
44
45
46
{-
Agda illiterator.
This Haskell program converts from .lagda files to .agda files.
Keeps only what is within code environments, removing lines which
consist of comments without code. It it a unix pipe. There are
no command-line options.
Typical usage:
$ cat file.lagda | runhaskell illiterator.hs > file.agda
-}
import Data.Char
isComment :: String -> Bool
isComment [] = False
isComment (x : xs) = (x == '-' && not (null xs) && head xs == '-')
|| (isSpace x && isComment xs)
begin = "\\begin{code}"
end = "\\end{code}"
illiterator, copy :: [String] -> String
illiterator [] = []
illiterator (xs:xss)
| take (length begin) (dropWhile isSpace xs) == begin = copy xss
| otherwise = illiterator xss
copy [] = []
copy (xs:xss)
| take (length end) (dropWhile isSpace xs) == end = "\n" ++ illiterator xss
| isComment xs = copy xss
| otherwise = xs ++ "\n" ++ copy xss
reduceBlankLines :: String -> String
reduceBlankLines "" = ""
reduceBlankLines ('\n' : '\n' : '\n' : xs) = reduceBlankLines ('\n' : '\n' : xs)
reduceBlankLines (x:xs) = x : reduceBlankLines xs
pipe :: String -> String
pipe stdin = reduceBlankLines(illiterator(lines stdin))
main :: IO()
main = interact pipe