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' []
78 listElement' :: [Char] -> Parser BlockElement
82 xs <- items (stack ++ [t])
83 return (List (toType t) xs)
85 items :: [Char] -> Parser [ListItem]
86 items stack = do xs <- many1 $ inlineElement cmdTypeOf
87 nested <- option Nothing
88 $ try $ do skipMany comment
91 listElement' stack >>= return . Just
93 return $ (map Inline xs ++ map Block (catMaybes [nested])) : rest
95 (try $ do skipMany comment
104 toType :: Char -> ListType
106 toType '#' = Numbered
109 definitionList :: CommandTypeOf -> Parser BlockElement
110 definitionList cmdTypeOf = many1 definition >>= return . DefinitionList
112 definition :: Parser Definition
113 definition = do char ';'
115 tHead <- inlineElement cmdTypeOf
118 return (Definition (tHead:tRest) d)
122 term :: Parser [InlineElement]
123 term = (char ':' >> ws >> return [])
125 (newline >> char ':' >> ws >> return [])
127 do x <- inlineElement cmdTypeOf
133 description :: Parser [InlineElement]
134 description = do x <- inlineElement cmdTypeOf
142 return (Text "\n" : xs)
145 (newline >> return [])
149 "description of term"
152 verbatim :: Parser BlockElement
153 verbatim = do try (string "<!verbatim[")
156 return (Preformatted [Text x])
158 verbatim' :: Parser String
159 verbatim' = do try (many (oneOf " \t\n") >> string "]>")
167 leadingSpaced :: CommandTypeOf -> Parser BlockElement
168 leadingSpaced cmdTypeOf = (char ' ' >> leadingSpaced' >>= return . Preformatted)
172 leadingSpaced' :: Parser [InlineElement]
173 leadingSpaced' = do x <- inlineElement cmdTypeOf
183 return . (Text "\n" :)
189 blockCommand :: Parser BlockElement
190 blockCommand = pzero -- not implemented
193 paragraph :: CommandTypeOf -> Parser BlockElement
194 paragraph cmdTypeOf = paragraph' >>= return . Paragraph
196 paragraph' :: Parser [InlineElement]
197 paragraph' = do x <- inlineElement cmdTypeOf
198 xs <- try ( do newline
201 -- \n で文字列が終はってゐたら、ここ
206 ((oneOf ('\n':blockSymbols) >> pzero) <|> return ())
207 ys <- (paragraph' <|> return [])
208 return (Text "\n" : ys)
209 -- \n があり、その次に \n または
210 -- blockSymbols があれば、fail して
211 -- 最初の newline を讀んだ所まで卷き
216 -- それ以外の場合は次の inlineElement から
217 -- を讀んで見る。但し一つも無くても良い。
219 return [] -- 全部失敗したらここで終了。
223 blockCmd :: CommandTypeOf -> Parser BlockElement
225 = (try $ do (tagName, tagAttrs) <- openTag
226 case cmdTypeOf tagName of
227 Just BlockCommandType
230 return $ BlockCmd $ BlockCommand {
232 , bCmdAttributes = tagAttrs
236 Just InlineCommandType
239 _ -> return $ undefinedCmdErr tagName
242 (try $ do (tagName, tagAttrs) <- emptyTag
243 case cmdTypeOf tagName of
244 Just BlockCommandType
245 -> return $ BlockCmd $ BlockCommand {
247 , bCmdAttributes = tagAttrs
251 Just InlineCommandType
254 _ -> return $ undefinedCmdErr tagName
259 contents :: Parser [BlockElement]
260 contents = do x <- blockElement cmdTypeOf
264 (newline >> contents)
266 (comment >> contents)
270 undefinedCmdErr :: String -> BlockElement
272 = Div [("class", "error")]
273 [ Block (Paragraph [Text ("The command `" ++ name ++ "' is not defined. " ++
274 "Make sure you haven't mistyped.")
279 inlineElement :: CommandTypeOf -> Parser InlineElement
280 inlineElement cmdTypeOf
281 = try $ do skipMany comment
282 foldr (<|>) pzero [ nowiki
283 , apostrophes cmdTypeOf
288 , inlineCmd cmdTypeOf
292 nowiki :: Parser InlineElement
293 nowiki = try (string "<!nowiki[") >> nowiki' >>= return . Text
295 nowiki' :: Parser String
296 nowiki' = 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 objLink :: Parser InlineElement
348 objLink = do try (string "[[[")
349 page <- many1 (noneOf "|]")
350 text <- option Nothing
351 (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
353 return $ ObjectLink page text
358 pageLink :: Parser InlineElement
359 pageLink = do try (string "[[")
360 page <- option Nothing
361 (many1 (noneOf "#|]") >>= return . Just)
362 fragment <- option Nothing
363 (char '#' >> many1 (noneOf "|]") >>= return . Just)
364 text <- option Nothing
365 (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
367 case (page, fragment) of
368 (Nothing, Nothing) -> pzero
372 return $ PageLink page fragment text
377 extLink :: Parser InlineElement
378 extLink = do char '['
379 uriStr <- many1 (noneOf " \t]")
380 skipMany (oneOf " \t")
381 text <- option Nothing
382 (many1 (noneOf "]") >>= return . Just)
384 case parseURI uriStr of
385 Just uri -> char ']' >> return (ExternalLink uri text)
386 Nothing -> pzero <?> "absolute URI"
391 inlineCmd :: CommandTypeOf -> Parser InlineElement
393 = (try $ do (tagName, tagAttrs) <- openTag
394 case cmdTypeOf tagName of
395 Just InlineCommandType
398 return $ InlineCmd $ InlineCommand {
400 , iCmdAttributes = tagAttrs
406 (try $ do (tagName, tagAttrs) <- emptyTag
407 case cmdTypeOf tagName of
408 Just InlineCommandType
409 -> return $ InlineCmd $ InlineCommand {
411 , iCmdAttributes = tagAttrs
419 contents :: Parser [InlineElement]
420 contents = do x <- inlineElement cmdTypeOf
424 (comment >> contents)
426 (newline >> contents >>= return . (Text "\n" :))
431 openTag :: Parser (String, [Attribute])
432 openTag = try $ do char '<'
436 attrs <- many $ do attr <- tagAttr
443 emptyTag :: Parser (String, [Attribute])
444 emptyTag = try $ do char '<'
448 attrs <- many $ do attr <- tagAttr
457 closeTag :: String -> Parser ()
458 closeTag name = try $ do char '<'
468 tagAttr :: Parser (String, String)
469 tagAttr = do name <- many1 letter
472 value <- many (satisfy (/= '"'))
478 comment = (try (string "<!--") >> skipTillEnd 1)
482 skipTillEnd :: Int -> Parser ()
483 skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
485 (try (string "-->") >> case level of
487 n -> skipTillEnd (n - 1))
489 (anyChar >> skipTillEnd level)
493 blockSymbols :: [Char]
494 blockSymbols = " =-*#;<"
497 inlineSymbols :: [Char]
498 inlineSymbols = "<[:'"
502 ws = skipMany ( (oneOf " \t" >> return ())
509 eol = ( (newline >> return ())