Skip to content

Commit 0608a19

Browse files
committed
feat: make outputHanzi works for ide and site
1 parent 50dfd35 commit 0608a19

File tree

3 files changed

+22
-49
lines changed

3 files changed

+22
-49
lines changed

src/parser.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ function hanzinize(value) {
772772
} else if (typeof value == "boolean") {
773773
return bool2hanzi(value);
774774
} else if (Array.isArray(value)) {
775-
return value.map(i => hanzinize(i));
775+
return value.map(i => hanzinize(i)).join("。");
776776
} else {
777777
return value;
778778
}
@@ -785,7 +785,12 @@ function outputHanziWrapper(log) {
785785
}
786786

787787
function evalCompiled(compiledCode, options = {}) {
788-
const { outputHanzi = true, scoped = false, lang = "js" } = options;
788+
const {
789+
outputHanzi = true,
790+
scoped = false,
791+
lang = "js",
792+
output = console.log
793+
} = options;
789794

790795
isLangSupportedForEval(lang);
791796

@@ -794,7 +799,7 @@ function evalCompiled(compiledCode, options = {}) {
794799
(() => {
795800
const _console_log = console.log;
796801
if (outputHanzi) {
797-
console.log = outputHanziWrapper(_console_log);
802+
console.log = outputHanziWrapper(output);
798803
}
799804
try {
800805
if (!scoped && "window" in this) {

tools/make_ide.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ function main() {
136136
selr.onchange = run;
137137

138138
var hidestd = document.getElementById("hide-std");
139+
var outdiv = document.getElementById("out");
140+
139141
hidestd.onchange = run;
140142

141143
function run() {
@@ -153,27 +155,16 @@ function main() {
153155

154156
document.getElementById("js").innerText = js_beautify(showcode);
155157
hljs.highlightBlock(document.getElementById("js"));
156-
code = code.replace(/console.log\(/g, `log2div(`);
157-
eval(code);
158+
159+
evalCompiled(code, {
160+
output: (...args) => {
161+
outdiv.innerText += args.join(" ") + "\n";
162+
}
163+
});
158164
}
159165

160-
// document.getElementById("in").append(ln);
161166
document.getElementById("in").append(ed);
162-
163167
document.getElementById("run").onclick = run;
164-
function log2div() {
165-
var outdiv = document.getElementById("out");
166-
for (var i = 0; i < arguments.length; i++) {
167-
if (typeof arguments[i] == "number") {
168-
outdiv.innerText += num2hanzi(arguments[i]);
169-
} else if (typeof arguments[i] == "boolean") {
170-
outdiv.innerText += bool2hanzi(arguments[i]);
171-
} else {
172-
outdiv.innerText += arguments[i];
173-
}
174-
}
175-
outdiv.innerText += "\n";
176-
}
177168
run();
178169
}
179170

tools/make_site.js

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ function load_svg(pth) {
2525
svg = svg.replace(/(\d+)\.(\d)\d*/g, `$1.$2`);
2626
svg = svg.replace(/width=".+?"/, `width="100%"`);
2727
svg = svg.replace(/height=".+?"/, ``);
28-
console.log(svg);
2928
return svg;
3029
}
3130

@@ -45,7 +44,7 @@ function main() {
4544
romanizeIdentifiers: "none",
4645
errorCallback: function(x) {
4746
hasError = true;
48-
log2div(i, x);
47+
louts[i].innerText = x;
4948
}
5049
});
5150
if (i == 0) {
@@ -64,36 +63,14 @@ function main() {
6463
document.getElementById("rb").innerText =
6564
"# Ruby\n" + rb.split("#####\n")[1];
6665
}
67-
// hljs.highlightBlock(document.getElementById("js"));
68-
if (!hasError) {
69-
code = code.replace(/console.log\(/g, `log2div(` + i + ",");
70-
eval(code);
71-
}
72-
}
7366

74-
function log2div() {
75-
// alert(arguments[1])
76-
if (arguments[1] instanceof Array && arguments.length == 2) {
77-
var l = [];
78-
for (var i = 0; i < arguments[1].length; i++) {
79-
if (i != 0) {
80-
l.push("。");
67+
if (!hasError) {
68+
evalCompiled(code, {
69+
output: (...args) => {
70+
outs[i].innerText += args.join(" ") + "\n";
8171
}
82-
l.push(arguments[1][i]);
83-
}
84-
return log2div(arguments[0], ...l);
85-
}
86-
var outdiv = outs[arguments[0]];
87-
for (var i = 1; i < arguments.length; i++) {
88-
if (typeof arguments[i] == "number") {
89-
outdiv.innerText += num2hanzi(arguments[i]);
90-
} else if (typeof arguments[i] == "boolean") {
91-
outdiv.innerText += bool2hanzi(arguments[i]);
92-
} else {
93-
outdiv.innerText += arguments[i];
94-
}
72+
});
9573
}
96-
outdiv.innerText += "\n";
9774
}
9875

9976
function loop() {

0 commit comments

Comments
 (0)