X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Rakka%2FWiki%2FParser.hs;h=19170b1fb1a941a9709968d24b2ae007c7563d37;hb=a4b849476ee3a204ea91dee8f3fd235b0b33a10a;hp=6e4edeb4bc5c604aab1f6e97c0080471c1ebeff4;hpb=e43bb104a7313dd696b8bb8aa3bafff94706a187;p=Rakka.git diff --git a/Rakka/Wiki/Parser.hs b/Rakka/Wiki/Parser.hs index 6e4edeb..19170b1 100644 --- a/Rakka/Wiki/Parser.hs +++ b/Rakka/Wiki/Parser.hs @@ -4,10 +4,11 @@ module Rakka.Wiki.Parser ) where +import Control.Monad import Data.Maybe -import Network.URI +import Network.URI hiding (fragment) import Rakka.Wiki -import Text.ParserCombinators.Parsec +import Text.ParserCombinators.Parsec hiding (label) type CommandTypeOf = String -> Maybe CommandType @@ -34,7 +35,7 @@ blockElement cmdTypeOf , horizontalLine , listElement cmdTypeOf , definitionList cmdTypeOf - , pdata + , verbatim , leadingSpaced cmdTypeOf , paragraph cmdTypeOf , blockCmd cmdTypeOf @@ -53,7 +54,7 @@ heading = foldr (<|>) pzero (map heading' [1..5]) x <- notFollowedBy (char '=') >> anyChar xs <- manyTill anyChar (try $ ws >> ( count n (char '=') - ("trailing " ++ take n (repeat '=')) + ("trailing " ++ replicate n '=') ) ) ws @@ -88,7 +89,7 @@ listElement cmdTypeOf = listElement' [] $ try $ do skipMany comment newline string stack - listElement' stack >>= return . Just + liftM Just (listElement' stack) rest <- items stack return $ (map Inline xs ++ map Block (catMaybes [nested])) : rest <|> @@ -104,10 +105,11 @@ listElement cmdTypeOf = listElement' [] toType :: Char -> ListType toType '*' = Bullet toType '#' = Numbered + toType _ = undefined definitionList :: CommandTypeOf -> Parser BlockElement -definitionList cmdTypeOf = many1 definition >>= return . DefinitionList +definitionList cmdTypeOf = liftM DefinitionList (many1 definition) where definition :: Parser Definition definition = do char ';' @@ -149,23 +151,23 @@ definitionList cmdTypeOf = many1 definition >>= return . DefinitionList "description of term" -pdata :: Parser BlockElement -pdata = do try (string "> string "]]>") - return [] - <|> - do x <- anyChar - xs <- pdata' - return (x:xs) + verbatim' :: Parser String + verbatim' = do try (many (oneOf " \t\n") >> string "]>") + return [] + <|> + do x <- anyChar + xs <- verbatim' + return (x:xs) leadingSpaced :: CommandTypeOf -> Parser BlockElement -leadingSpaced cmdTypeOf = (char ' ' >> leadingSpaced' >>= return . Preformatted) +leadingSpaced cmdTypeOf = liftM Preformatted (char ' ' >> leadingSpaced') "leading space" where @@ -174,24 +176,19 @@ leadingSpaced cmdTypeOf = (char ' ' >> leadingSpaced' >>= return . Preformatted) xs <- leadingSpaced' return (x:xs) <|> - try ( newline - >> - char ' ' - >> - leadingSpaced' - >>= - return . (Text "\n" :) + try ( liftM (Text "\n" :) ( newline + >> + char ' ' + >> + leadingSpaced' + ) ) <|> return [] -blockCommand :: Parser BlockElement -blockCommand = pzero -- not implemented - - paragraph :: CommandTypeOf -> Parser BlockElement -paragraph cmdTypeOf = paragraph' >>= return . Paragraph +paragraph cmdTypeOf = liftM Paragraph paragraph' where paragraph' :: Parser [InlineElement] paragraph' = do x <- inlineElement cmdTypeOf @@ -227,7 +224,7 @@ blockCmd cmdTypeOf Just BlockCommandType -> do xs <- contents closeTag tagName - return $ BlockCmd $ BlockCommand { + return $ BlockCmd BlockCommand { bCmdName = tagName , bCmdAttributes = tagAttrs , bCmdContents = xs @@ -242,7 +239,7 @@ blockCmd cmdTypeOf (try $ do (tagName, tagAttrs) <- emptyTag case cmdTypeOf tagName of Just BlockCommandType - -> return $ BlockCmd $ BlockCommand { + -> return $ BlockCmd BlockCommand { bCmdName = tagName , bCmdAttributes = tagAttrs , bCmdContents = [] @@ -279,7 +276,7 @@ blockCmd cmdTypeOf inlineElement :: CommandTypeOf -> Parser InlineElement inlineElement cmdTypeOf = try $ do skipMany comment - foldr (<|>) pzero [ cdata + foldr (<|>) pzero [ nowiki , apostrophes cmdTypeOf , text , objLink @@ -289,31 +286,26 @@ inlineElement cmdTypeOf ] -cdata :: Parser InlineElement -cdata = try (string "> cdata' >>= return . Text +nowiki :: Parser InlineElement +nowiki = liftM Text (try (string "> nowiki') where - cdata' :: Parser String - cdata' = do try (string "]]>") - return [] - <|> - do x <- anyChar - xs <- cdata' - return (x:xs) + nowiki' :: Parser String + nowiki' = do try (string "]>") + return [] + <|> + do x <- anyChar + xs <- nowiki' + return (x:xs) text :: Parser InlineElement -text = ( char ':' - >> - many (noneOf ('\n':inlineSymbols)) - >>= - return . Text . (':' :) - -- 定義リストとの關係上、コロンは先頭にしか來れない。 - ) +text = liftM (Text . (':' :)) ( char ':' + >> + many (noneOf ('\n':inlineSymbols)) + ) + -- 定義リストとの關係上、コロンは先頭にしか來られない。 <|> - ( many1 (noneOf ('\n':inlineSymbols)) - >>= - return . Text - ) + liftM Text (many1 (noneOf ('\n':inlineSymbols))) "text" @@ -346,11 +338,11 @@ apostrophes cmdTypeOf = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, objLink :: Parser InlineElement objLink = do try (string "[[[") - page <- many1 (noneOf "|]") - text <- option Nothing - (char '|' >> many1 (satisfy (/= ']')) >>= return . Just) + page <- many1 (noneOf "|]") + label <- option Nothing + (liftM Just (char '|' >> many1 (satisfy (/= ']')))) string "]]]" - return $ ObjectLink page text + return $ ObjectLink page label "object link" @@ -358,18 +350,18 @@ objLink = do try (string "[[[") pageLink :: Parser InlineElement pageLink = do try (string "[[") page <- option Nothing - (many1 (noneOf "#|]") >>= return . Just) + (liftM Just (many1 (noneOf "#|]"))) fragment <- option Nothing - (char '#' >> many1 (noneOf "|]") >>= return . Just) - text <- option Nothing - (char '|' >> many1 (satisfy (/= ']')) >>= return . Just) + (liftM Just (char '#' >> many1 (noneOf "|]"))) + label <- option Nothing + (liftM Just (char '|' >> many1 (satisfy (/= ']')))) case (page, fragment) of (Nothing, Nothing) -> pzero (_, _) -> return () string "]]" - return $ PageLink page fragment text + return $ PageLink page fragment label "page link" @@ -378,11 +370,11 @@ extLink :: Parser InlineElement extLink = do char '[' uriStr <- many1 (noneOf " \t]") skipMany (oneOf " \t") - text <- option Nothing - (many1 (noneOf "]") >>= return . Just) + label <- option Nothing + (liftM Just (many1 (noneOf "]"))) case parseURI uriStr of - Just uri -> char ']' >> return (ExternalLink uri text) + Just uri -> char ']' >> return (ExternalLink uri label) Nothing -> pzero "absolute URI" "external link" @@ -395,7 +387,7 @@ inlineCmd cmdTypeOf Just InlineCommandType -> do xs <- contents closeTag tagName - return $ InlineCmd $ InlineCommand { + return $ InlineCmd InlineCommand { iCmdName = tagName , iCmdAttributes = tagAttrs , iCmdContents = xs @@ -406,7 +398,7 @@ inlineCmd cmdTypeOf (try $ do (tagName, tagAttrs) <- emptyTag case cmdTypeOf tagName of Just InlineCommandType - -> return $ InlineCmd $ InlineCommand { + -> return $ InlineCmd InlineCommand { iCmdName = tagName , iCmdAttributes = tagAttrs , iCmdContents = [] @@ -423,7 +415,7 @@ inlineCmd cmdTypeOf <|> (comment >> contents) <|> - (newline >> contents >>= return . (Text "\n" :)) + liftM (Text "\n" :) (newline >> contents) <|> return [] @@ -480,14 +472,13 @@ comment = (try (string "") >> case level of - 1 -> return () - n -> skipTillEnd (n - 1)) - <|> - (anyChar >> skipTillEnd level) - ) + skipTillEnd level = (try (string "") >> case level of + 1 -> return () + n -> skipTillEnd (n - 1)) + <|> + (anyChar >> skipTillEnd level) blockSymbols :: [Char] @@ -506,7 +497,6 @@ ws = skipMany ( (oneOf " \t" >> return ()) -- end of line eol :: Parser () -eol = ( (newline >> return ()) - <|> - eof - ) +eol = (newline >> return ()) + <|> + eof