git.iko.soy/typeable/pandoc
on master help sign in
Implemented Ext_link_attributes in markdown reader.

Also simplified source URL and link title parsers.

commit49820d5b51cb1072d311814b999627c8fec9aa43 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 16 Jan 2013, 11:25 (14 years ago) parent517c4b5
2 files changed+28−31
src/Text/Pandoc/Options.hs+2 src/Text/Pandoc/Readers/Markdown.hs+26−31
src/Text/Pandoc/Options.hs+2
@@ -77,6 +77,7 @@ data Extension =
7777 | Ext_markdown_attribute -- ^ Interpret text inside HTML as markdown
7878 -- iff container has attribute 'markdown'
7979 | Ext_escaped_line_breaks -- ^ Treat a backslash at EOL as linebreak
80+ | Ext_link_attributes -- ^ MMD style reference link attributes
8081 | Ext_autolink_bare_uris -- ^ Make all absolute URIs into links
8182 | Ext_fancy_lists -- ^ Enable fancy list numbers and delimiters
8283 | Ext_startnum -- ^ Make start number of ordered list significant
@@ -170,6 +171,7 @@ multimarkdownExtensions = Set.fromList
170171 [ Ext_pipe_tables
171172 , Ext_raw_html
172173 , Ext_markdown_attribute
174+ , Ext_link_attributes
173175 , Ext_raw_tex
174176 , Ext_tex_math_double_backslash
175177 , Ext_intraword_underscores
src/Text/Pandoc/Readers/Markdown.hs+26−31
@@ -144,7 +144,7 @@ atMostSpaces n = (char ' ' >> atMostSpaces (n-1)) <|> return ()
144144litChar :: MarkdownParser Char
145145litChar = escapedChar'
146146 <|> noneOf "\n"
147− <|> (newline >> notFollowedBy blankline >> return ' ')
147+ <|> try (newline >> notFollowedBy blankline >> return ' ')
148148
149149-- | Parse a sequence of inline elements between square brackets,
150150-- including inlines between balanced pairs of square brackets.
@@ -265,6 +265,9 @@ referenceKey = try $ do
265265 manyTill (escapedChar' <|> litChar) (char '>')
266266 src <- try betweenAngles <|> sourceURL
267267 tit <- option "" referenceTitle
268+ -- currently we just ignore MMD-style link/image attributes
269+ _kvs <- option [] $ guardEnabled Ext_link_attributes
270+ >> many (spnl >> keyValAttr)
268271 blanklines
269272 let target = (escapeURI $ trimr src, tit)
270273 st <- getState
@@ -279,11 +282,18 @@ referenceKey = try $ do
279282referenceTitle :: MarkdownParser String
280283referenceTitle = try $ do
281284 skipSpaces >> optional newline >> skipSpaces
282− tit <- (charsInBalanced '(' ')' litChar >>= return . unwords . words)
283− <|> do delim <- char '\'' <|> char '"'
284− manyTill litChar (try (char delim >> skipSpaces >>
285− notFollowedBy (noneOf ")\n")))
286− return $ fromEntities tit
285+ let parenTit = charsInBalanced '(' ')' litChar
286+ fromEntities <$> (quotedTitle '"' <|> quotedTitle '\'' <|> parenTit)
287+
288+-- A link title in quotes
289+quotedTitle :: Char -> MarkdownParser String
290+quotedTitle c = try $ do
291+ char c
292+ notFollowedBy spaces
293+ let pEnder = try $ char c >> notFollowedBy (satisfy isAlphaNum)
294+ let regChunk = many1 (noneOf ['\\','\n',c]) <|> count 1 litChar
295+ let nestedChunk = (\x -> [c] ++ x ++ [c]) <$> quotedTitle c
296+ unwords . words . concat <$> manyTill (nestedChunk <|> regChunk) pEnder
287297
288298-- | PHP Markdown Extra style abbreviation key. Currently
289299-- we just skip them, since Pandoc doesn't have an element for
@@ -1266,7 +1276,7 @@ escapedChar' :: MarkdownParser Char
12661276escapedChar' = try $ do
12671277 char '\\'
12681278 (guardEnabled Ext_all_symbols_escapable >> satisfy (not . isAlphaNum))
1269− <|> oneOf "\\`*_{}[]()>#+-.!~"
1279+ <|> oneOf "\\`*_{}[]()>#+-.!~\""
12701280
12711281escapedChar :: MarkdownParser (F Inlines)
12721282escapedChar = do
@@ -1481,37 +1491,22 @@ reference = do notFollowedBy' (string "[^") -- footnote reference
14811491
14821492-- source for a link, with optional title
14831493source :: MarkdownParser (String, String)
1484−source =
1485− (try $ charsInBalanced '(' ')' litChar >>= parseFromString source') <|>
1486− -- the following is needed for cases like: [ref](/url(a).
1487− (enclosed (char '(') (char ')') litChar >>= parseFromString source')
1488−
1489−-- auxiliary function for source
1490−source' :: MarkdownParser (String, String)
1491−source' = do
1494+source = do
1495+ char '('
14921496 skipSpaces
1493− let nl = char '\n' >>~ notFollowedBy blankline
1494− let sourceURL = liftM unwords $ many $ try $ do
1495− notFollowedBy' linkTitle
1496− skipMany spaceChar
1497− optional nl
1498− skipMany spaceChar
1499− many1 $ escapedChar' <|> satisfy (not . isBlank)
1497+ let urlChunk = try $ notFollowedBy (oneOf "\"')") >>
1498+ (charsInBalanced '(' ')' litChar <|> count 1 litChar)
1499+ let sourceURL = (unwords . words . concat) <$> many urlChunk
15001500 let betweenAngles = try $
1501− char '<' >> manyTill (escapedChar' <|> noneOf ">\n" <|> nl) (char '>')
1501+ char '<' >> manyTill litChar (char '>')
15021502 src <- try betweenAngles <|> sourceURL
1503− tit <- option "" linkTitle
1503+ tit <- option "" $ try $ spnl >> linkTitle
15041504 skipSpaces
1505− eof
1505+ char ')'
15061506 return (escapeURI $ trimr src, tit)
15071507
15081508linkTitle :: MarkdownParser String
1509−linkTitle = try $ do
1510− (many1 spaceChar >> option '\n' newline) <|> newline
1511− skipSpaces
1512− delim <- oneOf "'\""
1513− tit <- manyTill litChar (try (char delim >> skipSpaces >> eof))
1514− return $ fromEntities tit
1509+linkTitle = fromEntities <$> (quotedTitle '"' <|> quotedTitle '\'')
15151510
15161511link :: MarkdownParser (F Inlines)
15171512link = try $ do