-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrip-pgn.hs
46 lines (39 loc) · 1.5 KB
/
strip-pgn.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
import Data.List
import Data.Char
import System.Environment
main :: IO ()
main = do
args <- getArgs
input <- getContents
case args of
["--help"] ->
putStrLn "usage: strip-pgn {--strip-comments|--move-list|--move-strings| }"
[] ->
putStrLn $ strip_comments input
["--strip-comments"] ->
putStrLn $ strip_comments input
["--move-list"] ->
(putStrLn . unwords . words . strip_move_nums . strip_tags . strip_comments) input
["--move-strings"] ->
(putStrLn . unwords . words . add_quotation_marks . strip_move_nums . strip_tags . strip_comments) input
_:_:_ ->
putStrLn $ "Multiple arguments are not allowed."
s:_ ->
putStrLn $ "Unknown argument " ++ s
strip_comments :: String -> String
strip_comments [] = []
strip_comments ('{':xs) = strip_comments (tail $ dropWhile (/= '}') xs)
strip_comments (x:xs) = x:strip_comments xs
strip_tags :: String -> String
strip_tags [] = []
strip_tags ('[':xs) = strip_tags (tail $ dropWhile (/= ']') xs)
strip_tags (x:xs) = x:strip_tags xs
strip_move_nums :: String -> String
strip_move_nums [] = []
strip_move_nums (x:'.':xs) | isDigit x = strip_move_nums xs
strip_move_nums (x:y:'.':xs) | all isDigit [x, y] = strip_move_nums xs
strip_move_nums (x:y:z:'.':xs) | all isDigit [x, y, z] = strip_move_nums xs
strip_move_nums ('.':'.':xs) = strip_move_nums xs
strip_move_nums (x:xs) = x:strip_move_nums xs
add_quotation_marks :: String -> String
add_quotation_marks = intercalate ", " . map ((++ "\"") . ("\"" ++)) . words