-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathcrossref.ts
More file actions
153 lines (137 loc) · 4.23 KB
/
crossref.ts
File metadata and controls
153 lines (137 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* command.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { join } from "../../deno_ral/path.ts";
import { readAll } from "../../deno_ral/io.ts";
import { error } from "../../deno_ral/log.ts";
import { encodeBase64 } from "../../deno_ral/encoding.ts";
import { Command } from "cliffy/command/mod.ts";
import { execProcess } from "../../core/process.ts";
import { pandocBinaryPath, resourcePath } from "../../core/resources.ts";
import { globalTempContext } from "../../core/temp.ts";
function parseCrossrefFlags(options: any, args: string[]): {
input?: string;
output?: string;
} {
let input: string | undefined, output: string | undefined;
// stop early with no input seems wonky in Cliffy so we need to undo the damage here
// by inspecting partially-parsed input...
if (options.input && args[0]) {
input = args.shift();
} else if (options.output && args[0]) {
output = args.shift();
}
const argsStack = [...args];
let arg = argsStack.shift();
while (arg !== undefined) {
switch (arg) {
case "-i":
case "--input":
arg = argsStack.shift();
if (arg) {
input = arg;
}
break;
case "-o":
case "--output":
arg = argsStack.shift();
if (arg) {
output = arg;
}
break;
default:
arg = argsStack.shift();
break;
}
}
return { input, output };
}
const makeCrossrefCommand = () => {
return new Command()
.description("Index cross references for content")
.stopEarly()
.arguments("[...args]")
.option(
"-i, --input",
"Use FILE as input (default: stdin).",
)
.option(
"-o, --output",
"Write output to FILE (default: stdout).",
)
.action(async (options, ...args: string[]) => {
const flags = parseCrossrefFlags(options, args);
const getInput = async () => {
if (flags.input) {
return Deno.readTextFileSync(flags.input);
} else {
// read input
const stdinContent = await readAll(Deno.stdin);
return new TextDecoder().decode(stdinContent);
}
};
const getOutputFile: () => string = () => (flags.output || "stdout");
const input = await getInput();
// create directory for indexing and write input into it
const indexingDir = globalTempContext().createDir();
// setup index file and input type
const indexFile = join(indexingDir, "index.json");
Deno.env.set("QUARTO_CROSSREF_INDEX_PATH", indexFile);
Deno.env.set("QUARTO_CROSSREF_INPUT_TYPE", "qmd");
// build command
const cmd = pandocBinaryPath();
const cmdArgs = ["+RTS", "-K512m", "-RTS"];
cmdArgs.push(...[
"--from",
resourcePath("filters/qmd-reader.lua"),
"--to",
"native",
"--data-dir",
resourcePath("pandoc/datadir"),
"--lua-filter",
resourcePath("filters/quarto-init/quarto-init.lua"),
"--lua-filter",
resourcePath("filters/crossref/crossref.lua"),
]);
// create filter params
const filterParams = encodeBase64(
JSON.stringify({
["crossref-index-file"]: "index.json",
["crossref-input-type"]: "qmd",
}),
);
// run pandoc
const result = await execProcess(
{
cmd,
args: cmdArgs,
cwd: indexingDir,
env: {
"QUARTO_FILTER_PARAMS": filterParams,
"QUARTO_SHARE_PATH": resourcePath(),
},
stdout: "piped",
},
input,
undefined, // mergeOutput?: "stderr>stdout" | "stdout>stderr",
undefined, // stderrFilter?: (output: string) => string,
undefined, // respectStreams?: boolean,
5000,
);
// check for error
if (!result.success) {
error("Error running Pandoc: " + result.stderr);
throw new Error(result.stderr);
}
const outputFile = getOutputFile();
if (outputFile === "stdout") {
// write back the index
Deno.stdout.writeSync(Deno.readFileSync(indexFile));
} else {
Deno.writeTextFileSync(outputFile, Deno.readTextFileSync(indexFile));
}
});
};
export const crossrefCommand = makeCrossrefCommand();