Skip to content

Commit

Permalink
adding tutorial option for a python template (#10097)
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Sep 12, 2024
1 parent 329ecb5 commit d6bd8e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ declare namespace pxt.tutorial {
code: string[]; // all code
language?: string; // language of code snippet (ts or python)
templateCode?: string;
templateLanguage?: string; // language of template code
metadata?: TutorialMetadata;
assetFiles?: pxt.Map<string>;
jres?: string; // JRES to be used when generating hints; necessary for tilemaps
Expand Down Expand Up @@ -1258,6 +1259,7 @@ declare namespace pxt.tutorial {
tutorialCode?: string[]; // all tutorial code bundled
tutorialRecipe?: boolean; // micro tutorial running within the context of a script
templateCode?: string;
templateLanguage?: string;
mergeHeaderId?: string;
mergeCarryoverCode?: boolean;
autoexpandStep?: boolean; // autoexpand tutorial card if instruction text overflows
Expand Down
23 changes: 22 additions & 1 deletion pxtlib/tutorial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace pxt.tutorial {
const {
code,
templateCode,
templateLanguage,
editor,
language,
jres,
Expand Down Expand Up @@ -51,6 +52,7 @@ namespace pxt.tutorial {
activities,
code,
templateCode,
templateLanguage,
metadata,
language,
jres,
Expand All @@ -63,7 +65,7 @@ namespace pxt.tutorial {
}

export function getMetadataRegex(): RegExp {
return /``` *(sim|block|blocks|filterblocks|spy|ghost|typescript|ts|js|javascript|template|python|jres|assetjson|customts|simtheme)\s*\n([\s\S]*?)\n```/gmi;
return /``` *(sim|block|blocks|filterblocks|spy|ghost|typescript|ts|js|javascript|template|python|jres|assetjson|customts|simtheme|python-template|ts-template|typescript-template|js-template|javascript-template)\s*\n([\s\S]*?)\n```/gmi;
}

function computeBodyMetadata(body: string) {
Expand All @@ -74,6 +76,7 @@ namespace pxt.tutorial {
let jres: string;
let code: string[] = [];
let templateCode: string;
let templateLanguage: string;
let language: string;
let idx = 0;
let assetJson: string;
Expand Down Expand Up @@ -109,6 +112,22 @@ namespace pxt.tutorial {
case "template":
templateCode = m2;
break;
case "python-template":
if (!checkTutorialEditor(pxt.PYTHON_PROJECT_NAME))
return undefined;
templateCode = m2;
templateLanguage = "python";
language = "python";
break;
case "ts-template":
case "typescript-template":
case "js-template":
case "javascript-template":
if (!checkTutorialEditor(pxt.JAVASCRIPT_PROJECT_NAME))
return undefined;
templateCode = m2;
templateLanguage = "typescript";
break;
case "jres":
jres = m2;
break;
Expand All @@ -132,6 +151,7 @@ namespace pxt.tutorial {
return {
code,
templateCode,
templateLanguage,
editor,
language,
jres,
Expand Down Expand Up @@ -443,6 +463,7 @@ ${code}
tutorialCode: tutorialInfo.code,
tutorialRecipe: !!recipe,
templateCode: tutorialInfo.templateCode,
templateLanguage: tutorialInfo.templateLanguage,
autoexpandStep: tutorialInfo.metadata?.autoexpandOff ? false : true,
metadata: tutorialInfo.metadata,
language: tutorialInfo.language,
Expand Down
6 changes: 4 additions & 2 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1974,8 +1974,10 @@ export class ProjectView

const projectname = projectNameForEditor(preferredEditor || header.editor);


if (projectname === pxt.JAVASCRIPT_PROJECT_NAME) {
if (projectname === pxt.PYTHON_PROJECT_NAME && header.tutorial.templateLanguage === "python") {
currentText[pxt.MAIN_PY] = template;
}
else if (projectname === pxt.JAVASCRIPT_PROJECT_NAME) {
currentText[pxt.MAIN_TS] = template;
}
else if (projectname === pxt.PYTHON_PROJECT_NAME) {
Expand Down

0 comments on commit d6bd8e2

Please sign in to comment.