Skip to content

Commit f6e1e92

Browse files
feat: add QUARTO_PDF_STANDARD environment variable fallback
When no pdf-standard is set in document YAML, fall back to the QUARTO_PDF_STANDARD environment variable (comma-separated, e.g. "ua-1" or "a-2b,ua-1"). This applies to LaTeX, Typst, and PDF format resolution. Includes a smoke test that verifies the env var produces the expected \DocumentMetadata in LaTeX output.
1 parent 22a1428 commit f6e1e92

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

src/command/render/output-tex.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
kPdfStandard,
1919
kPdfStandardApplied,
2020
kTargetFormat,
21+
pdfStandardEnv,
2122
} from "../../config/constants.ts";
2223
import { Format } from "../../config/types.ts";
2324
import { asArray } from "../../core/array.ts";
@@ -90,7 +91,8 @@ export function texToPdfOutputRecipe(
9091
const pdfStandards = asArray(
9192
pandocOptions.format.metadata?.[kPdfStandardApplied] ??
9293
format.render?.[kPdfStandard] ??
93-
format.metadata?.[kPdfStandard],
94+
format.metadata?.[kPdfStandard] ??
95+
pdfStandardEnv(),
9496
) as string[];
9597
if (pdfStandards.length > 0) {
9698
await validatePdfStandards(pdfOutput, pdfStandards, {

src/command/render/output-typst.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
kOutputFile,
3535
kPdfStandard,
3636
kVariant,
37+
pdfStandardEnv,
3738
} from "../../config/constants.ts";
3839
import { error, warning } from "../../deno_ral/log.ts";
3940
import { ErrorEx } from "../../core/lib/error.ts";
@@ -158,7 +159,8 @@ export function typstPdfOutputRecipe(
158159
),
159160
pdfStandard: normalizePdfStandardForTypst(
160161
asArray(
161-
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard],
162+
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard] ??
163+
pdfStandardEnv(),
162164
),
163165
),
164166
};
@@ -185,7 +187,8 @@ export function typstPdfOutputRecipe(
185187

186188
// Validate PDF against specified standards using verapdf (if available)
187189
const pdfStandards = asArray(
188-
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard],
190+
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard] ??
191+
pdfStandardEnv(),
189192
) as string[];
190193
if (pdfStandards.length > 0) {
191194
await validatePdfStandards(pdfOutput, pdfStandards, {

src/config/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ export const kKeepTex = "keep-tex";
8888
export const kKeepTyp = "keep-typ";
8989
export const kPdfStandard = "pdf-standard";
9090
export const kPdfStandardApplied = "pdf-standard-applied";
91+
92+
/** Read QUARTO_PDF_STANDARD env var as a fallback for pdf-standard option. */
93+
export function pdfStandardEnv(): string[] | undefined {
94+
const val = Deno.env.get("QUARTO_PDF_STANDARD");
95+
if (val) {
96+
return val.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
97+
}
98+
return undefined;
99+
}
91100
export const kKeepIpynb = "keep-ipynb";
92101
export const kKeepSource = "keep-source";
93102
export const kVariant = "variant";

src/format/pdf/format-pdf.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
kTblCapLoc,
3939
kTopLevelDivision,
4040
kWarning,
41+
pdfStandardEnv,
4142
} from "../../config/constants.ts";
4243
import { warning } from "../../deno_ral/log.ts";
4344
import { asArray } from "../../core/array.ts";
@@ -326,7 +327,8 @@ function createPdfFormat(
326327

327328
// Handle pdf-standard option for PDF/A, PDF/UA, PDF/X conformance
328329
const pdfStandard = asArray(
329-
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard],
330+
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard] ??
331+
pdfStandardEnv(),
330332
);
331333
if (pdfStandard.length > 0) {
332334
const { version, standards, needsTagging } =
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: PDF Standard from Environment Variable
3+
lang: en
4+
format:
5+
pdf:
6+
keep-tex: true
7+
---
8+
9+
This document does not set `pdf-standard` in its YAML.
10+
It should only get PDF standard settings from the `QUARTO_PDF_STANDARD` environment variable.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* render-pdf-standard-env.test.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*
6+
* Tests that QUARTO_PDF_STANDARD environment variable is used as a fallback
7+
* when no pdf-standard is set in the document YAML.
8+
*/
9+
10+
import { docs, outputForInput } from "../../utils.ts";
11+
import { ensureLatexFileRegexMatches } from "../../verify.ts";
12+
import { testRender } from "./render.ts";
13+
14+
const input = docs("render/pdf-standard-env.qmd");
15+
const output = outputForInput(input, "pdf");
16+
17+
// Test that QUARTO_PDF_STANDARD env var applies ua-2 even though
18+
// the document has no pdf-standard in its YAML
19+
testRender(input, "pdf", true, [
20+
ensureLatexFileRegexMatches(
21+
output.outputPath,
22+
[/\\DocumentMetadata\{/, /pdfstandard=\{ua-2\}/, /tagging=on/],
23+
[],
24+
input,
25+
),
26+
], {
27+
env: {
28+
QUARTO_PDF_STANDARD: "ua-2",
29+
},
30+
});

0 commit comments

Comments
 (0)