Markdown reader: attribute parsing improvements.
* Cleaned up parsing code. * '-' in an attribute context = '.unnumbered'. The point of this is to provide a way to specify unnumbered headers in non-English documents.
This commit is contained in:
parent
5b0b21b51d
commit
f4629df14a
1 changed files with 15 additions and 14 deletions
|
@ -488,20 +488,16 @@ blockDelimiter f len = try $ do
|
||||||
Nothing -> count 3 (char c) >> many (char c) >>=
|
Nothing -> count 3 (char c) >> many (char c) >>=
|
||||||
return . (+ 3) . length
|
return . (+ 3) . length
|
||||||
|
|
||||||
attributes :: MarkdownParser (String, [String], [(String, String)])
|
attributes :: MarkdownParser Attr
|
||||||
attributes = try $ do
|
attributes = try $ do
|
||||||
char '{'
|
char '{'
|
||||||
spnl
|
spnl
|
||||||
attrs <- many (attribute >>~ spnl)
|
attrs <- many (attribute >>~ spnl)
|
||||||
char '}'
|
char '}'
|
||||||
let (ids, classes, keyvals) = unzip3 attrs
|
return $ foldl (\x f -> f x) nullAttr attrs
|
||||||
let firstNonNull [] = ""
|
|
||||||
firstNonNull (x:xs) | not (null x) = x
|
|
||||||
| otherwise = firstNonNull xs
|
|
||||||
return (firstNonNull $ reverse ids, concat classes, concat keyvals)
|
|
||||||
|
|
||||||
attribute :: MarkdownParser (String, [String], [(String, String)])
|
attribute :: MarkdownParser (Attr -> Attr)
|
||||||
attribute = identifierAttr <|> classAttr <|> keyValAttr
|
attribute = identifierAttr <|> classAttr <|> keyValAttr <|> specialAttr
|
||||||
|
|
||||||
identifier :: MarkdownParser String
|
identifier :: MarkdownParser String
|
||||||
identifier = do
|
identifier = do
|
||||||
|
@ -509,26 +505,31 @@ identifier = do
|
||||||
rest <- many $ alphaNum <|> oneOf "-_:."
|
rest <- many $ alphaNum <|> oneOf "-_:."
|
||||||
return (first:rest)
|
return (first:rest)
|
||||||
|
|
||||||
identifierAttr :: MarkdownParser (String, [a], [a1])
|
identifierAttr :: MarkdownParser (Attr -> Attr)
|
||||||
identifierAttr = try $ do
|
identifierAttr = try $ do
|
||||||
char '#'
|
char '#'
|
||||||
result <- identifier
|
result <- identifier
|
||||||
return (result,[],[])
|
return $ \(_,cs,kvs) -> (result,cs,kvs)
|
||||||
|
|
||||||
classAttr :: MarkdownParser (String, [String], [a])
|
classAttr :: MarkdownParser (Attr -> Attr)
|
||||||
classAttr = try $ do
|
classAttr = try $ do
|
||||||
char '.'
|
char '.'
|
||||||
result <- identifier
|
result <- identifier
|
||||||
return ("",[result],[])
|
return $ \(id',cs,kvs) -> (id',cs ++ [result],kvs)
|
||||||
|
|
||||||
keyValAttr :: MarkdownParser (String, [a], [(String, String)])
|
keyValAttr :: MarkdownParser (Attr -> Attr)
|
||||||
keyValAttr = try $ do
|
keyValAttr = try $ do
|
||||||
key <- identifier
|
key <- identifier
|
||||||
char '='
|
char '='
|
||||||
val <- enclosed (char '"') (char '"') litChar
|
val <- enclosed (char '"') (char '"') litChar
|
||||||
<|> enclosed (char '\'') (char '\'') litChar
|
<|> enclosed (char '\'') (char '\'') litChar
|
||||||
<|> many (escapedChar' <|> noneOf " \t\n\r}")
|
<|> many (escapedChar' <|> noneOf " \t\n\r}")
|
||||||
return ("",[],[(key,val)])
|
return $ \(id',cs,kvs) -> (id',cs,kvs ++ [(key,val)])
|
||||||
|
|
||||||
|
specialAttr :: MarkdownParser (Attr -> Attr)
|
||||||
|
specialAttr = do
|
||||||
|
char '-'
|
||||||
|
return $ \(id',cs,kvs) -> (id',cs ++ ["unnumbered"],kvs)
|
||||||
|
|
||||||
codeBlockFenced :: MarkdownParser (F Blocks)
|
codeBlockFenced :: MarkdownParser (F Blocks)
|
||||||
codeBlockFenced = try $ do
|
codeBlockFenced = try $ do
|
||||||
|
|
Loading…
Reference in a new issue