summaryrefslogtreecommitdiffstats
path: root/src/MdBlocks/Parser.hs
blob: b5ca193e1372465f7aa2ecf7469c44fd5c3fa989 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
{-# LANGUAGE OverloadedStrings #-}

module MdBlocks.Parser
  ( parseFile
  )
where

import qualified Data.Text as T
import Data.Text (Text)

import MdBlocks.Metadata
import MdBlocks.Types


data State
  = Normal
  | InFence FenceState
  | InIndented IndentedState

data FenceState = FenceState
  { fsStartLine :: !Int
  , fsFenceTok  :: !Text
  , fsHeader    :: !Text
  , fsLines     :: [Text]
  }

data IndentedState = IndentedState
  { isStartLine :: !Int
  , isLines     :: [Text]
  }

data ParseState = ParseState
  { psFile   :: FilePath
  , psMode   :: State
  , psBlocks :: [Block]
  }


parseFile
  :: FilePath
  -> Text
  -> [Block]
parseFile fp txt =
    assignNumbers
  $ finalize
  $ foldl step initial numbered
  where
    numbered =
      zip [1..] (T.lines txt)

    initial =
      ParseState
        { psFile = fp
        , psMode = Normal
        , psBlocks = []
        }


assignNumbers :: [Block] -> [Block]
assignNumbers =
  zipWith
    (\n b -> b { blockNumber = n })
    [1..]


isIndented :: Text -> Bool
isIndented =
  ("    " `T.isPrefixOf`)

stripIndent :: Text -> Text
stripIndent =
  T.drop 4


fenceStart :: Text -> Maybe (Text, Text)
fenceStart line =
  let s = T.stripStart line
  in if "```" `T.isPrefixOf` s
       then Just ("```", T.drop 3 s)
       else Nothing


fenceEnd :: Text -> Text -> Bool
fenceEnd marker line =
    marker `T.isPrefixOf`
      T.stripStart line


step
  :: ParseState
  -> (Int, Text)
  -> ParseState

step st@(ParseState _ Normal _) (ln,txt)

  | Just (marker,hdr) <- fenceStart txt =
      st
        { psMode =
            InFence
              FenceState
                { fsStartLine = ln
                , fsFenceTok  = marker
                , fsHeader    = T.strip hdr
                , fsLines     = []
                }
        }

  | isIndented txt =
      st
        { psMode =
            InIndented
              IndentedState
                { isStartLine = ln
                , isLines =
                    [stripIndent txt]
                }
        }

  | otherwise =
      st

step st@(ParseState fp (InFence fs) acc) (ln,txt)

  | fenceEnd (fsFenceTok fs) txt =
      st
        { psMode = Normal
        , psBlocks =
            mkFenceBlock fp fs ln
            : acc
        }

  | otherwise =
      st
        { psMode =
            InFence
              fs
                { fsLines =
                    txt : fsLines fs
                }
        }

step st@(ParseState fp (InIndented is) acc) current@(ln,txt)

  | isIndented txt =
      st
        { psMode =
            InIndented
              is
                { isLines =
                    stripIndent txt
                    : isLines is
                }
        }

  | otherwise =
      let block =
            mkIndentedBlock
              fp
              is
              (ln - 1)

          reset =
            st
              { psMode = Normal
              , psBlocks = block : acc
              }

      in step reset current


finalize :: ParseState -> [Block]

finalize (ParseState _ Normal xs) =
  reverse xs

finalize (ParseState fp (InFence fs) xs) =
  reverse
    ( mkFenceBlock fp fs (fsStartLine fs)
    : xs
    )

finalize (ParseState fp (InIndented is) xs) =
  reverse
    ( mkIndentedBlock fp is (isStartLine is)
    : xs
    )


mkFenceBlock
  :: FilePath
  -> FenceState
  -> Int
  -> Block
mkFenceBlock fp fs endLine =
  Block
    { blockFile    = fp
    , blockNumber  = 0
    , blockKind    = Fenced
    , blockMeta    = meta
    , blockStart   = fsStartLine fs
    , blockEnd     = endLine
    , blockContent = body
    }
  where
    body =
      T.unlines
        (reverse (fsLines fs))

    meta =
      parseMetadataLine
        ("@" <> fsHeader fs)


mkIndentedBlock
  :: FilePath
  -> IndentedState
  -> Int
  -> Block
mkIndentedBlock fp is endLine =
  Block
    { blockFile    = fp
    , blockNumber  = 0
    , blockKind    = Indented
    , blockMeta    = meta
    , blockStart   = isStartLine is
    , blockEnd     = endLine
    , blockContent = code
    }
  where
    raw =
      T.unlines
        (reverse (isLines is))

    (meta, code) =
      splitIndentedMetadata raw


splitIndentedMetadata
  :: Text
  -> (Metadata, Text)
splitIndentedMetadata txt =
  case T.lines txt of

    [] ->
      (emptyMetadata, "")

    firstLine : rest
      | "@" `T.isPrefixOf` firstLine ->
          ( parseMetadataLine firstLine
          , T.unlines rest
          )

      | otherwise ->
          ( emptyMetadata
          , txt
          )