Skip to content

Commit 2693f55

Browse files
committed
Book projects now collect all the cites
1 parent a0795b1 commit 2693f55

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/command/render/filters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import { metadataNormalizationFilterActive } from "./normalize.ts";
6464
import { kCodeAnnotations } from "../../format/html/format-html-shared.ts";
6565
import { projectOutputDir } from "../../project/project-shared.ts";
6666
import { relative } from "path/mod.ts";
67+
import { citeIndexFilterParams } from "../../project/project-cites.ts";
6768

6869
const kQuartoParams = "quarto-params";
6970

@@ -122,6 +123,7 @@ export async function filterParamsJson(
122123
...quartoColumnParams,
123124
...await quartoFilterParams(options, defaults),
124125
...crossrefFilterParams(options, defaults),
126+
...citeIndexFilterParams(options, defaults),
125127
...layoutFilterParams(options.format),
126128
...languageFilterParams(options.format.language),
127129
...filterParams,

src/project/project-cites.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* project-cites.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*
6+
*/
7+
8+
import { ensureDirSync, existsSync } from "fs/mod.ts";
9+
10+
import { join } from "path/mod.ts";
11+
import { PandocOptions } from "../command/render/types.ts";
12+
import { FormatPandoc } from "../config/types.ts";
13+
import { projectIsBook } from "./project-context.ts";
14+
15+
import { projectScratchPath } from "./project-scratch.ts";
16+
17+
export const kCitesIndexFile = "cites-index-file";
18+
19+
const kCiteIndexDir = "cites";
20+
21+
type CiteIndex = Record<string, string[]>;
22+
23+
export function citeIndexFilterParams(
24+
options: PandocOptions,
25+
_defaults?: FormatPandoc,
26+
) {
27+
if (options.project && projectIsBook(options.project)) {
28+
return {
29+
[kCitesIndexFile]: citeIndexFile(options.project.dir),
30+
};
31+
} else {
32+
return {};
33+
}
34+
}
35+
36+
export function citeIndex(projectDir: string) {
37+
const mainIndexFile = citeIndexFile(projectDir);
38+
const mainIndex: CiteIndex = existsSync(mainIndexFile)
39+
? JSON.parse(Deno.readTextFileSync(mainIndexFile))
40+
: {};
41+
return mainIndex;
42+
}
43+
44+
function citeIndexFile(projectDir: string) {
45+
// read (or create) main index
46+
const citesDir = projectScratchPath(projectDir, kCiteIndexDir);
47+
ensureDirSync(citesDir);
48+
return join(citesDir, "index.json");
49+
}

src/resources/filters/main.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import("./quarto-init/includes.lua")
6060
import("./quarto-init/resourcerefs.lua")
6161

6262
import("./quarto-post/book.lua")
63+
import("./quarto-post/cites.lua")
6364
import("./quarto-post/delink.lua")
6465
import("./quarto-post/fig-cleanup.lua")
6566
import("./quarto-post/foldcode.lua")
@@ -217,6 +218,7 @@ local quartoPre = {
217218

218219
local quartoPost = {
219220
-- quarto-post
221+
{ name = "post-cites", filter = indexCites() },
220222
{ name = "post-foldCode", filter = foldCode() },
221223
{ name = "post-figureCleanupCombined", filter = combineFilters({
222224
latexDiv(),
@@ -244,6 +246,7 @@ local quartoFinalize = {
244246
})
245247
},
246248
{ name = "finalize-bookCleanup", filter = bookCleanup() },
249+
{ name = "finalize-cites", filter = writeCites() },
247250
{ name = "finalize-metaCleanup", filter = metaCleanup() },
248251
{ name = "finalize-dependencies", filter = dependencies() },
249252
{ name = "finalize-wrapped-writer", filter = wrapped_writer() }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- cites.lua
2+
-- Copyright (C) 2020-2022 Posit Software, PBC
3+
4+
local cites = pandoc.List()
5+
local kRefsIndentifier = "refs-target-identifier"
6+
7+
function indexCites()
8+
return {
9+
Div = function(el)
10+
local refsIndentifier = param(kRefsIndentifier)
11+
if el.attr.identifier == 'refs' and refsIndentifier then
12+
tappend(el.content, {pandoc.Plain(refsIndentifier)})
13+
return el;
14+
end
15+
end,
16+
Cite = function(el)
17+
el.citations:map(function (cite)
18+
cites:insert(cite.id)
19+
end)
20+
end
21+
}
22+
end
23+
24+
function writeCites()
25+
return {
26+
Pandoc = function(el)
27+
-- the file to write to
28+
local citesFilePath = param("cites-index-file")
29+
if citesFilePath then
30+
-- open the file
31+
local citesRaw = _quarto.file.read(citesFilePath)
32+
local documentCites = {}
33+
if citesRaw then
34+
documentCites = quarto.json.decode(citesRaw)
35+
end
36+
37+
-- write the cites
38+
local inputFile = quarto.doc.input_file
39+
local relativeFilePath = pandoc.path.make_relative(inputFile, quarto.project.directory)
40+
documentCites[relativeFilePath] = cites
41+
42+
-- write the file
43+
local json = quarto.json.encode(documentCites)
44+
local file = io.open(citesFilePath, "w")
45+
if file ~= nil then
46+
file:write(json .. "\n")
47+
file:close()
48+
else
49+
fail('Error opening book citations file at ' .. citesFilePath)
50+
end
51+
end
52+
end
53+
}
54+
end

0 commit comments

Comments
 (0)