]> gitweb @ CieloNegro.org - Rakka.git/blob - Rakka/Wiki/Parser.hs
Implemented dummy trackback commands
[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
237                   Just InlineCommandType
238                       -> pzero
239
240                   _   -> return $ undefinedCmdErr tagName
241       )
242       <|>
243       (try $ do (tagName, tagAttrs) <- emptyTag
244                 case cmdTypeOf tagName of
245                   Just BlockCommandType
246                       -> return $ BlockCmd $ BlockCommand {
247                                          bCmdName       = tagName
248                                        , bCmdAttributes = tagAttrs
249                                        , bCmdContents   = []
250                                        }
251
252                   Just InlineCommandType
253                       -> pzero
254
255                   _   -> return $ undefinedCmdErr tagName
256       )
257       <?>
258       "block command"
259     where
260       contents :: Parser [BlockElement]
261       contents = do x  <- blockElement cmdTypeOf
262                     xs <- contents
263                     return (x:xs)
264                  <|>
265                  (newline >> contents)
266                  <|>
267                  (comment >> contents)
268                  <|>
269                  return []
270
271       undefinedCmdErr :: String -> BlockElement
272       undefinedCmdErr name
273           = Div [("class", "error")]
274             [ Paragraph [Text ("The command `" ++ name ++ "' is not defined. " ++
275                                "Make sure you haven't mistyped.")
276                         ]
277             ]
278
279
280 inlineElement :: CommandTypeOf -> Parser InlineElement
281 inlineElement cmdTypeOf
282     = try $ do skipMany comment
283                foldr (<|>) pzero [ cdata
284                                  , apostrophes cmdTypeOf
285                                  , text
286                                  , pageLink
287                                  , extLink
288                                  , inlineCmd cmdTypeOf
289                                  ]
290
291
292 cdata :: Parser InlineElement
293 cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
294     where
295       cdata' :: Parser String
296       cdata' = do try (string "]]>")
297                   return []
298                <|>
299                do x  <- anyChar
300                   xs <- cdata'
301                   return (x:xs)
302
303
304 text :: Parser InlineElement
305 text = ( char ':'
306          >>
307          many (noneOf ('\n':inlineSymbols))
308          >>=
309          return . Text . (':' :)
310          -- 定義リストとの關係上、コロンは先頭にしか來れない。
311        )
312        <|>
313        ( many1 (noneOf ('\n':inlineSymbols))
314          >>=
315          return . Text
316        )
317        <?>
318        "text"
319
320
321 apostrophes :: CommandTypeOf -> Parser InlineElement
322 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
323     where
324       apos1 = apos 1 >> return (Text "'")
325
326       apos2 = do apos 2
327                  xs <- many1 $ inlineElement cmdTypeOf
328                  apos 2
329                  return (Italic xs)
330
331       apos3 = do apos 3
332                  xs <- many1 $ inlineElement cmdTypeOf
333                  apos 3
334                  return (Bold xs)
335
336       apos4 = apos 4 >> return (Text "'")
337
338       apos5 = do apos 5
339                  xs <- many1 $ inlineElement cmdTypeOf
340                  apos 5
341                  return (Italic [Bold xs])
342
343       apos :: Int -> Parser ()
344       apos n = count n (char '\'') >> notFollowedBy (char '\'')
345
346
347 pageLink :: Parser InlineElement
348 pageLink = do try (string "[[")
349               page     <- option Nothing 
350                           (many1 (noneOf "#|]") >>= return . Just)
351               fragment <- option Nothing
352                           (char '#' >> many1 (noneOf "|]") >>= return . Just)
353               text     <- option Nothing
354                           (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
355
356               case (page, fragment) of
357                 (Nothing, Nothing) -> pzero
358                 (_, _)             -> return ()
359
360               string "]]"
361               return $ PageLink page fragment text
362            <?>
363            "page link"
364
365
366 extLink :: Parser InlineElement
367 extLink = do char '['
368              uriStr <- many1 (noneOf " \t]")
369              skipMany (oneOf " \t")
370              text <- option Nothing
371                      (many1 (noneOf "]") >>= return . Just)
372              
373              case parseURI uriStr of
374                Just uri -> char ']' >> return (ExternalLink uri text)
375                Nothing  -> pzero <?> "absolute URI"
376           <?>
377           "external link"
378
379
380 inlineCmd :: CommandTypeOf -> Parser InlineElement
381 inlineCmd cmdTypeOf
382     = (try $ do (tagName, tagAttrs) <- openTag
383                 case cmdTypeOf tagName of
384                   Just InlineCommandType
385                       -> do xs <- contents
386                             closeTag tagName
387                             return $ InlineCmd $ InlineCommand {
388                                          iCmdName       = tagName
389                                        , iCmdAttributes = tagAttrs
390                                        , iCmdContents   = xs
391                                        }
392                   _   -> pzero
393       )
394       <|>
395       (try $ do (tagName, tagAttrs) <- emptyTag
396                 case cmdTypeOf tagName of
397                   Just InlineCommandType
398                       -> return $ InlineCmd $ InlineCommand {
399                                          iCmdName       = tagName
400                                        , iCmdAttributes = tagAttrs
401                                        , iCmdContents   = []
402                                        }
403                   _   -> pzero
404       )
405       <?>
406       "inline command"
407     where
408       contents :: Parser [InlineElement]
409       contents = do x  <- inlineElement cmdTypeOf
410                     xs <- contents
411                     return (x:xs)
412                  <|>
413                  (comment >> contents)
414                  <|>
415                  (newline >> contents >>= return . (Text "\n" :))
416                  <|>
417                  return []
418
419
420 openTag :: Parser (String, [Attribute])
421 openTag = try $ do char '<'
422                    many space
423                    name  <- many1 letter
424                    many space
425                    attrs <- many $ do attr <- tagAttr
426                                       many space
427                                       return attr
428                    char '>'
429                    return (name, attrs)
430
431
432 emptyTag :: Parser (String, [Attribute])
433 emptyTag = try $ do char '<'
434                     many space
435                     name  <- many1 letter
436                     many space
437                     attrs <- many $ do attr <- tagAttr
438                                        many space
439                                        return attr
440                     char '/'
441                     many space
442                     char '>'
443                     return (name, attrs)
444
445
446 closeTag :: String -> Parser ()
447 closeTag name = try $ do char '<'
448                          many space
449                          char '/'
450                          many space
451                          string name
452                          many space
453                          char '>'
454                          return ()
455
456
457 tagAttr :: Parser (String, String)
458 tagAttr = do name  <- many1 letter
459              char '='
460              char '"'
461              value <- many (satisfy (/= '"'))
462              char '"'
463              return (name, value)
464
465
466 comment :: Parser ()
467 comment = (try (string "<!--") >> skipTillEnd 1)
468           <?>
469           "comment"
470     where
471       skipTillEnd :: Int -> Parser ()
472       skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
473                             <|>
474                             (try (string "-->") >> case level of
475                                                      1 -> return ()
476                                                      n -> skipTillEnd (n - 1))
477                             <|>
478                             (anyChar >> skipTillEnd level)
479                           )
480
481
482 blockSymbols :: [Char]
483 blockSymbols = " =-*#;<"
484
485
486 inlineSymbols :: [Char]
487 inlineSymbols = "<[:'"
488
489 -- white space
490 ws :: Parser ()
491 ws = skipMany ( (oneOf " \t" >> return ())
492                 <|>
493                 comment
494               )
495
496 -- end of line
497 eol :: Parser ()
498 eol = ( (newline >> return ())
499         <|>
500         eof
501       )