Allow formatted bibliography to be placed in metadata fields.

This modifies `processCitations` so that pandoc will look not just
in the document body but in metadata for a Div with id `refs` in
which to place the formatted bibliography.

Thus, one can include a metadata field, say `refs`, whose content
is an empty div with id `refs`, and the formatted bibliography
will be put into this metadata field.  It may then be interpolated
into a template using the variable `refs`.

Closes #7969.

Closes #526 by providing a way to interpolate references into
a template.
This commit is contained in:
John MacFarlane 2022-03-16 14:34:47 -07:00
parent 878c4d6607
commit 75ddff2422
2 changed files with 27 additions and 13 deletions

View file

@ -5975,6 +5975,19 @@ The bibliography will be inserted after this heading. Note that
the `unnumbered` class will be added to this heading, so that the
section will not be numbered.
If you want to put the bibliography into a variable in your
template, one way to do that is to put the div with id `refs`
into a metadata field, e.g.
---
refs: |
::: {#refs}
:::
...
You can then put the variable `$refs$` into your template where
you want the bibliography to be placed.
## Including uncited items in the bibliography
If you want to include items in the bibliography without actually

View file

@ -108,8 +108,8 @@ processCitations (Pandoc meta bs) = do
evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
$ cits
return $ walk removeQuoteSpan
$ Pandoc meta''
$ insertRefs refkvs classes meta'' (B.toList bibs) bs'
$ insertRefs refkvs classes (B.toList bibs)
$ Pandoc meta'' bs'
removeQuoteSpan :: Inline -> Inline
removeQuoteSpan (Span ("",["csl-quoted"],[]) xs) = Span nullAttr xs
@ -461,23 +461,24 @@ isYesValue _ = False
-- if document contains a Div with id="refs", insert
-- references as its contents. Otherwise, insert references
-- at the end of the document in a Div with id="refs"
insertRefs :: [(Text,Text)] -> [Text] -> Meta -> [Block] -> [Block] -> [Block]
insertRefs _ _ _ [] bs = bs
insertRefs refkvs refclasses meta refs bs =
insertRefs :: [(Text,Text)] -> [Text] -> [Block] -> Pandoc -> Pandoc
insertRefs _ _ [] d = d
insertRefs refkvs refclasses refs (Pandoc meta bs) =
if isRefRemove meta
then bs
else case runState (walkM go bs) False of
(bs', True) -> bs'
(_, False)
-> case refTitle meta of
then Pandoc meta bs
else case runState (walkM go (Pandoc meta bs)) False of
(d', True) -> d'
(Pandoc meta' bs', False)
-> Pandoc meta' $
case refTitle meta of
Nothing ->
case reverse bs of
case reverse bs' of
Header lev (id',classes,kvs) ys : xs ->
reverse xs ++
[Header lev (id',addUnNumbered classes,kvs) ys,
Div ("refs",refclasses,refkvs) refs]
_ -> bs ++ [refDiv]
Just ils -> bs ++
_ -> bs' ++ [refDiv]
Just ils -> bs' ++
[Header 1 ("bibliography", ["unnumbered"], []) ils,
refDiv]
where