<|>
listElement
<|>
+ definitionList
+ <|>
+ pdata
+ <|>
leadingSpaced
<|>
paragraph
horizontalLine :: Parser BlockElement
-horizontalLine = try $ do count 4 (char '-')
+horizontalLine = try ( do count 4 (char '-')
many (char '-')
ws
eol
return HorizontalLine
+ )
+ <?>
+ "horizontal line"
listElement :: Parser BlockElement
)
<|>
return []
-{-
- items stack = do nested <- listElement' stack
- rest <- items stack
- return (Left nested : rest)
- <|>
- do xs <- many1 inlineElement
- rest <- items stack
- return (Right xs : rest)
- <|>
- try ( newline
- >>
- string stack
- >>
- items stack
- )
- <|>
- return []
--}
toType :: Char -> ListType
toType '*' = Bullet
toType '#' = Numbered
+definitionList :: Parser BlockElement
+definitionList = many1 definition >>= return . DefinitionList
+ where
+ definition :: Parser Definition
+ definition = do char ';'
+ ws
+ tHead <- inlineElement
+ tRest <- term
+ d <- description
+ return (Definition (tHead:tRest) d)
+ <?>
+ "definition list"
+
+ term :: Parser [InlineElement]
+ term = (char ':' >> ws >> return [])
+ <|>
+ (newline >> char ':' >> ws >> return [])
+ <|>
+ do x <- inlineElement
+ xs <- term
+ return (x:xs)
+ <?>
+ "term to be defined"
+
+ description :: Parser [InlineElement]
+ description = do x <- inlineElement
+ xs <- description
+ return (x:xs)
+ <|>
+ try ( do newline
+ char ':'
+ ws
+ xs <- description
+ return (Text "\n" : xs)
+ )
+ <|>
+ (newline >> return [])
+ <|>
+ (eof >> return [])
+ <?>
+ "description of term"
+
+
+pdata :: Parser BlockElement
+pdata = do try (string "<![PDATA[")
+ many (oneOf " \t\n")
+ x <- pdata'
+ return (Preformatted [Text x])
+ where
+ pdata' :: Parser String
+ pdata' = do try (many (oneOf " \t\n") >> string "]]>")
+ return []
+ <|>
+ do x <- anyChar
+ xs <- pdata'
+ return (x:xs)
+
+
leadingSpaced :: Parser BlockElement
-leadingSpaced = char ' ' >> leadingSpaced' >>= return . LeadingSpaced
+leadingSpaced = (char ' ' >> leadingSpaced' >>= return . Preformatted)
+ <?>
+ "leading space"
where
leadingSpaced' :: Parser [InlineElement]
leadingSpaced' = do x <- inlineElement
inlineElement :: Parser InlineElement
inlineElement = skipMany comment
>>
- ( try text
+ ( cdata
<|>
- try pageLink
+ apostrophes
+ <|>
+ text
+ <|>
+ pageLink
)
+cdata :: Parser InlineElement
+cdata = try (string "<![CDATA[") >> cdata' >>= return . Text
+ where
+ cdata' :: Parser String
+ cdata' = do try (string "]]>")
+ return []
+ <|>
+ do x <- anyChar
+ xs <- cdata'
+ return (x:xs)
+
+
text :: Parser InlineElement
-text = many1 (noneOf ('\n':inlineSymbols)) >>= return . Text
+text = ( char ':'
+ >>
+ many (noneOf ('\n':inlineSymbols))
+ >>=
+ return . Text . (':' :)
+ -- 定義リストとの關係上、コロンは先頭にしか來れない。
+ )
+ <|>
+ ( many1 (noneOf ('\n':inlineSymbols))
+ >>=
+ return . Text
+ )
+ <?>
+ "text"
+
+
+apostrophes :: Parser InlineElement
+apostrophes = foldr (<|>) pzero (map try [apos1, apos2, apos3, apos4, apos5])
+ where
+ apos1 = apos 1 >> return (Text "'")
+
+ apos2 = do apos 2
+ xs <- many1 inlineElement
+ apos 2
+ return (Italic xs)
+
+ apos3 = do apos 3
+ xs <- many1 inlineElement
+ apos 3
+ return (Bold xs)
+
+ apos4 = apos 4 >> return (Text "'")
+
+ apos5 = do apos 5
+ xs <- many1 inlineElement
+ apos 5
+ return (Italic [Bold xs])
+
+ apos :: Int -> Parser ()
+ apos n = count n (char '\'') >> notFollowedBy (char '\'')
pageLink :: Parser InlineElement
-pageLink = do string "[["
+pageLink = do try (string "[[")
page <- option Nothing
(many1 (noneOf "#|]") >>= return . Just)
fragment <- option Nothing
blockSymbols :: [Char]
-blockSymbols = " =-*#"
+blockSymbols = " =-*#;"
inlineSymbols :: [Char]
-inlineSymbols = "<["
+inlineSymbols = "<[:'"
-- white space
ws :: Parser ()