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
239 (try $ do (tagName, tagAttrs) <- emptyTag
240 case cmdTypeOf tagName of
241 Just BlockCommandType
242 -> return $ BlockCmd $ BlockCommand {
244 , bCmdAttributes = tagAttrs
252 contents :: Parser [BlockElement]
253 contents = do x <- blockElement cmdTypeOf
257 (newline >> contents)
259 (comment >> contents)
264 inlineElement :: CommandTypeOf -> Parser InlineElement
265 inlineElement cmdTypeOf
266 = try $ do skipMany comment
267 foldr (<|>) pzero [ cdata
268 , apostrophes cmdTypeOf
272 , inlineCmd cmdTypeOf
276 cdata :: Parser InlineElement
277 cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
279 cdata' :: Parser String
280 cdata' = do try (string "]]>")
288 text :: Parser InlineElement
291 many (noneOf ('\n':inlineSymbols))
293 return . Text . (':' :)
294 -- 定義リストとの關係上、コロンは先頭にしか來れない。
297 ( many1 (noneOf ('\n':inlineSymbols))
305 apostrophes :: CommandTypeOf -> Parser InlineElement
306 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
308 apos1 = apos 1 >> return (Text "'")
311 xs <- many1 $ inlineElement cmdTypeOf
316 xs <- many1 $ inlineElement cmdTypeOf
320 apos4 = apos 4 >> return (Text "'")
323 xs <- many1 $ inlineElement cmdTypeOf
325 return (Italic [Bold xs])
327 apos :: Int -> Parser ()
328 apos n = count n (char '\'') >> notFollowedBy (char '\'')
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)
340 case (page, fragment) of
341 (Nothing, Nothing) -> pzero
345 return $ PageLink page fragment text
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)
357 case parseURI uriStr of
358 Just uri -> char ']' >> return (ExternalLink uri text)
359 Nothing -> pzero <?> "absolute URI"
364 inlineCmd :: CommandTypeOf -> Parser InlineElement
366 = (try $ do (tagName, tagAttrs) <- openTag
367 case cmdTypeOf tagName of
368 Just InlineCommandType
371 return $ InlineCmd $ InlineCommand {
373 , iCmdAttributes = tagAttrs
379 (try $ do (tagName, tagAttrs) <- emptyTag
380 case cmdTypeOf tagName of
381 Just InlineCommandType
382 -> return $ InlineCmd $ InlineCommand {
384 , iCmdAttributes = tagAttrs
392 contents :: Parser [InlineElement]
393 contents = do x <- inlineElement cmdTypeOf
397 (comment >> contents)
399 (newline >> contents >>= return . (Text "\n" :))
404 openTag :: Parser (String, [Attribute])
405 openTag = try $ do char '<'
409 attrs <- many $ do attr <- tagAttr
416 emptyTag :: Parser (String, [Attribute])
417 emptyTag = try $ do char '<'
421 attrs <- many $ do attr <- tagAttr
430 closeTag :: String -> Parser ()
431 closeTag name = try $ do char '<'
441 tagAttr :: Parser (String, String)
442 tagAttr = do name <- many1 letter
445 value <- many (satisfy (/= '"'))
451 comment = (try (string "<!--") >> skipTillEnd 1)
455 skipTillEnd :: Int -> Parser ()
456 skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
458 (try (string "-->") >> case level of
460 n -> skipTillEnd (n - 1))
462 (anyChar >> skipTillEnd level)
466 blockSymbols :: [Char]
467 blockSymbols = " =-*#;<"
470 inlineSymbols :: [Char]
471 inlineSymbols = "<[:'"
475 ws = skipMany ( (oneOf " \t" >> return ())
482 eol = ( (newline >> return ())