Skip to content

Commit

Permalink
Determine engine from first arg in codeblock-to-json.ts (#70)
Browse files Browse the repository at this point in the history
Co-Authored-By: George Stagg <[email protected]>
  • Loading branch information
schloerke and georgestagg authored Oct 4, 2023
1 parent 0328b04 commit 0cd59dc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Components/HeaderBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import shinyPyLogo from "../assets/shiny-for-python.svg";
import shinyRLogo from "../assets/shiny-logo.svg";
import { engineSwitch } from "../utils";
import { AppEngine } from "./App";
import "./HeaderBar.css";
import { Icon } from "./Icons";
Expand Down Expand Up @@ -122,8 +123,8 @@ export default function HeaderBar({
const mainUrl = {
python: "https://shiny.posit.co/py/",
r: "https://shiny.posit.co/",
}
const shinyLogo = appEngine === "python" ? shinyPyLogo : shinyRLogo;
};
const shinyLogo = engineSwitch(appEngine, shinyRLogo, shinyPyLogo);

return (
<div className="HeaderBar">
Expand Down
13 changes: 9 additions & 4 deletions src/parse-codeblock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { load as yamlLoad } from "js-yaml";
import { AppEngine } from "./Components/App";
import type { FileContent } from "./Components/filecontent";
import { load as yamlLoad } from "js-yaml";
import { engineSwitch } from "./utils";

export type Component = "editor" | "terminal" | "viewer" | "examples" | "cell";

Expand Down Expand Up @@ -38,7 +39,10 @@ export type QuartoArgs = {
* iVBORw0KGgoAAAANSUhEUgAAACgAAAAuCAYAAABap1twAAAABGdBTUEAALGPC ...
* ------------------------------
*/
export function parseCodeBlock(codeblock: string | string[], engine: AppEngine): {
export function parseCodeBlock(
codeblock: string | string[],
engine: AppEngine
): {
files: FileContent[];
quartoArgs: Required<QuartoArgs>;
} {
Expand Down Expand Up @@ -66,15 +70,16 @@ export function parseCodeBlock(codeblock: string | string[], engine: AppEngine):
);
}
// For shiny apps, the default filename is "app.py" or "app.R".
defaultFilename = engine === 'python' ? 'app.py' : 'app.R';
defaultFilename = engineSwitch(engine, "app.R", "app.py");
} else {
// In the case of editor-terminal and editor-cell components...
if (quartoArgs.standalone !== false) {
throw new Error(
"'#| standalone: true' is not valid for editor-terminal and editor-cell code blocks."
);
}
defaultFilename = "code.py";

defaultFilename = engineSwitch(engine, "code.R", "code.py");
}

const files = parseFileContents(lines, defaultFilename);
Expand Down
5 changes: 4 additions & 1 deletion src/scripts/codeblock-to-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// import to work at run time, because the output path structure is different.
import { readLines } from "https://deno.land/std/io/mod.ts";

import type { AppEngine } from "../Components/App";
import { parseCodeBlock } from "../parse-codeblock";

const { args } = Deno;
Expand All @@ -20,7 +21,9 @@ const lines: string[] = [];
for await (const line of readLines(Deno.stdin)) {
lines.push(line);
}
// Default to python to support legacy codeblocks with an old version of shinylive quarto extension
const engine: AppEngine = args.length > 0 && args[0] == "r" ? "r" : "python";

const content = parseCodeBlock(lines);
const content = parseCodeBlock(lines, engine);

await Deno.stdout.write(new TextEncoder().encode(JSON.stringify(content)));
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AppEngine } from "./Components/App";

// =======================================================================
// Utility functions
// =======================================================================
Expand Down Expand Up @@ -141,3 +143,19 @@ export function stringToUint8Array(s: string): Uint8Array {
}
return bytes;
}

export function engineSwitch(
engine: AppEngine,
rValue: string,
pythonValue: string
): string {
switch (engine) {
case "r":
return rValue;

// Legacy default engine value was `python`
case "python":
default:
return pythonValue;
}
}

0 comments on commit 0cd59dc

Please sign in to comment.