49 lines
3.2 KiB
JavaScript
49 lines
3.2 KiB
JavaScript
#!/usr/bin/env node
|
|
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
/**
|
|
* Put a program from mojo/local/ next to a Quarto page, with everything the playground shows for it:
|
|
*
|
|
* pixi run quarto-export demo ../site/programmingplus/demo
|
|
*
|
|
* writes into that folder
|
|
* demo.mojo the program, header and all (the shortcode leaves the header out of the pane and listing)
|
|
* demo.asm the compiler's x86-64 for it, as the x86-64 pane shows it (from src/mojo/generated/listings.ts)
|
|
* demo.setup the setup block that goes with it: the arguments' registers and the listing's data
|
|
* and prints the shortcodes that embed the whole playground with them, and that run it with the page. Run `pixi run --manifest-path core/pixi.toml play demo` first so
|
|
* the listing is current; without one (no godbolt answer yet, or the program has no function of its own
|
|
* left at this build), only the .mojo is written and the shortcode says so.
|
|
*
|
|
* Every option the shortcode takes is at the top of quarto/_extensions/pplus/pplus.lua.
|
|
*/
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
|
const ROOT = fileURLToPath(new URL('..', import.meta.url));
|
|
const [name, outDir] = process.argv.slice(2).filter((a) => !a.startsWith('--'));
|
|
const die = (m) => { console.error(`quarto:export: ${m}`); process.exit(1); };
|
|
if (!name || !outDir) die('usage: pixi run quarto-export <program in mojo/local/> <folder beside the .qmd page>');
|
|
|
|
const src = path.join(ROOT, 'core/library/mojo/local', `${name}.mojo`);
|
|
if (!fs.existsSync(src)) die(`core/library/mojo/local/${name}.mojo does not exist`);
|
|
const listingsJs = path.join(ROOT, 'core/dist/mojo/generated/listings.js');
|
|
if (!fs.existsSync(listingsJs)) die('core/dist/ is missing; run `pixi run build` first');
|
|
const { MOJO_LISTINGS } = await import(pathToFileURL(listingsJs).href);
|
|
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
const wrote = [];
|
|
const put = (file, text) => { fs.writeFileSync(path.join(outDir, file), text.endsWith('\n') ? text : text + '\n'); wrote.push(file); };
|
|
put(`${name}.mojo`, fs.readFileSync(src, 'utf8'));
|
|
const listing = MOJO_LISTINGS[name];
|
|
if (listing) { put(`${name}.asm`, listing.asm); put(`${name}.setup`, listing.setup); }
|
|
|
|
const attrs = [`mojo="${name}.mojo"`];
|
|
if (listing) attrs.push(`asm="${name}.asm"`, `setup="${name}.setup"`);
|
|
attrs.push(`area="${listing ? 'hw' : 'dev'}"`, `title="${name}"`);
|
|
console.log(`wrote ${wrote.map((f) => path.join(outDir, f)).join(', ')}`);
|
|
if (!listing) console.log(`no x86-64 listing for ${name} yet: run \`pixi run --manifest-path core/pixi.toml play ${name}\` (with # mode: both and -O0 in its header), then export again`);
|
|
console.log(`\nin the page, the whole playground:\n\n{{< pplus-playground ${attrs.join(' ')} >}}\n`);
|
|
console.log(`or, rendered with the page (no iframe):\n\n{{< pplus-run mojo="${name}.mojo" code="1" >}}\n{{< pplus-lifetimes mojo="${name}.mojo" >}}\n`);
|
|
console.log('Other arrangements: panel="compiler" (open on the compiler\'s answer), build="default", dock="closed", editor="0.6"; {{< pplus-embed view="life" >}} for one live view. The gallery at #/quarto shows them all.');
|