Skip to content

Commit 8d0e6ba

Browse files
authored
resolve globs in template-partials
1 parent 5f5fe84 commit 8d0e6ba

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

news/changelog-1.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@
9393
- `QUARTO_PRINT_STACK` environment variable to print stack along with error messages
9494
- More compact download progress when installing Quarto tools in CI environments
9595
- Ignore case when loading date local files from `lang`
96+
97+
## Format Templates
98+
99+
- expand globs in template-partials (#1248)

src/command/render/pandoc.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export async function runPandoc(
429429
allDefaults[kTemplate];
430430

431431
// The user partials (if any)
432-
const userPartials = readPartials(options.format.metadata);
432+
const userPartials = readPartials(options.format.metadata, cwd);
433433
const inputDir = Deno.realPathSync(cwd);
434434
const resolvePath = (path: string) => {
435435
if (isAbsolute(path)) {
@@ -484,11 +484,7 @@ export async function runPandoc(
484484

485485
// Place any user partials at the end of the list of partials
486486
const partials: string[] = templateContext.partials || [];
487-
partials.push(
488-
...userPartials.map((path) => {
489-
return resolvePath(path);
490-
}),
491-
);
487+
partials.push(...userPartials);
492488

493489
// Stage the template and partials
494490
const stagedTemplate = await stageTemplate(

src/command/render/template.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2020 by RStudio, PBC
55
*
66
*/
7-
import { basename, join } from "path/mod.ts";
7+
import { basename, isAbsolute, join } from "path/mod.ts";
88
import {
99
kEmbedResources,
1010
kHtmlMathMethod,
@@ -22,15 +22,39 @@ import { copyTo } from "../../core/copy.ts";
2222
import { PandocOptions, RenderFlags } from "./types.ts";
2323
import * as ld from "../../core/lodash.ts";
2424
import { isHtmlDocOutput, isRevealjsOutput } from "../../config/format.ts";
25+
import { expandGlobSync } from "fs/mod.ts";
2526

2627
export const kPatchedTemplateExt = ".patched";
2728
export const kTemplatePartials = "template-partials";
2829

29-
export function readPartials(metadata: Metadata) {
30+
/**
31+
* read and expand template partial globs
32+
*
33+
* @param metadata
34+
* @param cwd current working directory for glob expansion
35+
*/
36+
export function readPartials(metadata: Metadata, inputDir?: string) {
3037
if (typeof (metadata?.[kTemplatePartials]) === "string") {
3138
metadata[kTemplatePartials] = [metadata[kTemplatePartials]];
3239
}
33-
return (metadata?.[kTemplatePartials] || []) as string[];
40+
const result = (metadata?.[kTemplatePartials] || []) as string[];
41+
42+
inputDir = inputDir ? Deno.realPathSync(inputDir) : undefined;
43+
const resolvePath = (path: string) => {
44+
if (!inputDir || isAbsolute(path)) {
45+
return path;
46+
} else {
47+
return join(inputDir, path);
48+
}
49+
};
50+
51+
return result.flatMap((path) => {
52+
const result = [];
53+
for (const walk of expandGlobSync(resolvePath(path))) {
54+
result.push(walk.path);
55+
}
56+
return result;
57+
});
3458
}
3559

3660
export async function stageTemplate(

src/format/ipynb/format-ipynb.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*/
77

8-
import { basename, join } from "path/mod.ts";
8+
import { basename, dirname, join } from "path/mod.ts";
99
import { readPartials } from "../../command/render/template.ts";
1010
import {
1111
kCellFormat,
@@ -31,7 +31,7 @@ export function ipynbFormat(): Format {
3131
[kDefaultImageExtension]: "png",
3232
},
3333
formatExtras: (
34-
_input: string,
34+
input: string,
3535
_markdown: string,
3636
_flags: PandocFlags,
3737
format: Format,
@@ -46,7 +46,7 @@ export function ipynbFormat(): Format {
4646
join("templates", "title-block.md"),
4747
);
4848

49-
const partials = readPartials(format.metadata);
49+
const partials = readPartials(format.metadata, dirname(input));
5050
if (partials.length > 0) {
5151
const userTitleTemplate = partials.find((part) => {
5252
return basename(part) === "title-block.md";

0 commit comments

Comments
 (0)