1 module Rakka.Wiki.Parser
10 import Text.ParserCombinators.Parsec
13 type CommandTypeOf = String -> Maybe CommandType
16 wikiPage :: CommandTypeOf -> Parser WikiPage
18 = do xs <- many (blockElement cmdTypeOf)
21 (newline >> return ())
27 blockElement :: CommandTypeOf -> Parser BlockElement
28 blockElement cmdTypeOf
29 = try $ do skipMany ( comment
31 (newline >> return ())
33 foldr (<|>) pzero [ heading
35 , listElement cmdTypeOf
36 , definitionList cmdTypeOf
38 , leadingSpaced cmdTypeOf
44 heading :: Parser BlockElement
45 heading = foldr (<|>) pzero (map heading' [1..5])
49 heading' :: Int -> Parser BlockElement
50 heading' n = do try $ do count n (char '=')
51 notFollowedBy (char '=')
53 x <- notFollowedBy (char '=') >> anyChar
54 xs <- manyTill anyChar (try $ ws >> ( count n (char '=')
56 ("trailing " ++ take n (repeat '='))
61 return (Heading n (x:xs))
64 horizontalLine :: Parser BlockElement
65 horizontalLine = try ( do count 4 (char '-')
75 listElement :: CommandTypeOf -> Parser BlockElement
76 listElement cmdTypeOf = listElement' [] >>= return . List
78 listElement' :: [Char] -> Parser ListElement
82 xs <- items (stack ++ [t])
83 return (ListElement (toType t) xs)
86 items :: [Char] -> Parser [ListItem]
87 items stack = do xs <- many1 $ inlineElement cmdTypeOf
88 nested <- option Nothing
89 $ try $ do skipMany comment
92 listElement' stack >>= return . Just
94 return $ (map Right xs ++ map Left (catMaybes [nested])) : rest
96 (try $ do skipMany comment
105 toType :: Char -> ListType
107 toType '#' = Numbered
110 definitionList :: CommandTypeOf -> Parser BlockElement
111 definitionList cmdTypeOf = many1 definition >>= return . DefinitionList
113 definition :: Parser Definition
114 definition = do char ';'
116 tHead <- inlineElement cmdTypeOf
119 return (Definition (tHead:tRest) d)
123 term :: Parser [InlineElement]
124 term = (char ':' >> ws >> return [])
126 (newline >> char ':' >> ws >> return [])
128 do x <- inlineElement cmdTypeOf
134 description :: Parser [InlineElement]
135 description = do x <- inlineElement cmdTypeOf
143 return (Text "\n" : xs)
146 (newline >> return [])
150 "description of term"
153 pdata :: Parser BlockElement
154 pdata = do try (string "<![PDATA[")
157 return (Preformatted [Text x])
159 pdata' :: Parser String
160 pdata' = do try (many (oneOf " \t\n") >> string "]]>")
168 leadingSpaced :: CommandTypeOf -> Parser BlockElement
169 leadingSpaced cmdTypeOf = (char ' ' >> leadingSpaced' >>= return . Preformatted)
173 leadingSpaced' :: Parser [InlineElement]
174 leadingSpaced' = do x <- inlineElement cmdTypeOf
184 return . (Text "\n" :)
190 blockCommand :: Parser BlockElement
191 blockCommand = pzero -- not implemented
194 paragraph :: CommandTypeOf -> Parser BlockElement
195 paragraph cmdTypeOf = paragraph' >>= return . Paragraph
197 paragraph' :: Parser [InlineElement]
198 paragraph' = do x <- inlineElement cmdTypeOf
199 xs <- try ( do newline
202 -- \n で文字列が終はってゐたら、ここ
207 ((oneOf ('\n':blockSymbols) >> pzero) <|> return ())
208 ys <- (paragraph' <|> return [])
209 return (Text "\n" : ys)
210 -- \n があり、その次に \n または
211 -- blockSymbols があれば、fail して
212 -- 最初の newline を讀んだ所まで卷き
217 -- それ以外の場合は次の inlineElement から
218 -- を讀んで見る。但し一つも無くても良い。
220 return [] -- 全部失敗したらここで終了。
224 blockCmd :: CommandTypeOf -> Parser BlockElement
226 = (try $ do (tagName, tagAttrs) <- openTag
227 case cmdTypeOf tagName of
228 Just BlockCommandType
231 return $ BlockCmd $ BlockCommand {
233 , bCmdAttributes = tagAttrs
237 Just InlineCommandType
240 _ -> return $ undefinedCmdErr tagName
243 (try $ do (tagName, tagAttrs) <- emptyTag
244 case cmdTypeOf tagName of
245 Just BlockCommandType
246 -> return $ BlockCmd $ BlockCommand {
248 , bCmdAttributes = tagAttrs
252 Just InlineCommandType
255 _ -> return $ undefinedCmdErr tagName
260 contents :: Parser [BlockElement]
261 contents = do x <- blockElement cmdTypeOf
265 (newline >> contents)
267 (comment >> contents)
271 undefinedCmdErr :: String -> BlockElement
273 = Div [("class", "error")]
274 [ Paragraph [Text ("The command `" ++ name ++ "' is not defined. " ++
275 "Make sure you haven't mistyped.")
280 inlineElement :: CommandTypeOf -> Parser InlineElement
281 inlineElement cmdTypeOf
282 = try $ do skipMany comment
283 foldr (<|>) pzero [ cdata
284 , apostrophes cmdTypeOf
288 , inlineCmd cmdTypeOf
292 cdata :: Parser InlineElement
293 cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
295 cdata' :: Parser String
296 cdata' = do try (string "]]>")
304 text :: Parser InlineElement
307 many (noneOf ('\n':inlineSymbols))
309 return . Text . (':' :)
310 -- 定義リストとの關係上、コロンは先頭にしか來れない。
313 ( many1 (noneOf ('\n':inlineSymbols))
321 apostrophes :: CommandTypeOf -> Parser InlineElement
322 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
324 apos1 = apos 1 >> return (Text "'")
327 xs <- many1 $ inlineElement cmdTypeOf
332 xs <- many1 $ inlineElement cmdTypeOf
336 apos4 = apos 4 >> return (Text "'")
339 xs <- many1 $ inlineElement cmdTypeOf
341 return (Italic [Bold xs])
343 apos :: Int -> Parser ()
344 apos n = count n (char '\'') >> notFollowedBy (char '\'')
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)
356 case (page, fragment) of
357 (Nothing, Nothing) -> pzero
361 return $ PageLink page fragment text
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)
373 case parseURI uriStr of
374 Just uri -> char ']' >> return (ExternalLink uri text)
375 Nothing -> pzero <?> "absolute URI"
380 inlineCmd :: CommandTypeOf -> Parser InlineElement
382 = (try $ do (tagName, tagAttrs) <- openTag
383 case cmdTypeOf tagName of
384 Just InlineCommandType
387 return $ InlineCmd $ InlineCommand {
389 , iCmdAttributes = tagAttrs
395 (try $ do (tagName, tagAttrs) <- emptyTag
396 case cmdTypeOf tagName of
397 Just InlineCommandType
398 -> return $ InlineCmd $ InlineCommand {
400 , iCmdAttributes = tagAttrs
408 contents :: Parser [InlineElement]
409 contents = do x <- inlineElement cmdTypeOf
413 (comment >> contents)
415 (newline >> contents >>= return . (Text "\n" :))
420 openTag :: Parser (String, [Attribute])
421 openTag = try $ do char '<'
425 attrs <- many $ do attr <- tagAttr
432 emptyTag :: Parser (String, [Attribute])
433 emptyTag = try $ do char '<'
437 attrs <- many $ do attr <- tagAttr
446 closeTag :: String -> Parser ()
447 closeTag name = try $ do char '<'
457 tagAttr :: Parser (String, String)
458 tagAttr = do name <- many1 letter
461 value <- many (satisfy (/= '"'))
467 comment = (try (string "<!--") >> skipTillEnd 1)
471 skipTillEnd :: Int -> Parser ()
472 skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
474 (try (string "-->") >> case level of
476 n -> skipTillEnd (n - 1))
478 (anyChar >> skipTillEnd level)
482 blockSymbols :: [Char]
483 blockSymbols = " =-*#;<"
486 inlineSymbols :: [Char]
487 inlineSymbols = "<[:'"
491 ws = skipMany ( (oneOf " \t" >> return ())
498 eol = ( (newline >> return ())