Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/bibliography.ts
3557 views
1
/*
2
* bibliography.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
* Copyright (c) 2016-2021 Thomas Watson Steen
6
*
7
* Adapted from: https://github.com/watson/ci-info
8
*/
9
10
import { dirname, isAbsolute, join } from "../deno_ral/path.ts";
11
import { kBibliography } from "../config/constants.ts";
12
import { Metadata } from "../config/types.ts";
13
import { asArray } from "./array.ts";
14
import { CSL } from "./csl.ts";
15
16
import { execProcess } from "./process.ts";
17
import { pandocBinaryPath } from "./resources.ts";
18
19
export async function bibliographyCslJson(input: string, metadata: Metadata) {
20
const bibliography = metadata[kBibliography] as string | string[];
21
if (bibliography) {
22
const references = await renderToCSLJSON(
23
dirname(input),
24
asArray<string>(bibliography),
25
);
26
return references;
27
}
28
}
29
30
export async function renderHtml(entry: CSL, csl?: string) {
31
const cmd = [pandocBinaryPath()];
32
cmd.push("-f");
33
cmd.push("csljson");
34
cmd.push("-t");
35
cmd.push("html5");
36
cmd.push("--citeproc");
37
if (csl) {
38
cmd.push("--csl");
39
cmd.push(csl);
40
}
41
42
const cslStr = JSON.stringify([entry], undefined, 2);
43
const result = await execProcess(
44
{ cmd: cmd[0], args: cmd.slice(1), stdout: "piped", stderr: "piped" },
45
cslStr,
46
);
47
if (result.success) {
48
return result.stdout;
49
} else {
50
throw new Error(
51
`Failed to render citation: error code ${result.code}\n${result.stderr}`,
52
);
53
}
54
}
55
56
export async function renderBibTex(entry: CSL) {
57
const cmd = [pandocBinaryPath()];
58
cmd.push("-f");
59
cmd.push("csljson");
60
cmd.push("-t");
61
cmd.push("biblatex");
62
cmd.push("--citeproc");
63
64
const cslStr = JSON.stringify([entry], undefined, 2);
65
const result = await execProcess(
66
{ cmd: cmd[0], args: cmd.slice(1), stdout: "piped", stderr: "piped" },
67
cslStr,
68
);
69
if (result.success) {
70
return result.stdout;
71
} else {
72
throw new Error(
73
`Failed to generate bibtex, rendering failed with code ${result.code}\n${result.stderr}`,
74
);
75
}
76
}
77
78
export async function renderToCSLJSON(
79
dir: string,
80
biblios: string[],
81
): Promise<CSL[]> {
82
const bibloEntries = [];
83
for (const biblio of biblios) {
84
// The bibliopath might end up as an absolute path
85
// in which case just leave it alone (otherwise it is cwd relative)
86
const biblioPath = join(dir, biblio);
87
const cmd = [pandocBinaryPath()];
88
cmd.push(
89
isAbsolute(biblioPath) ? biblioPath : join(Deno.cwd(), biblioPath),
90
);
91
cmd.push("-t");
92
cmd.push("csljson");
93
cmd.push("--citeproc");
94
95
const result = await execProcess(
96
{
97
cmd: cmd[0],
98
args: cmd.slice(1),
99
stdout: "piped",
100
stderr: "piped",
101
cwd: dir,
102
},
103
);
104
if (result.success) {
105
if (result.stdout) {
106
const entries = JSON.parse(result.stdout);
107
bibloEntries.push(...entries);
108
}
109
} else {
110
throw new Error(
111
`Failed to generate bibliography, rendering failed with code ${result.code}\n${result.stderr}`,
112
);
113
}
114
}
115
return bibloEntries;
116
}
117
118