-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathMarkdone.hs
134 lines (124 loc) · 3.73 KB
/
Markdone.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
-- | A subset of markdown that only supports @#headings@ and code
-- fences.
--
-- All content must be in section headings with proper hierarchy,
-- anything else is rejected.
module HIndent.Internal.Test.Markdone
( Token(..)
, Markdone(..)
, tokenize
, parse
) where
import Control.DeepSeq
import Control.Monad.Catch
import Control.Monad.State.Strict (State, evalState, get, put)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S8
import Data.Char
import Data.Typeable
import GHC.Generics
-- | A markdone token.
data Token
= Heading !Int !ByteString
| PlainLine !ByteString
| BeginFence !ByteString
| EndFence
deriving (Eq, Show)
-- | A markdone document.
data Markdone
= Section !ByteString ![Markdone]
| CodeFence !ByteString !ByteString
| PlainText !ByteString
deriving (Eq, Show, Generic)
instance NFData Markdone
-- | Parse error.
data MarkdownError
= NoFenceEnd
| ExpectedSection
deriving (Typeable, Show)
instance Exception MarkdownError
data TokenizerMode
= Normal
| Fenced
-- | Tokenize the bytestring.
tokenize :: ByteString -> [Token]
tokenize input =
evalState (mapM token (S8.filter (/= '\r') <$> S8.lines input)) Normal
where
token :: ByteString -> State TokenizerMode Token
token line = do
mode <- get
case mode of
Normal ->
if S8.isPrefixOf "#" line
then let (hashes, title) = S8.span (== '#') line
in return
$ Heading (S8.length hashes) (S8.dropWhile isSpace title)
else if S8.isPrefixOf "```" line
then do
put Fenced
return
$ BeginFence
(S8.dropWhile (\c -> c == '`' || c == ' ') line)
else return $ PlainLine line
Fenced ->
if line == "```"
then do
put Normal
return EndFence
else return $ PlainLine line
-- | Parse into a forest.
parse :: (Functor m, MonadThrow m) => [Token] -> m [Markdone]
parse = go (0 :: Int)
where
go level =
\case
(Heading n label:rest) ->
let (children, rest') =
span
(\case
Heading nextN _ -> nextN > n
_ -> True)
rest
in do
childs <- go (level + 1) children
siblings <- go level rest'
return (Section label childs : siblings)
(BeginFence label:rest)
| level > 0 ->
let (content, rest') =
span
(\case
PlainLine {} -> True
_ -> False)
rest
in case rest' of
(EndFence:rest'') ->
fmap
(CodeFence
label
(S8.intercalate "\n" (map getPlain content)) :)
(go level rest'')
_ -> throwM NoFenceEnd
PlainLine p:rest
| level > 0 ->
let (content, rest') =
span
(\case
PlainLine {} -> True
_ -> False)
(PlainLine p : rest)
in fmap
(PlainText
(S8.intercalate
"\n"
(filter (not . S8.null) (map getPlain content))) :)
(go level rest')
[] -> return []
_ -> throwM ExpectedSection
getPlain (PlainLine x) = x
getPlain _ = ""