Skip to content

Commit c15360b

Browse files
committed
Permit the natbib / biblatex to target refs div
Fixes #2770, #2009
1 parent 2ab96b4 commit c15360b

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/format/pdf/format-pdf.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { mergeConfigs } from "../../core/config.ts";
1111
import { texSafeFilename } from "../../core/tex.ts";
1212

1313
import {
14+
kBibliography,
1415
kCapBottom,
1516
kCapLoc,
1617
kCapTop,
@@ -318,6 +319,16 @@ function pdfLatexPostProcessor(
318319
calloutFigureHoldLineProcessor(),
319320
];
320321

322+
if (format.pandoc[kCiteMethod] === "biblatex") {
323+
lineProcessors.push(bibLatexBibligraphyRefsDivProcessor());
324+
} else if (format.pandoc[kCiteMethod] === "natbib") {
325+
lineProcessors.push(
326+
natbibBibligraphyRefsDivProcessor(
327+
format.metadata[kBibliography] as string[] | undefined,
328+
),
329+
);
330+
}
331+
321332
const marginCites = format.metadata[kCitationLocation] === "margin";
322333
const renderedCites = {};
323334
if (marginCites) {
@@ -633,6 +644,47 @@ const calloutFigureHoldLineProcessor = () => {
633644
};
634645
};
635646

647+
const kQuartoBibPlaceholderRegex = "%bib-loc-124C8010";
648+
const bibLatexBibligraphyRefsDivProcessor = () => {
649+
let hasRefsDiv = false;
650+
return (line: string): string | undefined => {
651+
if (line === kQuartoBibPlaceholderRegex) {
652+
if (!hasRefsDiv) {
653+
hasRefsDiv = true;
654+
return "\\printbibliography[heading=none]";
655+
} else {
656+
// already seen a refs div, just ignore this one
657+
return undefined;
658+
}
659+
} else if (hasRefsDiv && line.match(/^\\printbibliography$/)) {
660+
return undefined;
661+
} else {
662+
return line;
663+
}
664+
};
665+
};
666+
667+
const natbibBibligraphyRefsDivProcessor = (bibs?: string[]) => {
668+
let hasRefsDiv = false;
669+
return (line: string): string | undefined => {
670+
if (line === kQuartoBibPlaceholderRegex) {
671+
if (bibs && !hasRefsDiv) {
672+
hasRefsDiv = true;
673+
return `\\renewcommand{\\bibsection}{}\n\\bibliography{${
674+
bibs.join(",")
675+
}}`;
676+
} else {
677+
// already seen a refs div, just ignore this one
678+
return undefined;
679+
}
680+
} else if (hasRefsDiv && line.match(/^\s*\\bibliography{.*}$/)) {
681+
return undefined;
682+
} else {
683+
return line;
684+
}
685+
};
686+
};
687+
636688
// Removes the biblatex \printbibiliography command
637689
const suppressBibLatexBibliographyLineProcessor = () => {
638690
return (line: string): string | undefined => {
@@ -656,7 +708,6 @@ const suppressNatbibBibliographyLineProcessor = () => {
656708

657709
// {?quarto-cite:(id)}
658710
const kQuartoCiteRegex = /{\?quarto-cite:(.*?)}/g;
659-
660711
const bibLatexCiteLineProcessor = () => {
661712
return (line: string): string | undefined => {
662713
return line.replaceAll(kQuartoCiteRegex, (_match, citeKey) => {

src/resources/filters/main.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import("./quarto-post/reveal.lua")
7474
import("./quarto-post/tikz.lua")
7575
import("./quarto-post/pdf-images.lua")
7676
import("./quarto-post/cellcleanup.lua")
77+
import("./quarto-post/bibliography.lua")
7778

7879
import("./quarto-finalize/dependencies.lua")
7980
import("./quarto-finalize/book-cleanup.lua")
@@ -227,6 +228,7 @@ local quartoPost = {
227228
{ name = "post-cell-cleanup", filter = cell_cleanup() },
228229
{ name = "post-cites", filter = indexCites() },
229230
{ name = "post-foldCode", filter = foldCode() },
231+
{ name = "post-bibligraphy", filter = bibliography() },
230232
{ name = "post-figureCleanupCombined", filter = combineFilters({
231233
latexDiv(),
232234
responsive(),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- bibliography.lua
2+
-- Copyright (C) 2020-2022 Posit Software, PBC
3+
4+
function bibliography()
5+
return {
6+
Div = function(el)
7+
local citeMethod = param('cite-method', 'citeproc')
8+
if _quarto.format.isLatexOutput() and el.attr.identifier == "refs" and citeMethod ~= 'citeproc' then
9+
return pandoc.RawBlock("latex", '%bib-loc-124C8010')
10+
end
11+
end
12+
}
13+
end

0 commit comments

Comments
 (0)