]> gitweb @ CieloNegro.org - Rakka.git/blob - Rakka/Wiki/Parser.hs
e2e39261929c4fcd37ddde176b1c22f9ec36e59e
[Rakka.git] / Rakka / Wiki / Parser.hs
1 module Rakka.Wiki.Parser
2     ( CommandTypeOf
3     , wikiPage
4     )
5     where
6
7 import           Data.Maybe
8 import           Network.URI
9 import           Rakka.Wiki
10 import           Text.ParserCombinators.Parsec
11
12
13 type CommandTypeOf = String -> Maybe CommandType
14
15
16 wikiPage :: CommandTypeOf -> Parser WikiPage
17 wikiPage cmdTypeOf
18     = do xs <- many (blockElement cmdTypeOf)
19          skipMany ( comment
20                     <|>
21                     (newline >> return ())
22                   )
23          eof
24          return xs
25
26
27 blockElement :: CommandTypeOf -> Parser BlockElement
28 blockElement cmdTypeOf
29     = try $ do skipMany ( comment
30                           <|>
31                           (newline >> return ())
32                         )
33                foldr (<|>) pzero [ heading
34                                  , horizontalLine
35                                  , listElement cmdTypeOf
36                                  , definitionList cmdTypeOf
37                                  , pdata
38                                  , leadingSpaced cmdTypeOf
39                                  , paragraph cmdTypeOf
40                                  , blockCmd cmdTypeOf
41                                  ]
42
43
44 heading :: Parser BlockElement
45 heading = foldr (<|>) pzero (map heading' [1..5])
46           <?>
47           "heading"
48     where
49       heading' :: Int -> Parser BlockElement
50       heading' n = do try $ do count n (char '=')
51                                notFollowedBy (char '=')
52                       ws
53                       x  <- notFollowedBy (char '=') >> anyChar
54                       xs <- manyTill anyChar (try $ ws >> ( count n (char '=')
55                                                             <?>
56                                                             ("trailing " ++ take n (repeat '='))
57                                                           )
58                                              )
59                       ws
60                       eol
61                       return (Heading n (x:xs))
62
63
64 horizontalLine :: Parser BlockElement
65 horizontalLine = try ( do count 4 (char '-')
66                           many (char '-')
67                           ws
68                           eol
69                           return HorizontalLine
70                      )
71                  <?>
72                  "horizontal line"
73
74
75 listElement :: CommandTypeOf -> Parser BlockElement
76 listElement cmdTypeOf = listElement' [] >>= return . List
77     where
78       listElement' :: [Char] -> Parser ListElement
79       listElement' stack
80           = do t  <- oneOf "*#"
81                ws
82                xs <- items (stack ++ [t])
83                return (ListElement (toType t) xs)
84
85       -- ListItem の終了條件は、
86       items :: [Char] -> Parser [ListItem]
87       items stack = do xs     <- many1 $ inlineElement cmdTypeOf
88                        nested <- option Nothing
89                                  $ try $ do skipMany comment
90                                             newline
91                                             string stack
92                                             listElement' stack >>= return . Just
93                        rest <- items stack
94                        return $ (map Right xs ++ map Left (catMaybes [nested])) : rest
95                     <|>
96                     (try $ do skipMany comment
97                               newline
98                               string stack
99                               ws
100                               items stack
101                     )
102                     <|>
103                     return []
104
105       toType :: Char -> ListType
106       toType '*' = Bullet
107       toType '#' = Numbered
108
109
110 definitionList :: CommandTypeOf -> Parser BlockElement
111 definitionList cmdTypeOf = many1 definition >>= return . DefinitionList
112     where
113       definition :: Parser Definition
114       definition = do char ';'
115                       ws
116                       tHead <- inlineElement cmdTypeOf
117                       tRest <- term
118                       d     <- description
119                       return (Definition (tHead:tRest) d)
120                    <?>
121                    "definition list"
122
123       term :: Parser [InlineElement]
124       term = (char ':' >> ws >> return [])
125              <|>
126              (newline >> char ':' >> ws >> return [])
127              <|>
128              do x  <- inlineElement cmdTypeOf
129                 xs <- term
130                 return (x:xs)
131              <?>
132              "term to be defined"
133
134       description :: Parser [InlineElement]
135       description = do x  <- inlineElement cmdTypeOf
136                        xs <- description
137                        return (x:xs)
138                     <|>
139                     try ( do newline
140                              char ':'
141                              ws
142                              xs <- description
143                              return (Text "\n" : xs)
144                         )
145                     <|>
146                     (newline >> return [])
147                     <|>
148                     (eof >> return [])
149                     <?>
150                     "description of term"
151
152
153 pdata :: Parser BlockElement
154 pdata = do try (string "<![PDATA[")
155            many (oneOf " \t\n")
156            x <- pdata'
157            return (Preformatted [Text x])
158     where
159       pdata' :: Parser String
160       pdata' = do try (many (oneOf " \t\n") >> string "]]>")
161                   return []
162                <|>
163                do x  <- anyChar
164                   xs <- pdata'
165                   return (x:xs)
166
167
168 leadingSpaced :: CommandTypeOf -> Parser BlockElement
169 leadingSpaced cmdTypeOf = (char ' ' >> leadingSpaced' >>= return . Preformatted)
170                           <?>
171                           "leading space"
172     where
173       leadingSpaced' :: Parser [InlineElement]
174       leadingSpaced' = do x  <- inlineElement cmdTypeOf
175                           xs <- leadingSpaced'
176                           return (x:xs)
177                        <|>
178                        try ( newline
179                              >>
180                              char ' '
181                              >>
182                              leadingSpaced'
183                              >>=
184                              return . (Text "\n" :)
185                            )
186                        <|>
187                        return []
188
189
190 blockCommand :: Parser BlockElement
191 blockCommand = pzero -- not implemented
192
193
194 paragraph :: CommandTypeOf -> Parser BlockElement
195 paragraph cmdTypeOf = paragraph' >>= return . Paragraph
196     where
197       paragraph' :: Parser [InlineElement]
198       paragraph' = do x  <- inlineElement cmdTypeOf
199                       xs <- try ( do newline
200                                      eof
201                                      return []
202                                   -- \n で文字列が終はってゐたら、ここ
203                                   -- で終了。
204                                 )
205                             <|>
206                             try ( do newline
207                                      ((oneOf ('\n':blockSymbols) >> pzero) <|> return ())
208                                      ys <- (paragraph' <|> return [])
209                                      return (Text "\n" : ys)
210                                   -- \n があり、その次に \n または
211                                   -- blockSymbols があれば、fail して
212                                   -- 最初の newline を讀んだ所まで卷き
213                                   -- 戻す。
214                                 )
215                             <|>
216                             paragraph'
217                             -- それ以外の場合は次の inlineElement から
218                             -- を讀んで見る。但し一つも無くても良い。
219                             <|>
220                             return [] -- 全部失敗したらここで終了。
221                       return (x:xs)
222
223
224 blockCmd :: CommandTypeOf -> Parser BlockElement
225 blockCmd cmdTypeOf
226     = (try $ do (tagName, tagAttrs) <- openTag
227                 case cmdTypeOf tagName of
228                   Just BlockCommandType
229                       -> do xs <- contents
230                             closeTag tagName
231                             return $ BlockCmd $ BlockCommand {
232                                          bCmdName       = tagName
233                                        , bCmdAttributes = tagAttrs
234                                        , bCmdContents   = xs
235                                        }
236                   _   -> pzero
237       )
238       <|>
239       (try $ do (tagName, tagAttrs) <- emptyTag
240                 case cmdTypeOf tagName of
241                   Just BlockCommandType
242                       -> return $ BlockCmd $ BlockCommand {
243                                          bCmdName       = tagName
244                                        , bCmdAttributes = tagAttrs
245                                        , bCmdContents   = []
246                                        }
247                   _   -> pzero
248       )
249       <?>
250       "block command"
251     where
252       contents :: Parser [BlockElement]
253       contents = do x  <- blockElement cmdTypeOf
254                     xs <- contents
255                     return (x:xs)
256                  <|>
257                  (newline >> contents)
258                  <|>
259                  (comment >> contents)
260                  <|>
261                  return []
262
263
264 inlineElement :: CommandTypeOf -> Parser InlineElement
265 inlineElement cmdTypeOf
266     = try $ do skipMany comment
267                foldr (<|>) pzero [ cdata
268                                  , apostrophes cmdTypeOf
269                                  , text
270                                  , pageLink
271                                  , extLink
272                                  , inlineCmd cmdTypeOf
273                                  ]
274
275
276 cdata :: Parser InlineElement
277 cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
278     where
279       cdata' :: Parser String
280       cdata' = do try (string "]]>")
281                   return []
282                <|>
283                do x  <- anyChar
284                   xs <- cdata'
285                   return (x:xs)
286
287
288 text :: Parser InlineElement
289 text = ( char ':'
290          >>
291          many (noneOf ('\n':inlineSymbols))
292          >>=
293          return . Text . (':' :)
294          -- 定義リストとの關係上、コロンは先頭にしか來れない。
295        )
296        <|>
297        ( many1 (noneOf ('\n':inlineSymbols))
298          >>=
299          return . Text
300        )
301        <?>
302        "text"
303
304
305 apostrophes :: CommandTypeOf -> Parser InlineElement
306 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
307     where
308       apos1 = apos 1 >> return (Text "'")
309
310       apos2 = do apos 2
311                  xs <- many1 $ inlineElement cmdTypeOf
312                  apos 2
313                  return (Italic xs)
314
315       apos3 = do apos 3
316                  xs <- many1 $ inlineElement cmdTypeOf
317                  apos 3
318                  return (Bold xs)
319
320       apos4 = apos 4 >> return (Text "'")
321
322       apos5 = do apos 5
323                  xs <- many1 $ inlineElement cmdTypeOf
324                  apos 5
325                  return (Italic [Bold xs])
326
327       apos :: Int -> Parser ()
328       apos n = count n (char '\'') >> notFollowedBy (char '\'')
329
330
331 pageLink :: Parser InlineElement
332 pageLink = do try (string "[[")
333               page     <- option Nothing 
334                           (many1 (noneOf "#|]") >>= return . Just)
335               fragment <- option Nothing
336                           (char '#' >> many1 (noneOf "|]") >>= return . Just)
337               text     <- option Nothing
338                           (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
339
340               case (page, fragment) of
341                 (Nothing, Nothing) -> pzero
342                 (_, _)             -> return ()
343
344               string "]]"
345               return $ PageLink page fragment text
346            <?>
347            "page link"
348
349
350 extLink :: Parser InlineElement
351 extLink = do char '['
352              uriStr <- many1 (noneOf " \t]")
353              skipMany (oneOf " \t")
354              text <- option Nothing
355                      (many1 (noneOf "]") >>= return . Just)
356              
357              case parseURI uriStr of
358                Just uri -> char ']' >> return (ExternalLink uri text)
359                Nothing  -> pzero <?> "absolute URI"
360           <?>
361           "external link"
362
363
364 inlineCmd :: CommandTypeOf -> Parser InlineElement
365 inlineCmd cmdTypeOf
366     = (try $ do (tagName, tagAttrs) <- openTag
367                 case cmdTypeOf tagName of
368                   Just InlineCommandType
369                       -> do xs <- contents
370                             closeTag tagName
371                             return $ InlineCmd $ InlineCommand {
372                                          iCmdName       = tagName
373                                        , iCmdAttributes = tagAttrs
374                                        , iCmdContents   = xs
375                                        }
376                   _   -> pzero
377       )
378       <|>
379       (try $ do (tagName, tagAttrs) <- emptyTag
380                 case cmdTypeOf tagName of
381                   Just InlineCommandType
382                       -> return $ InlineCmd $ InlineCommand {
383                                          iCmdName       = tagName
384                                        , iCmdAttributes = tagAttrs
385                                        , iCmdContents   = []
386                                        }
387                   _   -> pzero
388       )
389       <?>
390       "inline command"
391     where
392       contents :: Parser [InlineElement]
393       contents = do x  <- inlineElement cmdTypeOf
394                     xs <- contents
395                     return (x:xs)
396                  <|>
397                  (comment >> contents)
398                  <|>
399                  (newline >> contents >>= return . (Text "\n" :))
400                  <|>
401                  return []
402
403
404 openTag :: Parser (String, [Attribute])
405 openTag = try $ do char '<'
406                    many space
407                    name  <- many1 letter
408                    many space
409                    attrs <- many $ do attr <- tagAttr
410                                       many space
411                                       return attr
412                    char '>'
413                    return (name, attrs)
414
415
416 emptyTag :: Parser (String, [Attribute])
417 emptyTag = try $ do char '<'
418                     many space
419                     name  <- many1 letter
420                     many space
421                     attrs <- many $ do attr <- tagAttr
422                                        many space
423                                        return attr
424                     char '/'
425                     many space
426                     char '>'
427                     return (name, attrs)
428
429
430 closeTag :: String -> Parser ()
431 closeTag name = try $ do char '<'
432                          many space
433                          char '/'
434                          many space
435                          string name
436                          many space
437                          char '>'
438                          return ()
439
440
441 tagAttr :: Parser (String, String)
442 tagAttr = do name  <- many1 letter
443              char '='
444              char '"'
445              value <- many (satisfy (/= '"'))
446              char '"'
447              return (name, value)
448
449
450 comment :: Parser ()
451 comment = (try (string "<!--") >> skipTillEnd 1)
452           <?>
453           "comment"
454     where
455       skipTillEnd :: Int -> Parser ()
456       skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
457                             <|>
458                             (try (string "-->") >> case level of
459                                                      1 -> return ()
460                                                      n -> skipTillEnd (n - 1))
461                             <|>
462                             (anyChar >> skipTillEnd level)
463                           )
464
465
466 blockSymbols :: [Char]
467 blockSymbols = " =-*#;<"
468
469
470 inlineSymbols :: [Char]
471 inlineSymbols = "<[:'"
472
473 -- white space
474 ws :: Parser ()
475 ws = skipMany ( (oneOf " \t" >> return ())
476                 <|>
477                 comment
478               )
479
480 -- end of line
481 eol :: Parser ()
482 eol = ( (newline >> return ())
483         <|>
484         eof
485       )