git.iko.soy/typeable/pandoc
on master help sign in
RST reader: cleaned up parsing of reference names in key blocks and links.
85657adfiddlosopher
Text/Pandoc/Readers/RST.hs+25−24
@@ -433,9 +433,29 @@ unknownDirective = try $ do
433-- reference key
433-- reference key
434--
434--
435
435
436+quotedReferenceName = try $ do
437+ char '`' >> notFollowedBy (char '`') -- `` means inline code!
438+ label <- many1Till inline (char '`')
439+ return label
440+
441+unquotedReferenceName = try $ do
442+ label <- many1Till inline (lookAhead $ char ':')
443+ return label
444+
445+isolated ch = try $ char ch >>~ notFollowedBy (char ch)
446+
447+simpleReferenceName = do
448+ raw <- many1 (alphaNum <|> isolated '-' <|> isolated '.' <|>
449+ (try $ char '_' >>~ lookAhead alphaNum))
450+ return [Str raw]
451+
452+referenceName = quotedReferenceName <|>
453+ (try $ simpleReferenceName >>~ lookAhead (char ':')) <|>
454+ unquotedReferenceName
455+
436referenceKey = do
456referenceKey = do
437 startPos <- getPosition
457 startPos <- getPosition
438− key <- choice [imageKey, anonymousKey, regularKeyQuoted, regularKey]
458+ key <- choice [imageKey, anonymousKey, regularKey]
439 st <- getState
459 st <- getState
440 let oldkeys = stateKeys st
460 let oldkeys = stateKeys st
441 updateState $ \st -> st { stateKeys = key : oldkeys }
461 updateState $ \st -> st { stateKeys = key : oldkeys }
@@ -466,16 +486,10 @@ anonymousKey = try $ do
466 state <- getState
486 state <- getState
467 return ([Str "_"], (removeLeadingTrailingSpace src, ""))
487 return ([Str "_"], (removeLeadingTrailingSpace src, ""))
468
488
469−regularKeyQuoted = try $ do
470− string ".. _`"
471− ref <- manyTill inline (char '`')
472− char ':'
473− src <- targetURI
474− return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
475−
476regularKey = try $ do
489regularKey = try $ do
477 string ".. _"
490 string ".. _"
478− ref <- manyTill inline (char ':')
491+ ref <- referenceName
492+ char ':'
479 src <- targetURI
493 src <- targetURI
480 return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
494 return (normalizeSpaces ref, (removeLeadingTrailingSpace src, ""))
481
495
@@ -557,27 +571,14 @@ link = choice [explicitLink, referenceLink, autoLink] <?> "link"
557explicitLink = try $ do
571explicitLink = try $ do
558 char '`'
572 char '`'
559 notFollowedBy (char '`') -- `` is marks start of inline code
573 notFollowedBy (char '`') -- `` is marks start of inline code
560− label <- manyTill inline (try (do {spaces; char '<'}))
574+ label <- manyTill inline (try (spaces >> char '<'))
561 src <- manyTill (noneOf ">\n ") (char '>')
575 src <- manyTill (noneOf ">\n ") (char '>')
562 skipSpaces
576 skipSpaces
563 string "`_"
577 string "`_"
564 return $ Link (normalizeSpaces label) (removeLeadingTrailingSpace src, "")
578 return $ Link (normalizeSpaces label) (removeLeadingTrailingSpace src, "")
565
579
566−reference = try $ do
567− char '`'
568− notFollowedBy (char '`')
569− label <- many1Till inline (char '`')
570− char '_'
571− return label
572−
573−oneWordReference = do
574− raw <- many1 alphaNum
575− char '_'
576− notFollowedBy alphaNum -- because this_is_not a link
577− return [Str raw]
578−
579referenceLink = try $ do
580referenceLink = try $ do
580− label <- reference <|> oneWordReference
581+ label <- (quotedReferenceName <|> simpleReferenceName) >>~ char '_'
581 key <- option label (do{char '_'; return [Str "_"]}) -- anonymous link
582 key <- option label (do{char '_'; return [Str "_"]}) -- anonymous link
582 state <- getState
583 state <- getState
583 let keyTable = stateKeys state
584 let keyTable = stateKeys state