Skip to content

Commit 0cd59dc

Browse files
Determine engine from first arg in codeblock-to-json.ts (#70)
Co-Authored-By: George Stagg <982548+georgestagg@users.noreply.github.com>
1 parent 0328b04 commit 0cd59dc

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/Components/HeaderBar.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import shinyPyLogo from "../assets/shiny-for-python.svg";
22
import shinyRLogo from "../assets/shiny-logo.svg";
3+
import { engineSwitch } from "../utils";
34
import { AppEngine } from "./App";
45
import "./HeaderBar.css";
56
import { Icon } from "./Icons";
@@ -122,8 +123,8 @@ export default function HeaderBar({
122123
const mainUrl = {
123124
python: "https://shiny.posit.co/py/",
124125
r: "https://shiny.posit.co/",
125-
}
126-
const shinyLogo = appEngine === "python" ? shinyPyLogo : shinyRLogo;
126+
};
127+
const shinyLogo = engineSwitch(appEngine, shinyRLogo, shinyPyLogo);
127128

128129
return (
129130
<div className="HeaderBar">

src/parse-codeblock.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { load as yamlLoad } from "js-yaml";
12
import { AppEngine } from "./Components/App";
23
import type { FileContent } from "./Components/filecontent";
3-
import { load as yamlLoad } from "js-yaml";
4+
import { engineSwitch } from "./utils";
45

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

@@ -38,7 +39,10 @@ export type QuartoArgs = {
3839
* iVBORw0KGgoAAAANSUhEUgAAACgAAAAuCAYAAABap1twAAAABGdBTUEAALGPC ...
3940
* ------------------------------
4041
*/
41-
export function parseCodeBlock(codeblock: string | string[], engine: AppEngine): {
42+
export function parseCodeBlock(
43+
codeblock: string | string[],
44+
engine: AppEngine
45+
): {
4246
files: FileContent[];
4347
quartoArgs: Required<QuartoArgs>;
4448
} {
@@ -66,15 +70,16 @@ export function parseCodeBlock(codeblock: string | string[], engine: AppEngine):
6670
);
6771
}
6872
// For shiny apps, the default filename is "app.py" or "app.R".
69-
defaultFilename = engine === 'python' ? 'app.py' : 'app.R';
73+
defaultFilename = engineSwitch(engine, "app.R", "app.py");
7074
} else {
7175
// In the case of editor-terminal and editor-cell components...
7276
if (quartoArgs.standalone !== false) {
7377
throw new Error(
7478
"'#| standalone: true' is not valid for editor-terminal and editor-cell code blocks."
7579
);
7680
}
77-
defaultFilename = "code.py";
81+
82+
defaultFilename = engineSwitch(engine, "code.R", "code.py");
7883
}
7984

8085
const files = parseFileContents(lines, defaultFilename);

src/scripts/codeblock-to-json.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// import to work at run time, because the output path structure is different.
1313
import { readLines } from "https://deno.land/std/io/mod.ts";
1414

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

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

24-
const content = parseCodeBlock(lines);
27+
const content = parseCodeBlock(lines, engine);
2528

2629
await Deno.stdout.write(new TextEncoder().encode(JSON.stringify(content)));

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { AppEngine } from "./Components/App";
2+
13
// =======================================================================
24
// Utility functions
35
// =======================================================================
@@ -141,3 +143,19 @@ export function stringToUint8Array(s: string): Uint8Array {
141143
}
142144
return bytes;
143145
}
146+
147+
export function engineSwitch(
148+
engine: AppEngine,
149+
rValue: string,
150+
pythonValue: string
151+
): string {
152+
switch (engine) {
153+
case "r":
154+
return rValue;
155+
156+
// Legacy default engine value was `python`
157+
case "python":
158+
default:
159+
return pythonValue;
160+
}
161+
}

0 commit comments

Comments
 (0)