git.iko.soy/typeable/pandoc
on master help sign in
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`.

commitd7f905fb63e7cef83920bdb5621029f833538014 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 15 Nov 2020, 17:46 (6 years ago) parentf822514
2 files changed+36−27
src/Text/Pandoc/Readers/Markdown.hs+30−27 test/command/citeproc-author-in-text-suffix.md+6
src/Text/Pandoc/Readers/Markdown.hs+30−27
@@ -2076,7 +2076,7 @@ textualCite = try $ do
20762076 -- item comes later, we'll parse it here and figure out in
20772077 -- the runF stage if it's a citation. But it helps with
20782078 -- issue #6836 to filter out known example list references
2079− -- at this stage, so we don't increment stateNoteNumber.
2079+ -- at this stage, so that we don't increment stateNoteNumber.
20802080 getState >>= guard . isNothing . M.lookup key . stateExamples
20812081 noteNum <- stateNoteNumber <$> getState
20822082 let first = Citation{ citationId = key
@@ -2088,30 +2088,29 @@ textualCite = try $ do
20882088 , citationNoteNum = noteNum
20892089 , citationHash = 0
20902090 }
2091− mbrest <- option Nothing $ try $ spnl >> Just <$> withRaw normalCite
2092− case mbrest of
2093− Just (rest, raw) ->
2094− return $ flip B.cite (B.text $ "@" <> key <> " " <> raw) . (first:)
2095− <$> rest
2096− Nothing ->
2097− (do
2098− (cs, raw) <- withRaw $ bareloc first
2099− let (spaces',raw') = T.span isSpace raw
2100− spc | T.null spaces' = mempty
2101− | otherwise = B.space
2102− lab <- parseFromString' inlines $ dropBrackets raw'
2103− fallback <- referenceLink B.linkWith (lab,raw')
2104− return $ do
2105− fallback' <- fallback
2106− cs' <- cs
2107− return $
2108− case B.toList fallback' of
2109− Link{}:_ -> B.cite [first] (B.str $ "@" <> key) <> spc <> fallback'
2110− _ -> B.cite cs' (B.text $ "@" <> key <> " " <> raw))
2111− <|> return (do st <- askF
2112− return $ case M.lookup key (stateExamples st) of
2113− Just n -> B.str $ tshow n
2114− _ -> B.cite [first] $ B.str $ "@" <> key)
2091+ (do -- parse [braced] material after author-in-text cite
2092+ (cs, raw) <- withRaw $
2093+ (fmap (first:) <$> try (spnl *> normalCite))
2094+ <|> bareloc first
2095+ let (spaces',raw') = T.span isSpace raw
2096+ spc | T.null spaces' = mempty
2097+ | otherwise = B.space
2098+ lab <- parseFromString' inlines $ dropBrackets raw'
2099+ fallback <- referenceLink B.linkWith (lab,raw')
2100+ -- undo any incrementing of stateNoteNumber from last step:
2101+ updateState $ \st -> st{ stateNoteNumber = noteNum }
2102+ return $ do
2103+ fallback' <- fallback
2104+ cs' <- cs
2105+ return $
2106+ case B.toList fallback' of
2107+ Link{}:_ -> B.cite [first] (B.str $ "@" <> key) <> spc <> fallback'
2108+ _ -> B.cite cs' (B.text $ "@" <> key <> " " <> raw))
2109+ <|> -- no braced material
2110+ return (do st <- askF
2111+ return $ case M.lookup key (stateExamples st) of
2112+ Just n -> B.str $ tshow n
2113+ _ -> B.cite [first] $ B.str $ "@" <> key)
21152114
21162115bareloc :: PandocMonad m => Citation -> MarkdownParser m (F [Citation])
21172116bareloc c = try $ do
@@ -2119,7 +2118,7 @@ bareloc c = try $ do
21192118 char '['
21202119 notFollowedBy $ char '^'
21212120 suff <- suffix
2122− rest <- option (return []) $ try $ char ';' >> citeList
2121+ rest <- option (return []) $ try $ char ';' >> spnl >> citeList
21232122 spnl
21242123 char ']'
21252124 notFollowedBy $ oneOf "[("
@@ -2148,7 +2147,11 @@ suffix = try $ do
21482147
21492148prefix :: PandocMonad m => MarkdownParser m (F Inlines)
21502149prefix = trimInlinesF . mconcat <$>
2151− manyTill inline (char ']' <|> fmap (const ']') (lookAhead citeKey))
2150+ manyTill inline (char ']'
2151+ <|> lookAhead
2152+ (try $ do optional (try (char ';' >> spnl))
2153+ citeKey
2154+ return ']'))
21522155
21532156citeList :: PandocMonad m => MarkdownParser m (F [Citation])
21542157citeList = fmap sequence $ sepBy1 citation (try $ char ';' >> spnl)
test/command/citeproc-author-in-text-suffix.mdnew file+6
@@ -0,0 +1,6 @@
1+```
2+% pandoc -t native
3+@a [p. 33; @b]
4+^D
5+[Para [Cite [Citation {citationId = "a", citationPrefix = [], citationSuffix = [Str "p.\160\&33"], citationMode = AuthorInText, citationNoteNum = 1, citationHash = 0},Citation {citationId = "b", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 1, citationHash = 0}] [Str "@a",Space,Str "[p.",Space,Str "33;",Space,Str "@b]"]]]
6+```