|
| 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; |
0 commit comments