diff options
author | John MacFarlane <jgm@berkeley.edu> | 2020-11-15 17:46:24 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2020-11-15 17:51:03 -0800 |
commit | d7f905fb63e7cef83920bdb5621029f833538014 (patch) | |
tree | 3419e228a6fea11fda7ba00f1cba1f97a28e0a36 /src/Text/Pandoc | |
parent | f8225140a58fa53d5c45ec644b8d5add342ced75 (diff) | |
download | pandoc-d7f905fb63e7cef83920bdb5621029f833538014.tar.gz |
Markdown reader: fix detection of locators following in-text citations.
Prevously, if we had `@foo [p. 33; @bar]`, the `p. 33` would be
incorrectly parsed as a prefix of `@bar` rather than a suffix
of `@foo`.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r-- | src/Text/Pandoc/Readers/Markdown.hs | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index e62a8a978..02f1f212f 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -2076,7 +2076,7 @@ textualCite = try $ do -- item comes later, we'll parse it here and figure out in -- the runF stage if it's a citation. But it helps with -- issue #6836 to filter out known example list references - -- at this stage, so we don't increment stateNoteNumber. + -- at this stage, so that we don't increment stateNoteNumber. getState >>= guard . isNothing . M.lookup key . stateExamples noteNum <- stateNoteNumber <$> getState let first = Citation{ citationId = key @@ -2088,30 +2088,29 @@ textualCite = try $ do , citationNoteNum = noteNum , citationHash = 0 } - mbrest <- option Nothing $ try $ spnl >> Just <$> withRaw normalCite - case mbrest of - Just (rest, raw) -> - return $ flip B.cite (B.text $ "@" <> key <> " " <> raw) . (first:) - <$> rest - Nothing -> - (do - (cs, raw) <- withRaw $ bareloc first - let (spaces',raw') = T.span isSpace raw - spc | T.null spaces' = mempty - | otherwise = B.space - lab <- parseFromString' inlines $ dropBrackets raw' - fallback <- referenceLink B.linkWith (lab,raw') - return $ do - fallback' <- fallback - cs' <- cs - return $ - case B.toList fallback' of - Link{}:_ -> B.cite [first] (B.str $ "@" <> key) <> spc <> fallback' - _ -> B.cite cs' (B.text $ "@" <> key <> " " <> raw)) - <|> return (do st <- askF - return $ case M.lookup key (stateExamples st) of - Just n -> B.str $ tshow n - _ -> B.cite [first] $ B.str $ "@" <> key) + (do -- parse [braced] material after author-in-text cite + (cs, raw) <- withRaw $ + (fmap (first:) <$> try (spnl *> normalCite)) + <|> bareloc first + let (spaces',raw') = T.span isSpace raw + spc | T.null spaces' = mempty + | otherwise = B.space + lab <- parseFromString' inlines $ dropBrackets raw' + fallback <- referenceLink B.linkWith (lab,raw') + -- undo any incrementing of stateNoteNumber from last step: + updateState $ \st -> st{ stateNoteNumber = noteNum } + return $ do + fallback' <- fallback + cs' <- cs + return $ + case B.toList fallback' of + Link{}:_ -> B.cite [first] (B.str $ "@" <> key) <> spc <> fallback' + _ -> B.cite cs' (B.text $ "@" <> key <> " " <> raw)) + <|> -- no braced material + return (do st <- askF + return $ case M.lookup key (stateExamples st) of + Just n -> B.str $ tshow n + _ -> B.cite [first] $ B.str $ "@" <> key) bareloc :: PandocMonad m => Citation -> MarkdownParser m (F [Citation]) bareloc c = try $ do @@ -2119,7 +2118,7 @@ bareloc c = try $ do char '[' notFollowedBy $ char '^' suff <- suffix - rest <- option (return []) $ try $ char ';' >> citeList + rest <- option (return []) $ try $ char ';' >> spnl >> citeList spnl char ']' notFollowedBy $ oneOf "[(" @@ -2148,7 +2147,11 @@ suffix = try $ do prefix :: PandocMonad m => MarkdownParser m (F Inlines) prefix = trimInlinesF . mconcat <$> - manyTill inline (char ']' <|> fmap (const ']') (lookAhead citeKey)) + manyTill inline (char ']' + <|> lookAhead + (try $ do optional (try (char ';' >> spnl)) + citeKey + return ']')) citeList :: PandocMonad m => MarkdownParser m (F [Citation]) citeList = fmap sequence $ sepBy1 citation (try $ char ';' >> spnl) |