1 module Rakka.Wiki.Parser
8 import Network.URI hiding (fragment)
10 import Text.ParserCombinators.Parsec hiding (label)
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
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 verbatim :: Parser BlockElement
154 verbatim = do try (string "<!verbatim[")
157 return (Preformatted [Text x])
159 verbatim' :: Parser String
160 verbatim' = 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 paragraph :: CommandTypeOf -> Parser BlockElement
191 paragraph cmdTypeOf = paragraph' >>= return . Paragraph
193 paragraph' :: Parser [InlineElement]
194 paragraph' = do x <- inlineElement cmdTypeOf
195 xs <- try ( do newline
198 -- \n で文字列が終はってゐたら、ここ
203 ((oneOf ('\n':blockSymbols) >> pzero) <|> return ())
204 ys <- (paragraph' <|> return [])
205 return (Text "\n" : ys)
206 -- \n があり、その次に \n または
207 -- blockSymbols があれば、fail して
208 -- 最初の newline を讀んだ所まで卷き
213 -- それ以外の場合は次の inlineElement から
214 -- を讀んで見る。但し一つも無くても良い。
216 return [] -- 全部失敗したらここで終了。
220 blockCmd :: CommandTypeOf -> Parser BlockElement
222 = (try $ do (tagName, tagAttrs) <- openTag
223 case cmdTypeOf tagName of
224 Just BlockCommandType
227 return $ BlockCmd $ BlockCommand {
229 , bCmdAttributes = tagAttrs
233 Just InlineCommandType
236 _ -> return $ undefinedCmdErr tagName
239 (try $ do (tagName, tagAttrs) <- emptyTag
240 case cmdTypeOf tagName of
241 Just BlockCommandType
242 -> return $ BlockCmd $ BlockCommand {
244 , bCmdAttributes = tagAttrs
248 Just InlineCommandType
251 _ -> return $ undefinedCmdErr tagName
256 contents :: Parser [BlockElement]
257 contents = do x <- blockElement cmdTypeOf
261 (newline >> contents)
263 (comment >> contents)
267 undefinedCmdErr :: String -> BlockElement
269 = Div [("class", "error")]
270 [ Block (Paragraph [Text ("The command `" ++ name ++ "' is not defined. " ++
271 "Make sure you haven't mistyped.")
276 inlineElement :: CommandTypeOf -> Parser InlineElement
277 inlineElement cmdTypeOf
278 = try $ do skipMany comment
279 foldr (<|>) pzero [ nowiki
280 , apostrophes cmdTypeOf
285 , inlineCmd cmdTypeOf
289 nowiki :: Parser InlineElement
290 nowiki = try (string "<!nowiki[") >> nowiki' >>= return . Text
292 nowiki' :: Parser String
293 nowiki' = do try (string "]>")
301 text :: Parser InlineElement
304 many (noneOf ('\n':inlineSymbols))
306 return . Text . (':' :)
307 -- 定義リストとの關係上、コロンは先頭にしか來れない。
310 ( many1 (noneOf ('\n':inlineSymbols))
318 apostrophes :: CommandTypeOf -> Parser InlineElement
319 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
321 apos1 = apos 1 >> return (Text "'")
324 xs <- many1 $ inlineElement cmdTypeOf
329 xs <- many1 $ inlineElement cmdTypeOf
333 apos4 = apos 4 >> return (Text "'")
336 xs <- many1 $ inlineElement cmdTypeOf
338 return (Italic [Bold xs])
340 apos :: Int -> Parser ()
341 apos n = count n (char '\'') >> notFollowedBy (char '\'')
344 objLink :: Parser InlineElement
345 objLink = do try (string "[[[")
346 page <- many1 (noneOf "|]")
347 label <- option Nothing
348 (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
350 return $ ObjectLink page label
355 pageLink :: Parser InlineElement
356 pageLink = do try (string "[[")
357 page <- option Nothing
358 (many1 (noneOf "#|]") >>= return . Just)
359 fragment <- option Nothing
360 (char '#' >> many1 (noneOf "|]") >>= return . Just)
361 label <- option Nothing
362 (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
364 case (page, fragment) of
365 (Nothing, Nothing) -> pzero
369 return $ PageLink page fragment label
374 extLink :: Parser InlineElement
375 extLink = do char '['
376 uriStr <- many1 (noneOf " \t]")
377 skipMany (oneOf " \t")
378 label <- option Nothing
379 (many1 (noneOf "]") >>= return . Just)
381 case parseURI uriStr of
382 Just uri -> char ']' >> return (ExternalLink uri label)
383 Nothing -> pzero <?> "absolute URI"
388 inlineCmd :: CommandTypeOf -> Parser InlineElement
390 = (try $ do (tagName, tagAttrs) <- openTag
391 case cmdTypeOf tagName of
392 Just InlineCommandType
395 return $ InlineCmd $ InlineCommand {
397 , iCmdAttributes = tagAttrs
403 (try $ do (tagName, tagAttrs) <- emptyTag
404 case cmdTypeOf tagName of
405 Just InlineCommandType
406 -> return $ InlineCmd $ InlineCommand {
408 , iCmdAttributes = tagAttrs
416 contents :: Parser [InlineElement]
417 contents = do x <- inlineElement cmdTypeOf
421 (comment >> contents)
423 (newline >> contents >>= return . (Text "\n" :))
428 openTag :: Parser (String, [Attribute])
429 openTag = try $ do char '<'
433 attrs <- many $ do attr <- tagAttr
440 emptyTag :: Parser (String, [Attribute])
441 emptyTag = try $ do char '<'
445 attrs <- many $ do attr <- tagAttr
454 closeTag :: String -> Parser ()
455 closeTag name = try $ do char '<'
465 tagAttr :: Parser (String, String)
466 tagAttr = do name <- many1 letter
469 value <- many (satisfy (/= '"'))
475 comment = (try (string "<!--") >> skipTillEnd 1)
479 skipTillEnd :: Int -> Parser ()
480 skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
482 (try (string "-->") >> case level of
484 n -> skipTillEnd (n - 1))
486 (anyChar >> skipTillEnd level)
490 blockSymbols :: [Char]
491 blockSymbols = " =-*#;<"
494 inlineSymbols :: [Char]
495 inlineSymbols = "<[:'"
499 ws = skipMany ( (oneOf " \t" >> return ())
506 eol = ( (newline >> return ())