-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.hs
69 lines (61 loc) · 3.54 KB
/
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
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
module Converter where
backticks :: String
backticks = "```"
languageMark :: String
languageMark = backticks ++ "haskell"
birdticks :: String
birdticks = "<>"
-- | converts a files contents from the LHS birdtick style
-- to markdown, replacing the code marked by birdticks with ```haskell ... ```
convertToMd :: String -> String
convertToMd = unlines . convert' "" . lines
where
convert' :: String -> [String] -> [String]
convert' prev []
| isFirstOO prev birdticks = [backticks] -- close code tags at the end
| otherwise = []
convert' prev (h:t)
| not (isFirstOO prev birdticks) && -- checks for code to stars, inserts newline above code block if needed
isFirstOO h birdticks = (if prev == "" then [] else [""]) ++ [languageMark, drop 2 h] ++ rest
| isFirstOO h birdticks = (drop 2 h):rest -- checks for code
| isFirstOO prev birdticks = [backticks] ++ (if h == "" then [] else [""]) ++ [h] ++ rest -- checks for code end, insers newline after code block if needed
| otherwise = h:rest
where
rest = convert' h t
-- | converts a files contents from git flavoured markdown
-- to literate haskell, replacing code marked with ```haskell ...``` with > birdticks
-- and code marked with ``` ... ``` with < birdticks
-- quotes are converted to `NOTE:` marked lines.
convertToLhs :: String -> String
convertToLhs = unlines . convert' False False "" . lines
where
convert' :: Bool -> Bool -> String -> [String] -> [String]
convert' inCode inSample prev [] = []
convert' inCode inSample prev (h:t)
| inCode && h /= backticks = ["> " ++ h] ++ (convert' True False h t) -- handles code
| inSample && h /= backticks = ["< " ++ h] ++ (convert' False True h t) -- handles code sample
| headingN h /= 0 = [headingO h ++ (tail $ dropWhile (=='#') h) ++ headingC h] ++ rest -- converts headings to html
| h == languageMark = (if prev == "" then [] else [""]) ++ convert' True False prev t -- handles code block starts, adds newline if needed
| (inCode || inSample)
&& h == "```" = (if length t > 0 && (head t) == ""
then []
else [""])
++ convert' False False prev t -- handles code and sample block ends
| take 3 h == backticks = (if prev == "" then [] else [""]) ++ convert' False True prev t -- handles sample block starts, adds newline if needed
| isFirstOO h ">" = ["NOTE: " ++ drop 2 h] ++ rest -- handles quotes
| otherwise = h:rest
where
headingO h = "<h" ++ show (headingN h) ++ ">" -- heading open tag
headingC h = "</h" ++ show (headingN h) ++ ">" -- heading close tag
headingN h = length $ takeWhile (=='#') h -- looks for headings
rest = convert' False False h t
-- | uses @isFirst@ to check multiple characters
isFirstOO :: String -> [Char] -> Bool
isFirstOO l e = or $ map (isFirst l) e
-- | checks wether the first two characters of a string are
-- a given character and ' '
-- useful to look for code and quotes
isFirst :: String -> Char -> Bool
isFirst [] _ = False
isFirst (h:' ':_) a = a == h
isFirst (h:_) a = False