1 module Rakka.Wiki.Parser
9 import Text.ParserCombinators.Parsec
12 type CommandTypeOf = String -> Maybe CommandType
15 wikiPage :: CommandTypeOf -> Parser WikiPage
17 = do xs <- many $ try (blockElement cmdTypeOf)
20 (newline >> return ())
26 blockElement :: CommandTypeOf -> Parser BlockElement
27 blockElement cmdTypeOf
30 (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 ((blockCommand >> pzero) <|> return ())
209 ys <- (paragraph' <|> return [])
210 return (Text "\n" : ys)
211 -- \n があり、その次に \n、ブロックタ
212 -- グまたは blockSymbols があれば、
213 -- fail して 最初の newline を讀んだ
218 -- それ以外の場合は次の inlineElement から
219 -- を讀んで見る。但し一つも無くても良い。
221 return [] -- 全部失敗したらここで終了。
225 inlineElement :: CommandTypeOf -> Parser InlineElement
226 inlineElement cmdTypeOf
227 = try $ do skipMany comment
228 foldr (<|>) pzero [ cdata
229 , apostrophes cmdTypeOf
232 , inlineCmd cmdTypeOf
236 cdata :: Parser InlineElement
237 cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
239 cdata' :: Parser String
240 cdata' = do try (string "]]>")
248 text :: Parser InlineElement
251 many (noneOf ('\n':inlineSymbols))
253 return . Text . (':' :)
254 -- 定義リストとの關係上、コロンは先頭にしか來れない。
257 ( many1 (noneOf ('\n':inlineSymbols))
265 apostrophes :: CommandTypeOf -> Parser InlineElement
266 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
268 apos1 = apos 1 >> return (Text "'")
271 xs <- many1 $ inlineElement cmdTypeOf
276 xs <- many1 $ inlineElement cmdTypeOf
280 apos4 = apos 4 >> return (Text "'")
283 xs <- many1 $ inlineElement cmdTypeOf
285 return (Italic [Bold xs])
287 apos :: Int -> Parser ()
288 apos n = count n (char '\'') >> notFollowedBy (char '\'')
291 pageLink :: Parser InlineElement
292 pageLink = do try (string "[[")
293 page <- option Nothing
294 (many1 (noneOf "#|]") >>= return . Just)
295 fragment <- option Nothing
296 (char '#' >> many1 (noneOf "|]") >>= return . Just)
297 text <- option Nothing
298 (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
300 case (page, fragment) of
301 (Nothing, Nothing) -> pzero
305 return $ PageLink page fragment text
310 inlineCmd :: CommandTypeOf -> Parser InlineElement
312 = (try $ do (tagName, tagAttrs) <- openTag
313 case cmdTypeOf tagName of
314 Just InlineCommandType
317 return $ InlineCmd $ InlineCommand {
319 , iCmdAttributes = tagAttrs
325 (try $ do (tagName, tagAttrs) <- emptyTag
326 case cmdTypeOf tagName of
327 Just InlineCommandType
328 -> return $ InlineCmd $ InlineCommand {
330 , iCmdAttributes = tagAttrs
336 contents :: Parser [InlineElement]
337 contents = do x <- inlineElement cmdTypeOf
341 (comment >> contents)
343 (newline >> contents >>= return . (Text "\n" :))
348 openTag :: Parser (String, [Attribute])
349 openTag = try $ do char '<'
353 attrs <- many $ do attr <- tagAttr
360 emptyTag :: Parser (String, [Attribute])
361 emptyTag = try $ do char '<'
365 attrs <- many $ do attr <- tagAttr
374 closeTag :: String -> Parser ()
375 closeTag name = try $ do char '<'
385 tagAttr :: Parser (String, String)
386 tagAttr = do name <- many1 letter
389 value <- many (satisfy (/= '"'))
395 comment = (try (string "<!--") >> skipTillEnd 1)
399 skipTillEnd :: Int -> Parser ()
400 skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
402 (try (string "-->") >> case level of
404 n -> skipTillEnd (n - 1))
406 (anyChar >> skipTillEnd level)
410 blockSymbols :: [Char]
411 blockSymbols = " =-*#;"
414 inlineSymbols :: [Char]
415 inlineSymbols = "<[:'"
419 ws = skipMany ( (oneOf " \t" >> return ())
426 eol = ( (newline >> return ())