Markdown reader: don't increment stateNoteNumber for example refs.

Background:  syntactically, references to example list items
can't be distinguished from citations; we only know which they
are after we've parsed the whole document (and this is resolved
in the `runF` stage).

This means that pandoc's calculation of `citationNoteNum`
can sometimes be wrong when there are example list references.

This commit partially addresses #6836, but only for the case
where the example list references refer to list items defined
previously in the document.
This commit is contained in:
John MacFarlane 2020-11-14 15:00:17 -08:00
parent 68b298ed9a
commit b8d17f7ae8
3 changed files with 30 additions and 1 deletions

View file

@ -1535,6 +1535,11 @@ ltSign = do
char '<'
return $ return $ B.str "<"
-- Note that if the citations extension is enabled, example refs will be
-- parsed as citations, and handled by a clause in the parser for citations,
-- since we won't know whether we have an example ref until the
-- whole document has been parsed. But we need this parser
-- here in case citations is disabled.
exampleRef :: PandocMonad m => MarkdownParser m (F Inlines)
exampleRef = try $ do
guardEnabled Ext_example_lists
@ -2066,6 +2071,13 @@ cite = do
textualCite :: PandocMonad m => MarkdownParser m (F Inlines)
textualCite = try $ do
(suppressAuthor, key) <- citeKey
-- If this is a reference to an earlier example list item,
-- then don't parse it as a citation. If the example list
-- 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.
getState >>= guard . isNothing . M.lookup key . stateExamples
noteNum <- stateNoteNumber <$> getState
let first = Citation{ citationId = key
, citationPrefix = []

17
test/command/6836.md Normal file
View file

@ -0,0 +1,17 @@
```
pandoc -t native
[@buchanan]
(@foo)
See @foo.
[@buchanan]
^D
[Para [Cite [Citation {citationId = "buchanan", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 1, citationHash = 0}] [Str "[@buchanan]"]]
,OrderedList (1,Example,TwoParens)
[[]]
,Para [Str "See",Space,Str "1."]
,Para [Cite [Citation {citationId = "buchanan", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 2, citationHash = 0}] [Str "[@buchanan]"]]]
```

View file

@ -328,7 +328,7 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "Author",S
,Header 2 ("reference-link-fallbacks",[],[]) [Str "Reference",Space,Str "link",Space,Str "fallbacks"]
,Para [Str "[",Emph [Str "not",Space,Str "a",Space,Str "link"],Str "]",Space,Str "[",Emph [Str "nope"],Str "]\8230"]
,Header 2 ("reference-link-followed-by-a-citation",[],[]) [Str "Reference",Space,Str "link",Space,Str "followed",Space,Str "by",Space,Str "a",Space,Str "citation"]
,Para [Str "MapReduce",Space,Str "is",Space,Str "a",Space,Str "paradigm",Space,Str "popularized",Space,Str "by",Space,Link ("",[],[]) [Str "Google"] ("http://google.com",""),Space,Cite [Citation {citationId = "mapreduce", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 3, citationHash = 0}] [Str "[@mapreduce]"],Space,Str "as",Space,Str "its",SoftBreak,Str "most",Space,Str "vocal",Space,Str "proponent."]
,Para [Str "MapReduce",Space,Str "is",Space,Str "a",Space,Str "paradigm",Space,Str "popularized",Space,Str "by",Space,Link ("",[],[]) [Str "Google"] ("http://google.com",""),Space,Cite [Citation {citationId = "mapreduce", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 2, citationHash = 0}] [Str "[@mapreduce]"],Space,Str "as",Space,Str "its",SoftBreak,Str "most",Space,Str "vocal",Space,Str "proponent."]
,Header 2 ("empty-reference-links",[],[]) [Str "Empty",Space,Str "reference",Space,Str "links"]
,Para [Str "bar"]
,Para [Link ("",[],[]) [Str "foo2"] ("","")]