-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMunge.hs
75 lines (68 loc) · 2.83 KB
/
Munge.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Munge where
import Parser
import Text.ParserCombinators.Parsec
-- | Take a parsed line, with the first element being metadata and the remainer being data
-- | and turn that data into a nice markdown document
-- Examples:
-- >>> munge [["Website;Full Name", "jack.ellenberger.zone","","Jack Ellenberger"], [""]]
-- ["Website: jack.ellenberger.zone\n","Full Name: Jack Ellenberger\n"]
-- >>> munge [["Company;_skip;_take;", "Braintree", "Skip me", "I did some stuff"]]
-- ["Company: Braintree\n","I did some stuff\n"]
munge :: [[String]] -> [String]
munge parsedLines =
concat $ map applyMetadata $ filterLines parsedLines
-- | Take structured, cleaned input and accumulate an md doc
-- >>> applyMetadata ["Key", "Value"]
-- ["Key: Value\n"]
-- >>> applyMetadata ["_take;_skip;_take;","take me", "skip me", "take me too"]
-- ["take me\n","take me too\n"]
applyMetadata :: [String] -> [String]
applyMetadata (metadata:celldata) = case
parse (separatedValues ';') "(unknown)" (metadata ++ "\n\r") of
Left err -> return $ show err
Right res -> do
let meta = concat $ filterLines res
let cell = filter keepCell celldata
concat $ map templateMetadata $ zip meta cell
-- | Given a tuple of key, value, return "Key: Value"
-- | or "Value" when key == "_take"
-- | or "" when key == "_skip"
-- >>> templateMetadata ("Key", "Value")
-- ["Key: Value\n"]
-- >>> templateMetadata ("_take", "take this string")
-- ["take this string\n"]
-- >>> templateMetadata ("_skip", "skip this string")
-- []
templateMetadata :: (String, String) -> [String]
templateMetadata dataPair = case dataPair of ("_skip", _) -> []
("_take", val) -> [val ++ "\n"]
("_takeLn", val) -> [val ++ "\n\n"]
("_lnTake", val) -> ["\n" ++ val ++ "\n"]
(key, val) -> [key ++ ": " ++ val ++ "\n"]
-- | Given a list of string lists (see: a list of lines of words)
-- | remove lines marked for skipping and empty words
-- Examples:
-- >>> filterLines [["", "skip", "me"], ["keep", "", "me"]]
-- [["keep","me"]]
filterLines :: [[String]] -> [[String]]
filterLines lines =
map (filter keepCell) $ filter keepLine lines
-- | False if first element is an empty string or "_skip_line", True otherwise
-- Examples:
-- >>> keepLine ["", "anything"]
-- False
-- >>> keepLine ["anything", "else"]
-- True
keepLine :: [String] -> Bool
keepLine xs = case xs of ("":_) -> False
[] -> False
xs -> True
-- | False if empty string, True otherwise
-- Examples:
-- >>> keepCell ""
-- False
-- >>> keepCell "anything"
-- True
keepCell :: String -> Bool
keepCell str = case str of "" -> False
str -> True