Skip to content

Commit a71b54e

Browse files
claude: Refine claimsLanguage semantics: false skips, any number claims
- false means "don't claim" (skip engine entirely) - true means claim with priority 1 - any number means claim with that priority (higher wins) Changed bestScore from 0 to -Infinity so any number can win. Added class-override-twice test verifying priority 3 beats priority 2. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 021ecce commit a71b54e

10 files changed

Lines changed: 257 additions & 8 deletions

File tree

‎packages/quarto-types/src/execution-engine.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export interface ExecutionEngineDiscovery {
9898
*
9999
* @param language - The language identifier (e.g., "python", "r", "julia")
100100
* @param firstClass - Optional first class from code block attributes (e.g., "marimo" from {python .marimo})
101-
* @returns boolean for simple claim, or number for priority (higher wins, 0 = no claim, 1 = standard claim)
101+
* @returns false to skip (don't claim), true to claim with priority 1, or any number for custom priority (higher wins)
102102
*/
103103
claimsLanguage: (language: string, firstClass?: string) => boolean | number;
104104

‎src/execute/engine.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,16 @@ export function markdownExecutionEngine(
176176
// see if there is an engine that claims this language (highest score wins)
177177
for (const [language, firstClass] of languagesWithClassesMap) {
178178
let bestEngine: ExecutionEngineDiscovery | undefined;
179-
let bestScore = 0;
179+
let bestScore = -Infinity;
180180

181181
for (const [_, engine] of reorderedEngines) {
182182
const claim = engine.claimsLanguage(language, firstClass);
183-
// Convert boolean to number for backwards compatibility
184-
const score = typeof claim === "boolean" ? (claim ? 1 : 0) : claim;
183+
// false means "don't claim", skip this engine entirely
184+
if (claim === false) {
185+
continue;
186+
}
187+
// true -> score 1, number -> use as score
188+
const score = claim === true ? 1 : claim;
185189
if (score > bestScore) {
186190
bestScore = score;
187191
bestEngine = engine;

‎src/execute/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface ExecutionEngineDiscovery {
5151
*
5252
* @param language - The language identifier (e.g., "python", "r", "julia")
5353
* @param firstClass - Optional first class from code block attributes (e.g., "marimo" from {python .marimo})
54-
* @returns boolean for simple claim, or number for priority (higher wins, 0 = no claim, 1 = standard claim)
54+
* @returns false to skip (don't claim), true to claim with priority 1, or any number for custom priority (higher wins)
5555
*/
5656
claimsLanguage: (language: string, firstClass?: string) => boolean | number;
5757
canFreeze: boolean;

‎src/resources/create/extensions/engine/src/qstart-filesafename-qend.ejs.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const exampleEngineDiscovery: ExecutionEngineDiscovery = {
5858
): boolean | number => {
5959
// This engine claims cells with its own language name.
6060
// The optional firstClass parameter allows claiming based on code block class
61-
// (e.g., {python.myengine} would have firstClass="myengine").
62-
// Return a number > 1 to override other engines that also claim this language.
61+
// (e.g., {python .myengine} would have firstClass="myengine").
62+
// Return false to skip, true to claim with priority 1, or any number for custom priority.
6363
return language.toLowerCase() === kCellLanguage.toLowerCase();
6464
},
6565

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Bar Engine
2+
author: Quarto Dev Team
3+
version: 1.0.0
4+
quarto-required: ">=1.9.17"
5+
contributes:
6+
engines:
7+
- path: bar-engine.js
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Engine to test priority-based engine override
2+
// Claims {python.foo} blocks with priority 3 (higher than foo-engine's 2)
3+
4+
let quarto;
5+
6+
const barEngineDiscovery = {
7+
init: (quartoAPI) => {
8+
quarto = quartoAPI;
9+
},
10+
11+
name: "bar",
12+
defaultExt: ".qmd",
13+
defaultYaml: () => ["engine: bar"],
14+
defaultContent: () => ["# Bar Engine Document"],
15+
validExtensions: () => [".qmd"],
16+
17+
claimsFile: (_file, _ext) => false,
18+
19+
claimsLanguage: (language, firstClass) => {
20+
// Claim python.foo with priority 3 (overrides foo-engine's 2)
21+
if (language === "python" && firstClass === "foo") {
22+
return 3;
23+
}
24+
return false; // Don't claim
25+
},
26+
27+
canFreeze: false,
28+
generatesFigures: false,
29+
30+
launch: (context) => {
31+
return {
32+
name: "bar",
33+
canFreeze: false,
34+
35+
markdownForFile: async (file) => {
36+
return quarto.mappedString.fromFile(file);
37+
},
38+
39+
target: async (file, _quiet, markdown) => {
40+
if (!markdown) {
41+
markdown = quarto.mappedString.fromFile(file);
42+
}
43+
const metadata = quarto.markdownRegex.extractYaml(markdown.value);
44+
return {
45+
source: file,
46+
input: file,
47+
markdown,
48+
metadata,
49+
};
50+
},
51+
52+
partitionedMarkdown: async (file) => {
53+
const markdown = quarto.mappedString.fromFile(file);
54+
return quarto.markdownRegex.partition(markdown.value);
55+
},
56+
57+
execute: async (options) => {
58+
const chunks = await quarto.markdownRegex.breakQuartoMd(
59+
options.target.markdown,
60+
);
61+
62+
const processedCells = [];
63+
for (const cell of chunks.cells) {
64+
if (
65+
typeof cell.cell_type === "object" &&
66+
cell.cell_type.language === "python"
67+
) {
68+
const header = cell.sourceVerbatim.value.split(/\r?\n/)[0];
69+
const hasClassFoo = /\.foo\b/.test(header);
70+
if (hasClassFoo) {
71+
processedCells.push(`::: {#bar-engine-marker .bar-engine-output}
72+
**BAR ENGINE PROCESSED THIS BLOCK**
73+
74+
Original code:
75+
\`\`\`python
76+
${cell.source.value.trim()}
77+
\`\`\`
78+
:::
79+
`);
80+
continue;
81+
}
82+
}
83+
processedCells.push(cell.sourceVerbatim.value);
84+
}
85+
86+
return {
87+
markdown: processedCells.join(""),
88+
supporting: [],
89+
filters: [],
90+
};
91+
},
92+
93+
dependencies: async (_options) => {
94+
return { includes: {} };
95+
},
96+
97+
postprocess: async (_options) => {},
98+
};
99+
},
100+
};
101+
102+
export default barEngineDiscovery;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Foo Engine
2+
author: Quarto Dev Team
3+
version: 1.0.0
4+
quarto-required: ">=1.9.17"
5+
contributes:
6+
engines:
7+
- path: foo-engine.js
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Minimal engine to test class-based engine override
2+
// Claims {python.foo} blocks with priority 2 (higher than Jupyter's 1)
3+
4+
let quarto;
5+
6+
const fooEngineDiscovery = {
7+
init: (quartoAPI) => {
8+
quarto = quartoAPI;
9+
},
10+
11+
name: "foo",
12+
defaultExt: ".qmd",
13+
defaultYaml: () => ["engine: foo"],
14+
defaultContent: () => ["# Foo Engine Document"],
15+
validExtensions: () => [".qmd"],
16+
17+
claimsFile: (_file, _ext) => false,
18+
19+
claimsLanguage: (language, firstClass) => {
20+
// Claim python.foo with priority 2 (overrides Jupyter's 1)
21+
if (language === "python" && firstClass === "foo") {
22+
return 2;
23+
}
24+
return false; // Don't claim
25+
},
26+
27+
canFreeze: false,
28+
generatesFigures: false,
29+
30+
launch: (context) => {
31+
return {
32+
name: "foo",
33+
canFreeze: false,
34+
35+
markdownForFile: async (file) => {
36+
return quarto.mappedString.fromFile(file);
37+
},
38+
39+
target: async (file, _quiet, markdown) => {
40+
if (!markdown) {
41+
markdown = quarto.mappedString.fromFile(file);
42+
}
43+
const metadata = quarto.markdownRegex.extractYaml(markdown.value);
44+
return {
45+
source: file,
46+
input: file,
47+
markdown,
48+
metadata,
49+
};
50+
},
51+
52+
partitionedMarkdown: async (file) => {
53+
const markdown = quarto.mappedString.fromFile(file);
54+
return quarto.markdownRegex.partition(markdown.value);
55+
},
56+
57+
execute: async (options) => {
58+
const chunks = await quarto.markdownRegex.breakQuartoMd(
59+
options.target.markdown,
60+
);
61+
62+
const processedCells = [];
63+
for (const cell of chunks.cells) {
64+
if (
65+
typeof cell.cell_type === "object" &&
66+
cell.cell_type.language === "python"
67+
) {
68+
const header = cell.sourceVerbatim.value.split(/\r?\n/)[0];
69+
const hasClassFoo = /\.foo\b/.test(header);
70+
if (hasClassFoo) {
71+
processedCells.push(`::: {#foo-engine-marker .foo-engine-output}
72+
**FOO ENGINE PROCESSED THIS BLOCK**
73+
74+
Original code:
75+
\`\`\`python
76+
${cell.source.value.trim()}
77+
\`\`\`
78+
:::
79+
`);
80+
continue;
81+
}
82+
}
83+
processedCells.push(cell.sourceVerbatim.value);
84+
}
85+
86+
return {
87+
markdown: processedCells.join(""),
88+
supporting: [],
89+
filters: [],
90+
};
91+
},
92+
93+
dependencies: async (_options) => {
94+
return { includes: {} };
95+
},
96+
97+
postprocess: async (_options) => {},
98+
};
99+
},
100+
};
101+
102+
export default fooEngineDiscovery;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Engine Class Override Priority Test
3+
_quarto:
4+
tests:
5+
html:
6+
ensureHtmlElements:
7+
-
8+
- "#bar-engine-marker"
9+
- ".bar-engine-output"
10+
- []
11+
ensureFileRegexMatches:
12+
-
13+
- "BAR ENGINE PROCESSED THIS BLOCK"
14+
- []
15+
---
16+
17+
This document tests that `{python .foo}` blocks are processed by bar-engine
18+
(priority 3) instead of foo-engine (priority 2), verifying that higher
19+
priority numbers win.
20+
21+
```{python .foo}
22+
x = 1 + 1
23+
print(x)
24+
```
25+
26+
The block above should show "BAR ENGINE PROCESSED THIS BLOCK" because
27+
bar-engine claims with priority 3, which is higher than foo-engine's priority 2.

‎tests/docs/smoke-all/engine/class-override/_extensions/foo-engine/foo-engine.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const fooEngineDiscovery = {
2121
if (language === "python" && firstClass === "foo") {
2222
return 2;
2323
}
24-
return 0;
24+
return false; // Don't claim
2525
},
2626

2727
canFreeze: false,

0 commit comments

Comments
 (0)