Skip to content

Commit

Permalink
feat: solve html entities when it would be python
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezcarlosjr committed Nov 19, 2023
1 parent 46831c1 commit 933a897
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/app/notebook/cellTypes/languages/JavaScriptMainThread.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { exec } from "../../shell/exec";
import { JavaScript } from "./JavaScript";
import '../../shell/protocols';
import {stringify} from "../stringToHTML";

export class JavaScriptMainThread extends JavaScript {
override dispatchShellRun() {
Expand All @@ -14,4 +15,4 @@ export class JavaScriptMainThread extends JavaScript {
override get name() {
return 'javascript main thread';
}
}
}
20 changes: 2 additions & 18 deletions src/app/notebook/cellTypes/languages/Python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@ import {Language} from "./language";
import {Observable, shareReplay} from "rxjs";
import {Extension} from "@codemirror/state";
import {python} from "@codemirror/lang-python";

/**
* @param {string} key
* @param {any} value
*/
function jsonStringifyToObjectReplacer(key: string, value: any) {
if (value && value.toObject) {
return value.toObject();
}
if (value && value.toJs) {
return value.toString();
}
if (value && value.toJSON) {
return value.toJSON();
}
return value;
}
import {jsonStringifyToObjectReplacer, stringify} from "../stringToHTML";


const pyodide = new Observable<{
Expand Down Expand Up @@ -79,7 +63,7 @@ ${this.mostRecentCode}`;
await instance.runPythonAsync(code)
.then((output: any) => {
if (output !== undefined) {
this.write(JSON.stringify(output, jsonStringifyToObjectReplacer));
this.write(stringify(JSON.stringify(output, jsonStringifyToObjectReplacer)));
}
this.stop();
}).catch((e: any) => {
Expand Down
23 changes: 22 additions & 1 deletion src/app/notebook/cellTypes/stringToHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
*/
export function stringToHTML(str: string): ChildNode {
const parser = new DOMParser();
const doc = parser.parseFromString(str.replace(/[\u00A0-\u9999<>&]/g, i => '&#'+i.charCodeAt(0)+';'), 'text/html');
const doc = parser.parseFromString(str, 'text/html');
return doc.body.firstChild as ChildNode;
}

/**
* @param {string} key
* @param {any} value
*/
export function jsonStringifyToObjectReplacer(key: string, value: any) {
if (value && value.toObject) {
return value.toObject();
}
if (value && value.toJs) {
return value.toString().replace(/[\u00A0-\u9999<>&]/g, (i: string) => '&#' + i.charCodeAt(0) + ';');
}
if (value && value.toJSON) {
return value.toJSON();
}
return value;
}

export function stringify(value: any) {
return JSON.stringify(value, jsonStringifyToObjectReplacer);
}
3 changes: 2 additions & 1 deletion src/app/notebook/notebook.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<mat-toolbar *ngIf="!isMode2">
<mat-toolbar *ngIf="!hideMainToolbar">
<button mat-button [matMenuTriggerFor]="fileMenu">File</button>
<mat-menu #fileMenu="matMenu">
<input mat-menu-item [(ngModel)]="name" (ngModelChange)="updateName($event)" id="doc-name" class="doc-name" maxlength="259" autocomplete="off" aria-label="Notebook name" data-form-type="name" data-lpignore="true">
Expand All @@ -16,6 +16,7 @@
<mat-menu #runtimeMenu="matMenu">
<button mat-menu-item (click)="runAll()">Run all (Ctrl+F9)</button>
<button mat-menu-item (click)="stopAll()">Stop all</button>
<button mat-menu-item (click)="modeZen()">Active mode zen</button>
</mat-menu>
<button mat-button [matMenuTriggerFor]="helpMenu">Help</button>
<mat-menu #helpMenu="matMenu">
Expand Down
6 changes: 6 additions & 0 deletions src/app/notebook/notebook.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class NotebookComponent implements OnInit {
isMode2: boolean = true;
loading: boolean = true;
name: string = "";
hideMainToolbar = false;
constructor(
private injector: Injector,
private _snackBar: MatSnackBar,
Expand All @@ -52,6 +53,7 @@ export class NotebookComponent implements OnInit {
async ngOnInit() {
const EditorJS = await import("@editorjs/editorjs");
this.isMode2 = url.read("m") === "2";
this.hideMainToolbar = this.isMode2;
const editor = new EditorJS.default({
holder: 'editor-js',
autofocus: true,
Expand Down Expand Up @@ -272,4 +274,8 @@ export class NotebookComponent implements OnInit {
openRecent() {
this.dialog.open(HistoryComponent);
}

modeZen() {
this.hideMainToolbar = true;
}
}

0 comments on commit 933a897

Please sign in to comment.