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 (blockElement cmdTypeOf)
20 (newline >> return ())
26 blockElement :: CommandTypeOf -> Parser BlockElement
27 blockElement cmdTypeOf
28 = try $ do skipMany ( comment
30 (newline >> return ())
32 foldr (<|>) pzero [ heading
34 , listElement cmdTypeOf
35 , definitionList cmdTypeOf
37 , leadingSpaced cmdTypeOf
43 heading :: Parser BlockElement
44 heading = foldr (<|>) pzero (map heading' [1..5])
48 heading' :: Int -> Parser BlockElement
49 heading' n = do try $ do count n (char '=')
50 notFollowedBy (char '=')
52 x <- notFollowedBy (char '=') >> anyChar
53 xs <- manyTill anyChar (try $ ws >> ( count n (char '=')
55 ("trailing " ++ take n (repeat '='))
60 return (Heading n (x:xs))
63 horizontalLine :: Parser BlockElement
64 horizontalLine = try ( do count 4 (char '-')
74 listElement :: CommandTypeOf -> Parser BlockElement
75 listElement cmdTypeOf = listElement' [] >>= return . List
77 listElement' :: [Char] -> Parser ListElement
81 xs <- items (stack ++ [t])
82 return (ListElement (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 Right xs ++ map Left (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 pdata :: Parser BlockElement
153 pdata = do try (string "<![PDATA[")
156 return (Preformatted [Text x])
158 pdata' :: Parser String
159 pdata' = 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
238 (try $ do (tagName, tagAttrs) <- emptyTag
239 case cmdTypeOf tagName of
240 Just BlockCommandType
241 -> return $ BlockCmd $ BlockCommand {
243 , bCmdAttributes = tagAttrs
251 contents :: Parser [BlockElement]
252 contents = do x <- try $ blockElement cmdTypeOf
256 (comment >> contents)
261 inlineElement :: CommandTypeOf -> Parser InlineElement
262 inlineElement cmdTypeOf
263 = try $ do skipMany comment
264 foldr (<|>) pzero [ cdata
265 , apostrophes cmdTypeOf
268 , inlineCmd cmdTypeOf
272 cdata :: Parser InlineElement
273 cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
275 cdata' :: Parser String
276 cdata' = do try (string "]]>")
284 text :: Parser InlineElement
287 many (noneOf ('\n':inlineSymbols))
289 return . Text . (':' :)
290 -- 定義リストとの關係上、コロンは先頭にしか來れない。
293 ( many1 (noneOf ('\n':inlineSymbols))
301 apostrophes :: CommandTypeOf -> Parser InlineElement
302 apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
304 apos1 = apos 1 >> return (Text "'")
307 xs <- many1 $ inlineElement cmdTypeOf
312 xs <- many1 $ inlineElement cmdTypeOf
316 apos4 = apos 4 >> return (Text "'")
319 xs <- many1 $ inlineElement cmdTypeOf
321 return (Italic [Bold xs])
323 apos :: Int -> Parser ()
324 apos n = count n (char '\'') >> notFollowedBy (char '\'')
327 pageLink :: Parser InlineElement
328 pageLink = do try (string "[[")
329 page <- option Nothing
330 (many1 (noneOf "#|]") >>= return . Just)
331 fragment <- option Nothing
332 (char '#' >> many1 (noneOf "|]") >>= return . Just)
333 text <- option Nothing
334 (char '|' >> many1 (satisfy (/= ']')) >>= return . Just)
336 case (page, fragment) of
337 (Nothing, Nothing) -> pzero
341 return $ PageLink page fragment text
346 inlineCmd :: CommandTypeOf -> Parser InlineElement
348 = (try $ do (tagName, tagAttrs) <- openTag
349 case cmdTypeOf tagName of
350 Just InlineCommandType
353 return $ InlineCmd $ InlineCommand {
355 , iCmdAttributes = tagAttrs
361 (try $ do (tagName, tagAttrs) <- emptyTag
362 case cmdTypeOf tagName of
363 Just InlineCommandType
364 -> return $ InlineCmd $ InlineCommand {
366 , iCmdAttributes = tagAttrs
374 contents :: Parser [InlineElement]
375 contents = do x <- inlineElement cmdTypeOf
379 (comment >> contents)
381 (newline >> contents >>= return . (Text "\n" :))
386 openTag :: Parser (String, [Attribute])
387 openTag = try $ do char '<'
391 attrs <- many $ do attr <- tagAttr
398 emptyTag :: Parser (String, [Attribute])
399 emptyTag = try $ do char '<'
403 attrs <- many $ do attr <- tagAttr
412 closeTag :: String -> Parser ()
413 closeTag name = try $ do char '<'
423 tagAttr :: Parser (String, String)
424 tagAttr = do name <- many1 letter
427 value <- many (satisfy (/= '"'))
433 comment = (try (string "<!--") >> skipTillEnd 1)
437 skipTillEnd :: Int -> Parser ()
438 skipTillEnd level = ( (try (string "<!--") >> skipTillEnd (level + 1))
440 (try (string "-->") >> case level of
442 n -> skipTillEnd (n - 1))
444 (anyChar >> skipTillEnd level)
448 blockSymbols :: [Char]
449 blockSymbols = " =-*#;<"
452 inlineSymbols :: [Char]
453 inlineSymbols = "<[:'"
457 ws = skipMany ( (oneOf " \t" >> return ())
464 eol = ( (newline >> return ())