@hpcc-js/observablehq-compiler 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +43 -0
- package/README.md +105 -105
- package/bin/ojscc.mjs +74 -74
- package/dist/index.esm.js +554 -554
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +4 -0
- package/dist/index.esm.min.js.map +1 -0
- package/dist/index.js +6700 -6696
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +4 -0
- package/dist/index.min.js.map +1 -0
- package/package.json +4 -3
- package/src/__package__.ts +3 -3
- package/src/__tests__/File Attachments.ts +894 -894
- package/src/__tests__/Introduction to Imports.ts +748 -748
- package/src/__tests__/Observable TimeChart.ts +771 -771
- package/src/__tests__/index.ts +13 -13
- package/src/__tests__/node.ts +177 -177
- package/src/__tests__/tsconfig.json +20 -20
- package/src/compiler.md +234 -234
- package/src/compiler.ts +264 -264
- package/src/cst.ts +172 -172
- package/src/index.css +459 -459
- package/src/index.ts +7 -7
- package/src/util.md +113 -113
- package/src/util.ts +153 -153
- package/src/writer.ts +80 -80
- package/types/__package__.d.ts +3 -3
- package/types/__tests__/File Attachments.d.ts +109 -109
- package/types/__tests__/Introduction to Imports.d.ts +119 -119
- package/types/__tests__/Observable TimeChart.d.ts +110 -110
- package/types/__tests__/index.d.ts +1 -1
- package/types/__tests__/node.d.ts +1 -1
- package/types/compiler.d.ts +87 -87
- package/types/cst.d.ts +41 -41
- package/types/index.d.ts +5 -5
- package/types/util.d.ts +25 -25
- package/types/writer.d.ts +18 -18
- package/types-3.4/__package__.d.ts +4 -0
- package/types-3.4/__tests__/File Attachments.d.ts +110 -0
- package/types-3.4/__tests__/Introduction to Imports.d.ts +120 -0
- package/types-3.4/__tests__/Observable TimeChart.d.ts +111 -0
- package/types-3.4/__tests__/index.d.ts +2 -0
- package/types-3.4/__tests__/node.d.ts +2 -0
- package/types-3.4/compiler.d.ts +91 -0
- package/types-3.4/cst.d.ts +42 -0
- package/types-3.4/index.d.ts +6 -0
- package/types-3.4/util.d.ts +26 -0
- package/types-3.4/writer.d.ts +19 -0
package/dist/index.esm.js
CHANGED
|
@@ -6600,568 +6600,568 @@ function parseFeatures(cell, input) {
|
|
|
6600
6600
|
return cell;
|
|
6601
6601
|
}
|
|
6602
6602
|
|
|
6603
|
-
class ModuleParser extends CellParser {
|
|
6604
|
-
parseTopLevel(node) {
|
|
6605
|
-
if (!node.cells)
|
|
6606
|
-
node.cells = [];
|
|
6607
|
-
// @ts-ignore
|
|
6608
|
-
while (this.type !== types$1.eof) {
|
|
6609
|
-
// @ts-ignore
|
|
6610
|
-
const cell = this.parseCell(this.startNode());
|
|
6611
|
-
// @ts-ignore
|
|
6612
|
-
cell.input = this.input;
|
|
6613
|
-
node.cells.push(cell);
|
|
6614
|
-
}
|
|
6615
|
-
// @ts-ignore
|
|
6616
|
-
this.next();
|
|
6617
|
-
// @ts-ignore
|
|
6618
|
-
return this.finishNode(node, "Program");
|
|
6619
|
-
}
|
|
6620
|
-
}
|
|
6621
|
-
// @ts-ignore
|
|
6622
|
-
function parseModule(input, { globals } = {}) {
|
|
6623
|
-
// @ts-ignore
|
|
6624
|
-
return ModuleParser.parse(input).cells.map(cell => input.substring(cell.start, cell.end));
|
|
6603
|
+
class ModuleParser extends CellParser {
|
|
6604
|
+
parseTopLevel(node) {
|
|
6605
|
+
if (!node.cells)
|
|
6606
|
+
node.cells = [];
|
|
6607
|
+
// @ts-ignore
|
|
6608
|
+
while (this.type !== types$1.eof) {
|
|
6609
|
+
// @ts-ignore
|
|
6610
|
+
const cell = this.parseCell(this.startNode());
|
|
6611
|
+
// @ts-ignore
|
|
6612
|
+
cell.input = this.input;
|
|
6613
|
+
node.cells.push(cell);
|
|
6614
|
+
}
|
|
6615
|
+
// @ts-ignore
|
|
6616
|
+
this.next();
|
|
6617
|
+
// @ts-ignore
|
|
6618
|
+
return this.finishNode(node, "Program");
|
|
6619
|
+
}
|
|
6620
|
+
}
|
|
6621
|
+
// @ts-ignore
|
|
6622
|
+
function parseModule(input, { globals } = {}) {
|
|
6623
|
+
// @ts-ignore
|
|
6624
|
+
return ModuleParser.parse(input).cells.map(cell => input.substring(cell.start, cell.end));
|
|
6625
6625
|
}
|
|
6626
6626
|
|
|
6627
|
-
const FuncTypes = {
|
|
6628
|
-
functionType: Object.getPrototypeOf(function () { }).constructor,
|
|
6629
|
-
asyncFunctionType: Object.getPrototypeOf(async function () { }).constructor,
|
|
6630
|
-
generatorFunctionType: Object.getPrototypeOf(function* () { }).constructor,
|
|
6631
|
-
asyncGeneratorFunctionType: Object.getPrototypeOf(async function* () { }).constructor
|
|
6632
|
-
};
|
|
6633
|
-
function funcType(async = false, generator = false) {
|
|
6634
|
-
if (!async && !generator)
|
|
6635
|
-
return FuncTypes.functionType;
|
|
6636
|
-
if (async && !generator)
|
|
6637
|
-
return FuncTypes.asyncFunctionType;
|
|
6638
|
-
if (!async && generator)
|
|
6639
|
-
return FuncTypes.generatorFunctionType;
|
|
6640
|
-
return FuncTypes.asyncGeneratorFunctionType;
|
|
6641
|
-
}
|
|
6642
|
-
function createFunction(refs, async = false, generator = false, blockStatement = false, body) {
|
|
6643
|
-
if (body === undefined) {
|
|
6644
|
-
return undefined;
|
|
6645
|
-
}
|
|
6646
|
-
refs.patches.sort((l, r) => r.start - l.start);
|
|
6647
|
-
refs.patches.forEach(r => {
|
|
6648
|
-
body = body.substring(0, r.start) + r.newText + body.substring(r.end);
|
|
6649
|
-
});
|
|
6650
|
-
return new (funcType(async, generator))(...refs.args, blockStatement ?
|
|
6651
|
-
body.substring(1, body.length - 1).trim() :
|
|
6652
|
-
`return (\n${body}\n);`);
|
|
6653
|
-
}
|
|
6654
|
-
// Hide "import" from bundlers as they have a habit of replacing "import" with "require"
|
|
6655
|
-
async function obfuscatedImport(url) {
|
|
6656
|
-
return new FuncTypes.asyncFunctionType("url", "return import(url)")(url);
|
|
6657
|
-
}
|
|
6658
|
-
function encodeBacktick(str) {
|
|
6659
|
-
return str
|
|
6660
|
-
.split("`").join("\\`");
|
|
6661
|
-
}
|
|
6662
|
-
function createParsedOJS(ojs, offset, inlineMD) {
|
|
6663
|
-
return {
|
|
6664
|
-
ojs,
|
|
6665
|
-
offset,
|
|
6666
|
-
inlineMD
|
|
6667
|
-
};
|
|
6668
|
-
}
|
|
6669
|
-
function parseOmd(_) {
|
|
6670
|
-
const retVal = [];
|
|
6671
|
-
// Load Markdown ---
|
|
6672
|
-
const re = /(```(?:\s|\S)[\s\S]*?```)/g;
|
|
6673
|
-
let prevOffset = 0;
|
|
6674
|
-
let match = re.exec(_);
|
|
6675
|
-
while (match !== null) {
|
|
6676
|
-
if (match.index > prevOffset) {
|
|
6677
|
-
retVal.push(createParsedOJS(_.substring(prevOffset, match.index), prevOffset, true));
|
|
6678
|
-
}
|
|
6679
|
-
const outer = match[0];
|
|
6680
|
-
if (outer.indexOf("``` ") === 0 || outer.indexOf("```\n") === 0 || outer.indexOf("```\r\n") === 0) {
|
|
6681
|
-
const prefixLen = 3;
|
|
6682
|
-
const inner = outer.substring(prefixLen, outer.length - prefixLen);
|
|
6683
|
-
retVal.push(createParsedOJS(inner, match.index + prefixLen, false));
|
|
6684
|
-
}
|
|
6685
|
-
else {
|
|
6686
|
-
retVal.push(createParsedOJS(outer, match.index, true));
|
|
6687
|
-
}
|
|
6688
|
-
prevOffset = match.index + match[0].length;
|
|
6689
|
-
match = re.exec(_);
|
|
6690
|
-
}
|
|
6691
|
-
if (_.length > prevOffset) {
|
|
6692
|
-
retVal.push(createParsedOJS(_.substring(prevOffset, _.length), prevOffset, true));
|
|
6693
|
-
}
|
|
6694
|
-
return retVal;
|
|
6695
|
-
}
|
|
6696
|
-
function ojs2notebook(ojs) {
|
|
6697
|
-
const cells = parseModule(ojs);
|
|
6698
|
-
return {
|
|
6699
|
-
files: [],
|
|
6700
|
-
nodes: cells.map((cell, idx) => {
|
|
6701
|
-
return {
|
|
6702
|
-
id: idx,
|
|
6703
|
-
mode: "js",
|
|
6704
|
-
value: cell
|
|
6705
|
-
};
|
|
6706
|
-
})
|
|
6707
|
-
};
|
|
6708
|
-
}
|
|
6709
|
-
function omd2notebook(omd) {
|
|
6710
|
-
const cells = parseOmd(omd);
|
|
6711
|
-
return {
|
|
6712
|
-
files: [],
|
|
6713
|
-
nodes: cells.map((cell, idx) => {
|
|
6714
|
-
return {
|
|
6715
|
-
id: idx,
|
|
6716
|
-
mode: cell.inlineMD ? "md" : "js",
|
|
6717
|
-
value: cell.ojs
|
|
6718
|
-
};
|
|
6719
|
-
})
|
|
6720
|
-
};
|
|
6721
|
-
}
|
|
6722
|
-
function fetchEx(url) {
|
|
6723
|
-
return fetch(url)
|
|
6724
|
-
.then(response => {
|
|
6725
|
-
if (response.ok)
|
|
6726
|
-
return response;
|
|
6727
|
-
throw new Error("CORS?");
|
|
6728
|
-
}).catch(e => {
|
|
6729
|
-
const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
|
|
6730
|
-
url = "https://observable-cors.glitch.me/" + url;
|
|
6731
|
-
return fetch(url, {
|
|
6732
|
-
headers: {
|
|
6733
|
-
origin: matches[0],
|
|
6734
|
-
referer: url
|
|
6735
|
-
}
|
|
6736
|
-
});
|
|
6737
|
-
});
|
|
6738
|
-
}
|
|
6739
|
-
function download(impUrl) {
|
|
6740
|
-
const isShared = impUrl.indexOf("https://observablehq.com/d") === 0;
|
|
6741
|
-
return fetchEx(impUrl.replace(`https://observablehq.com/${isShared ? "d/" : ""}`, "https://api.observablehq.com/document/"))
|
|
6742
|
-
.then(r => r.json());
|
|
6627
|
+
const FuncTypes = {
|
|
6628
|
+
functionType: Object.getPrototypeOf(function () { }).constructor,
|
|
6629
|
+
asyncFunctionType: Object.getPrototypeOf(async function () { }).constructor,
|
|
6630
|
+
generatorFunctionType: Object.getPrototypeOf(function* () { }).constructor,
|
|
6631
|
+
asyncGeneratorFunctionType: Object.getPrototypeOf(async function* () { }).constructor
|
|
6632
|
+
};
|
|
6633
|
+
function funcType(async = false, generator = false) {
|
|
6634
|
+
if (!async && !generator)
|
|
6635
|
+
return FuncTypes.functionType;
|
|
6636
|
+
if (async && !generator)
|
|
6637
|
+
return FuncTypes.asyncFunctionType;
|
|
6638
|
+
if (!async && generator)
|
|
6639
|
+
return FuncTypes.generatorFunctionType;
|
|
6640
|
+
return FuncTypes.asyncGeneratorFunctionType;
|
|
6641
|
+
}
|
|
6642
|
+
function createFunction(refs, async = false, generator = false, blockStatement = false, body) {
|
|
6643
|
+
if (body === undefined) {
|
|
6644
|
+
return undefined;
|
|
6645
|
+
}
|
|
6646
|
+
refs.patches.sort((l, r) => r.start - l.start);
|
|
6647
|
+
refs.patches.forEach(r => {
|
|
6648
|
+
body = body.substring(0, r.start) + r.newText + body.substring(r.end);
|
|
6649
|
+
});
|
|
6650
|
+
return new (funcType(async, generator))(...refs.args, blockStatement ?
|
|
6651
|
+
body.substring(1, body.length - 1).trim() :
|
|
6652
|
+
`return (\n${body}\n);`);
|
|
6653
|
+
}
|
|
6654
|
+
// Hide "import" from bundlers as they have a habit of replacing "import" with "require"
|
|
6655
|
+
async function obfuscatedImport(url) {
|
|
6656
|
+
return new FuncTypes.asyncFunctionType("url", "return import(url)")(url);
|
|
6657
|
+
}
|
|
6658
|
+
function encodeBacktick(str) {
|
|
6659
|
+
return str
|
|
6660
|
+
.split("`").join("\\`");
|
|
6661
|
+
}
|
|
6662
|
+
function createParsedOJS(ojs, offset, inlineMD) {
|
|
6663
|
+
return {
|
|
6664
|
+
ojs,
|
|
6665
|
+
offset,
|
|
6666
|
+
inlineMD
|
|
6667
|
+
};
|
|
6668
|
+
}
|
|
6669
|
+
function parseOmd(_) {
|
|
6670
|
+
const retVal = [];
|
|
6671
|
+
// Load Markdown ---
|
|
6672
|
+
const re = /(```(?:\s|\S)[\s\S]*?```)/g;
|
|
6673
|
+
let prevOffset = 0;
|
|
6674
|
+
let match = re.exec(_);
|
|
6675
|
+
while (match !== null) {
|
|
6676
|
+
if (match.index > prevOffset) {
|
|
6677
|
+
retVal.push(createParsedOJS(_.substring(prevOffset, match.index), prevOffset, true));
|
|
6678
|
+
}
|
|
6679
|
+
const outer = match[0];
|
|
6680
|
+
if (outer.indexOf("``` ") === 0 || outer.indexOf("```\n") === 0 || outer.indexOf("```\r\n") === 0) {
|
|
6681
|
+
const prefixLen = 3;
|
|
6682
|
+
const inner = outer.substring(prefixLen, outer.length - prefixLen);
|
|
6683
|
+
retVal.push(createParsedOJS(inner, match.index + prefixLen, false));
|
|
6684
|
+
}
|
|
6685
|
+
else {
|
|
6686
|
+
retVal.push(createParsedOJS(outer, match.index, true));
|
|
6687
|
+
}
|
|
6688
|
+
prevOffset = match.index + match[0].length;
|
|
6689
|
+
match = re.exec(_);
|
|
6690
|
+
}
|
|
6691
|
+
if (_.length > prevOffset) {
|
|
6692
|
+
retVal.push(createParsedOJS(_.substring(prevOffset, _.length), prevOffset, true));
|
|
6693
|
+
}
|
|
6694
|
+
return retVal;
|
|
6695
|
+
}
|
|
6696
|
+
function ojs2notebook(ojs) {
|
|
6697
|
+
const cells = parseModule(ojs);
|
|
6698
|
+
return {
|
|
6699
|
+
files: [],
|
|
6700
|
+
nodes: cells.map((cell, idx) => {
|
|
6701
|
+
return {
|
|
6702
|
+
id: idx,
|
|
6703
|
+
mode: "js",
|
|
6704
|
+
value: cell
|
|
6705
|
+
};
|
|
6706
|
+
})
|
|
6707
|
+
};
|
|
6708
|
+
}
|
|
6709
|
+
function omd2notebook(omd) {
|
|
6710
|
+
const cells = parseOmd(omd);
|
|
6711
|
+
return {
|
|
6712
|
+
files: [],
|
|
6713
|
+
nodes: cells.map((cell, idx) => {
|
|
6714
|
+
return {
|
|
6715
|
+
id: idx,
|
|
6716
|
+
mode: cell.inlineMD ? "md" : "js",
|
|
6717
|
+
value: cell.ojs
|
|
6718
|
+
};
|
|
6719
|
+
})
|
|
6720
|
+
};
|
|
6721
|
+
}
|
|
6722
|
+
function fetchEx(url) {
|
|
6723
|
+
return fetch(url)
|
|
6724
|
+
.then(response => {
|
|
6725
|
+
if (response.ok)
|
|
6726
|
+
return response;
|
|
6727
|
+
throw new Error("CORS?");
|
|
6728
|
+
}).catch(e => {
|
|
6729
|
+
const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
|
|
6730
|
+
url = "https://observable-cors.glitch.me/" + url;
|
|
6731
|
+
return fetch(url, {
|
|
6732
|
+
headers: {
|
|
6733
|
+
origin: matches[0],
|
|
6734
|
+
referer: url
|
|
6735
|
+
}
|
|
6736
|
+
});
|
|
6737
|
+
});
|
|
6738
|
+
}
|
|
6739
|
+
function download(impUrl) {
|
|
6740
|
+
const isShared = impUrl.indexOf("https://observablehq.com/d") === 0;
|
|
6741
|
+
return fetchEx(impUrl.replace(`https://observablehq.com/${isShared ? "d/" : ""}`, "https://api.observablehq.com/document/"))
|
|
6742
|
+
.then(r => r.json());
|
|
6743
6743
|
}
|
|
6744
6744
|
|
|
6745
|
-
function calcRefs(cellAst, cellStr) {
|
|
6746
|
-
if (cellAst.references === undefined)
|
|
6747
|
-
return { inputs: [], args: [], patches: [] };
|
|
6748
|
-
const dedup = {};
|
|
6749
|
-
cellAst.references.forEach(r => dedup[cellStr.substring(r.start, r.end)] = true);
|
|
6750
|
-
const retVal = {
|
|
6751
|
-
inputs: Object.keys(dedup),
|
|
6752
|
-
args: Object.keys(dedup).map(r => r.split(" ").join("_")),
|
|
6753
|
-
patches: []
|
|
6754
|
-
};
|
|
6755
|
-
const pushPatch = (node, newText) => retVal.patches.push({ start: node.start - cellAst.body.start, end: node.end - cellAst.body.start, newText });
|
|
6756
|
-
if (cellAst.body) {
|
|
6757
|
-
ancestor(cellAst.body, {
|
|
6758
|
-
Identifier(node) {
|
|
6759
|
-
const value = cellStr.substring(node.start, node.end);
|
|
6760
|
-
if (dedup[value]) ;
|
|
6761
|
-
},
|
|
6762
|
-
MutableExpression(node) {
|
|
6763
|
-
const value = cellStr.substring(node.start, node.end);
|
|
6764
|
-
const newText = value.split(" ").join("_") + ".value";
|
|
6765
|
-
pushPatch(node, newText);
|
|
6766
|
-
},
|
|
6767
|
-
ViewExpression(node) {
|
|
6768
|
-
const value = cellStr.substring(node.start, node.end);
|
|
6769
|
-
const newText = value.split(" ").join("_");
|
|
6770
|
-
pushPatch(node, newText);
|
|
6771
|
-
},
|
|
6772
|
-
ThisExpression(node, ancestors) {
|
|
6773
|
-
const value = cellStr.substring(node.start, node.end);
|
|
6774
|
-
if (value === "this" && !ancestors.find(n => n.type === "FunctionExpression")) {
|
|
6775
|
-
pushPatch(node, "((this === globalThis || this === globalThis.window)? undefined : this?.valueOf())");
|
|
6776
|
-
}
|
|
6777
|
-
}
|
|
6778
|
-
}, walk);
|
|
6779
|
-
}
|
|
6780
|
-
return retVal;
|
|
6781
|
-
}
|
|
6782
|
-
function parseImportExpression(cellAst) {
|
|
6783
|
-
var _a, _b, _c, _d;
|
|
6784
|
-
return {
|
|
6785
|
-
type: "import",
|
|
6786
|
-
src: cellAst.body.source.value,
|
|
6787
|
-
specifiers: (_b = (_a = cellAst.body.specifiers) === null || _a === void 0 ? void 0 : _a.map(spec => {
|
|
6788
|
-
var _a;
|
|
6789
|
-
return {
|
|
6790
|
-
view: spec.view,
|
|
6791
|
-
name: spec.imported.name,
|
|
6792
|
-
alias: (((_a = spec.local) === null || _a === void 0 ? void 0 : _a.name) && spec.imported.name !== spec.local.name) ? spec.local.name : spec.imported.name
|
|
6793
|
-
};
|
|
6794
|
-
})) !== null && _b !== void 0 ? _b : [],
|
|
6795
|
-
injections: (_d = (_c = cellAst.body.injections) === null || _c === void 0 ? void 0 : _c.map(inj => {
|
|
6796
|
-
var _a, _b;
|
|
6797
|
-
return {
|
|
6798
|
-
name: inj.imported.name,
|
|
6799
|
-
alias: (_b = (_a = inj.local) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : inj.imported.name
|
|
6800
|
-
};
|
|
6801
|
-
})) !== null && _d !== void 0 ? _d : [],
|
|
6802
|
-
};
|
|
6803
|
-
}
|
|
6804
|
-
function parseViewExpression(cellStr, cellAst, refs, bodyStr) {
|
|
6805
|
-
var _a, _b;
|
|
6806
|
-
const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
|
|
6807
|
-
return {
|
|
6808
|
-
type: "viewof",
|
|
6809
|
-
variable: {
|
|
6810
|
-
type: "variable",
|
|
6811
|
-
id,
|
|
6812
|
-
inputs: refs.inputs,
|
|
6813
|
-
func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
|
|
6814
|
-
},
|
|
6815
|
-
variableValue: {
|
|
6816
|
-
type: "variable",
|
|
6817
|
-
id: (_b = (_a = cellAst === null || cellAst === void 0 ? void 0 : cellAst.id) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.name,
|
|
6818
|
-
inputs: ["Generators", id],
|
|
6819
|
-
func: (G, _) => G.input(_)
|
|
6820
|
-
}
|
|
6821
|
-
};
|
|
6822
|
-
}
|
|
6823
|
-
function parseMutableExpression(cellStr, cellAst, refs, bodyStr) {
|
|
6824
|
-
var _a, _b;
|
|
6825
|
-
const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
|
|
6826
|
-
const initialValueId = (_b = (_a = cellAst === null || cellAst === void 0 ? void 0 : cellAst.id) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.name;
|
|
6827
|
-
const initialId = `initial ${initialValueId}`;
|
|
6828
|
-
return {
|
|
6829
|
-
type: "mutable",
|
|
6830
|
-
initial: {
|
|
6831
|
-
type: "variable",
|
|
6832
|
-
id: initialId,
|
|
6833
|
-
inputs: refs.inputs,
|
|
6834
|
-
func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
|
|
6835
|
-
},
|
|
6836
|
-
variable: {
|
|
6837
|
-
type: "variable",
|
|
6838
|
-
id,
|
|
6839
|
-
inputs: ["Mutable", initialId],
|
|
6840
|
-
func: (M, _) => new M(_)
|
|
6841
|
-
},
|
|
6842
|
-
variableValue: {
|
|
6843
|
-
type: "variable",
|
|
6844
|
-
id: initialValueId,
|
|
6845
|
-
inputs: [id],
|
|
6846
|
-
func: _ => _.generator
|
|
6847
|
-
}
|
|
6848
|
-
};
|
|
6849
|
-
}
|
|
6850
|
-
function parseVariableExpression(cellStr, cellAst, refs, bodyStr) {
|
|
6851
|
-
var _a, _b;
|
|
6852
|
-
return {
|
|
6853
|
-
type: "variable",
|
|
6854
|
-
id: cellAst.id && cellStr.substring((_a = cellAst.id) === null || _a === void 0 ? void 0 : _a.start, (_b = cellAst.id) === null || _b === void 0 ? void 0 : _b.end),
|
|
6855
|
-
inputs: refs.inputs,
|
|
6856
|
-
func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
|
|
6857
|
-
};
|
|
6858
|
-
}
|
|
6859
|
-
function parseCell(cellStr) {
|
|
6860
|
-
var _a, _b;
|
|
6861
|
-
const cellAst = parseCell$1(cellStr);
|
|
6862
|
-
if (((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) == "ImportDeclaration") {
|
|
6863
|
-
return parseImportExpression(cellAst);
|
|
6864
|
-
}
|
|
6865
|
-
const refs = calcRefs(cellAst, cellStr);
|
|
6866
|
-
const bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
|
|
6867
|
-
switch ((_b = cellAst.id) === null || _b === void 0 ? void 0 : _b.type) {
|
|
6868
|
-
case "ViewExpression":
|
|
6869
|
-
return parseViewExpression(cellStr, cellAst, refs, bodyStr);
|
|
6870
|
-
case "MutableExpression":
|
|
6871
|
-
return parseMutableExpression(cellStr, cellAst, refs, bodyStr);
|
|
6872
|
-
default:
|
|
6873
|
-
return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
|
|
6874
|
-
}
|
|
6745
|
+
function calcRefs(cellAst, cellStr) {
|
|
6746
|
+
if (cellAst.references === undefined)
|
|
6747
|
+
return { inputs: [], args: [], patches: [] };
|
|
6748
|
+
const dedup = {};
|
|
6749
|
+
cellAst.references.forEach(r => dedup[cellStr.substring(r.start, r.end)] = true);
|
|
6750
|
+
const retVal = {
|
|
6751
|
+
inputs: Object.keys(dedup),
|
|
6752
|
+
args: Object.keys(dedup).map(r => r.split(" ").join("_")),
|
|
6753
|
+
patches: []
|
|
6754
|
+
};
|
|
6755
|
+
const pushPatch = (node, newText) => retVal.patches.push({ start: node.start - cellAst.body.start, end: node.end - cellAst.body.start, newText });
|
|
6756
|
+
if (cellAst.body) {
|
|
6757
|
+
ancestor(cellAst.body, {
|
|
6758
|
+
Identifier(node) {
|
|
6759
|
+
const value = cellStr.substring(node.start, node.end);
|
|
6760
|
+
if (dedup[value]) ;
|
|
6761
|
+
},
|
|
6762
|
+
MutableExpression(node) {
|
|
6763
|
+
const value = cellStr.substring(node.start, node.end);
|
|
6764
|
+
const newText = value.split(" ").join("_") + ".value";
|
|
6765
|
+
pushPatch(node, newText);
|
|
6766
|
+
},
|
|
6767
|
+
ViewExpression(node) {
|
|
6768
|
+
const value = cellStr.substring(node.start, node.end);
|
|
6769
|
+
const newText = value.split(" ").join("_");
|
|
6770
|
+
pushPatch(node, newText);
|
|
6771
|
+
},
|
|
6772
|
+
ThisExpression(node, ancestors) {
|
|
6773
|
+
const value = cellStr.substring(node.start, node.end);
|
|
6774
|
+
if (value === "this" && !ancestors.find(n => n.type === "FunctionExpression")) {
|
|
6775
|
+
pushPatch(node, "((this === globalThis || this === globalThis.window)? undefined : this?.valueOf())");
|
|
6776
|
+
}
|
|
6777
|
+
}
|
|
6778
|
+
}, walk);
|
|
6779
|
+
}
|
|
6780
|
+
return retVal;
|
|
6781
|
+
}
|
|
6782
|
+
function parseImportExpression(cellAst) {
|
|
6783
|
+
var _a, _b, _c, _d;
|
|
6784
|
+
return {
|
|
6785
|
+
type: "import",
|
|
6786
|
+
src: cellAst.body.source.value,
|
|
6787
|
+
specifiers: (_b = (_a = cellAst.body.specifiers) === null || _a === void 0 ? void 0 : _a.map(spec => {
|
|
6788
|
+
var _a;
|
|
6789
|
+
return {
|
|
6790
|
+
view: spec.view,
|
|
6791
|
+
name: spec.imported.name,
|
|
6792
|
+
alias: (((_a = spec.local) === null || _a === void 0 ? void 0 : _a.name) && spec.imported.name !== spec.local.name) ? spec.local.name : spec.imported.name
|
|
6793
|
+
};
|
|
6794
|
+
})) !== null && _b !== void 0 ? _b : [],
|
|
6795
|
+
injections: (_d = (_c = cellAst.body.injections) === null || _c === void 0 ? void 0 : _c.map(inj => {
|
|
6796
|
+
var _a, _b;
|
|
6797
|
+
return {
|
|
6798
|
+
name: inj.imported.name,
|
|
6799
|
+
alias: (_b = (_a = inj.local) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : inj.imported.name
|
|
6800
|
+
};
|
|
6801
|
+
})) !== null && _d !== void 0 ? _d : [],
|
|
6802
|
+
};
|
|
6803
|
+
}
|
|
6804
|
+
function parseViewExpression(cellStr, cellAst, refs, bodyStr) {
|
|
6805
|
+
var _a, _b;
|
|
6806
|
+
const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
|
|
6807
|
+
return {
|
|
6808
|
+
type: "viewof",
|
|
6809
|
+
variable: {
|
|
6810
|
+
type: "variable",
|
|
6811
|
+
id,
|
|
6812
|
+
inputs: refs.inputs,
|
|
6813
|
+
func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
|
|
6814
|
+
},
|
|
6815
|
+
variableValue: {
|
|
6816
|
+
type: "variable",
|
|
6817
|
+
id: (_b = (_a = cellAst === null || cellAst === void 0 ? void 0 : cellAst.id) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.name,
|
|
6818
|
+
inputs: ["Generators", id],
|
|
6819
|
+
func: (G, _) => G.input(_)
|
|
6820
|
+
}
|
|
6821
|
+
};
|
|
6822
|
+
}
|
|
6823
|
+
function parseMutableExpression(cellStr, cellAst, refs, bodyStr) {
|
|
6824
|
+
var _a, _b;
|
|
6825
|
+
const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
|
|
6826
|
+
const initialValueId = (_b = (_a = cellAst === null || cellAst === void 0 ? void 0 : cellAst.id) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.name;
|
|
6827
|
+
const initialId = `initial ${initialValueId}`;
|
|
6828
|
+
return {
|
|
6829
|
+
type: "mutable",
|
|
6830
|
+
initial: {
|
|
6831
|
+
type: "variable",
|
|
6832
|
+
id: initialId,
|
|
6833
|
+
inputs: refs.inputs,
|
|
6834
|
+
func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
|
|
6835
|
+
},
|
|
6836
|
+
variable: {
|
|
6837
|
+
type: "variable",
|
|
6838
|
+
id,
|
|
6839
|
+
inputs: ["Mutable", initialId],
|
|
6840
|
+
func: (M, _) => new M(_)
|
|
6841
|
+
},
|
|
6842
|
+
variableValue: {
|
|
6843
|
+
type: "variable",
|
|
6844
|
+
id: initialValueId,
|
|
6845
|
+
inputs: [id],
|
|
6846
|
+
func: _ => _.generator
|
|
6847
|
+
}
|
|
6848
|
+
};
|
|
6849
|
+
}
|
|
6850
|
+
function parseVariableExpression(cellStr, cellAst, refs, bodyStr) {
|
|
6851
|
+
var _a, _b;
|
|
6852
|
+
return {
|
|
6853
|
+
type: "variable",
|
|
6854
|
+
id: cellAst.id && cellStr.substring((_a = cellAst.id) === null || _a === void 0 ? void 0 : _a.start, (_b = cellAst.id) === null || _b === void 0 ? void 0 : _b.end),
|
|
6855
|
+
inputs: refs.inputs,
|
|
6856
|
+
func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
|
|
6857
|
+
};
|
|
6858
|
+
}
|
|
6859
|
+
function parseCell(cellStr) {
|
|
6860
|
+
var _a, _b;
|
|
6861
|
+
const cellAst = parseCell$1(cellStr);
|
|
6862
|
+
if (((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) == "ImportDeclaration") {
|
|
6863
|
+
return parseImportExpression(cellAst);
|
|
6864
|
+
}
|
|
6865
|
+
const refs = calcRefs(cellAst, cellStr);
|
|
6866
|
+
const bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
|
|
6867
|
+
switch ((_b = cellAst.id) === null || _b === void 0 ? void 0 : _b.type) {
|
|
6868
|
+
case "ViewExpression":
|
|
6869
|
+
return parseViewExpression(cellStr, cellAst, refs, bodyStr);
|
|
6870
|
+
case "MutableExpression":
|
|
6871
|
+
return parseMutableExpression(cellStr, cellAst, refs, bodyStr);
|
|
6872
|
+
default:
|
|
6873
|
+
return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
|
|
6874
|
+
}
|
|
6875
6875
|
}
|
|
6876
6876
|
|
|
6877
|
-
class Writer {
|
|
6878
|
-
constructor() {
|
|
6879
|
-
this._files = [];
|
|
6880
|
-
this._imports = [];
|
|
6881
|
-
this._functions = [];
|
|
6882
|
-
this._defines = [];
|
|
6883
|
-
this._defineUid = 0;
|
|
6884
|
-
this._functionUid = 0;
|
|
6885
|
-
}
|
|
6886
|
-
toString() {
|
|
6887
|
-
return `\
|
|
6888
|
-
${this._imports.join("\n")}
|
|
6889
|
-
|
|
6890
|
-
${this._functions.join("\n").split("\n) {").join("){")}
|
|
6891
|
-
|
|
6892
|
-
export default function define(runtime, observer) {
|
|
6893
|
-
const main = runtime.module();
|
|
6894
|
-
|
|
6895
|
-
function toString() { return this.url; }
|
|
6896
|
-
const fileAttachments = new Map([
|
|
6897
|
-
${this._files.map(f => `["${f.name}", { url: new URL("${f.url}"), mimeType: ${JSON.stringify(f.mime_type)}, toString }]`).join(",\n ")}
|
|
6898
|
-
]);
|
|
6899
|
-
main.builtin("FileAttachment", runtime.fileAttachments(name => fileAttachments.get(name)));
|
|
6900
|
-
|
|
6901
|
-
${this._defines.join("\n ")}
|
|
6902
|
-
|
|
6903
|
-
return main;
|
|
6904
|
-
}\n`;
|
|
6905
|
-
}
|
|
6906
|
-
files(files) {
|
|
6907
|
-
this._files = [...this._files, ...files];
|
|
6908
|
-
}
|
|
6909
|
-
import(url) {
|
|
6910
|
-
this._imports.push(`import define${++this._defineUid} from "${url}"; `);
|
|
6911
|
-
}
|
|
6912
|
-
importDefine(imp) {
|
|
6913
|
-
const injections = imp.injections.map(inj => {
|
|
6914
|
-
return inj.name === inj.alias ?
|
|
6915
|
-
`"${inj.name}"` :
|
|
6916
|
-
`{name: "${inj.name}", alias: "${inj.alias}"}`;
|
|
6917
|
-
});
|
|
6918
|
-
const derive = imp.injections.length ? `.derive([${injections.join(", ")}], main)` : "";
|
|
6919
|
-
this._defines.push(`const child${this._defineUid} = runtime.module(define${this._defineUid})${derive};`);
|
|
6920
|
-
imp.specifiers.forEach(s => {
|
|
6921
|
-
this._defines.push(`main.import("${s.name}"${s.alias && s.alias !== s.name ? `, "${s.alias}"` : ""}, child${this._defineUid}); `);
|
|
6922
|
-
});
|
|
6923
|
-
}
|
|
6924
|
-
function(variable) {
|
|
6925
|
-
var _a, _b, _c;
|
|
6926
|
-
let id = (_a = variable.id) !== null && _a !== void 0 ? _a : `${++this._functionUid}`;
|
|
6927
|
-
const idParts = id.split(" ");
|
|
6928
|
-
id = `_${idParts[idParts.length - 1]}`;
|
|
6929
|
-
this._functions.push(`${(_c = (_b = variable.func) === null || _b === void 0 ? void 0 : _b.toString()) === null || _c === void 0 ? void 0 : _c.replace("anonymous", `${id}`)}`);
|
|
6930
|
-
return id;
|
|
6931
|
-
}
|
|
6932
|
-
define(variable, observable = true, inlineFunc = false, funcId) {
|
|
6933
|
-
var _a;
|
|
6934
|
-
funcId = funcId !== null && funcId !== void 0 ? funcId : variable.id;
|
|
6935
|
-
const observe = observable ? `.variable(observer(${variable.id ? JSON.stringify(variable.id) : ""}))` : "";
|
|
6936
|
-
const id = variable.id ? `${JSON.stringify(variable.id)}, ` : "";
|
|
6937
|
-
const inputs = variable.inputs.length ? `[${variable.inputs.map(i => JSON.stringify(i)).join(", ")}], ` : "";
|
|
6938
|
-
const func = inlineFunc ?
|
|
6939
|
-
(_a = variable.func) === null || _a === void 0 ? void 0 : _a.toString() :
|
|
6940
|
-
funcId;
|
|
6941
|
-
this._defines.push(`main${observe}.define(${id}${inputs}${func});`);
|
|
6942
|
-
}
|
|
6943
|
-
error(msg) {
|
|
6944
|
-
}
|
|
6877
|
+
class Writer {
|
|
6878
|
+
constructor() {
|
|
6879
|
+
this._files = [];
|
|
6880
|
+
this._imports = [];
|
|
6881
|
+
this._functions = [];
|
|
6882
|
+
this._defines = [];
|
|
6883
|
+
this._defineUid = 0;
|
|
6884
|
+
this._functionUid = 0;
|
|
6885
|
+
}
|
|
6886
|
+
toString() {
|
|
6887
|
+
return `\
|
|
6888
|
+
${this._imports.join("\n")}
|
|
6889
|
+
|
|
6890
|
+
${this._functions.join("\n").split("\n) {").join("){")}
|
|
6891
|
+
|
|
6892
|
+
export default function define(runtime, observer) {
|
|
6893
|
+
const main = runtime.module();
|
|
6894
|
+
|
|
6895
|
+
function toString() { return this.url; }
|
|
6896
|
+
const fileAttachments = new Map([
|
|
6897
|
+
${this._files.map(f => `["${f.name}", { url: new URL("${f.url}"), mimeType: ${JSON.stringify(f.mime_type)}, toString }]`).join(",\n ")}
|
|
6898
|
+
]);
|
|
6899
|
+
main.builtin("FileAttachment", runtime.fileAttachments(name => fileAttachments.get(name)));
|
|
6900
|
+
|
|
6901
|
+
${this._defines.join("\n ")}
|
|
6902
|
+
|
|
6903
|
+
return main;
|
|
6904
|
+
}\n`;
|
|
6905
|
+
}
|
|
6906
|
+
files(files) {
|
|
6907
|
+
this._files = [...this._files, ...files];
|
|
6908
|
+
}
|
|
6909
|
+
import(url) {
|
|
6910
|
+
this._imports.push(`import define${++this._defineUid} from "${url}"; `);
|
|
6911
|
+
}
|
|
6912
|
+
importDefine(imp) {
|
|
6913
|
+
const injections = imp.injections.map(inj => {
|
|
6914
|
+
return inj.name === inj.alias ?
|
|
6915
|
+
`"${inj.name}"` :
|
|
6916
|
+
`{name: "${inj.name}", alias: "${inj.alias}"}`;
|
|
6917
|
+
});
|
|
6918
|
+
const derive = imp.injections.length ? `.derive([${injections.join(", ")}], main)` : "";
|
|
6919
|
+
this._defines.push(`const child${this._defineUid} = runtime.module(define${this._defineUid})${derive};`);
|
|
6920
|
+
imp.specifiers.forEach(s => {
|
|
6921
|
+
this._defines.push(`main.import("${s.name}"${s.alias && s.alias !== s.name ? `, "${s.alias}"` : ""}, child${this._defineUid}); `);
|
|
6922
|
+
});
|
|
6923
|
+
}
|
|
6924
|
+
function(variable) {
|
|
6925
|
+
var _a, _b, _c;
|
|
6926
|
+
let id = (_a = variable.id) !== null && _a !== void 0 ? _a : `${++this._functionUid}`;
|
|
6927
|
+
const idParts = id.split(" ");
|
|
6928
|
+
id = `_${idParts[idParts.length - 1]}`;
|
|
6929
|
+
this._functions.push(`${(_c = (_b = variable.func) === null || _b === void 0 ? void 0 : _b.toString()) === null || _c === void 0 ? void 0 : _c.replace("anonymous", `${id}`)}`);
|
|
6930
|
+
return id;
|
|
6931
|
+
}
|
|
6932
|
+
define(variable, observable = true, inlineFunc = false, funcId) {
|
|
6933
|
+
var _a;
|
|
6934
|
+
funcId = funcId !== null && funcId !== void 0 ? funcId : variable.id;
|
|
6935
|
+
const observe = observable ? `.variable(observer(${variable.id ? JSON.stringify(variable.id) : ""}))` : "";
|
|
6936
|
+
const id = variable.id ? `${JSON.stringify(variable.id)}, ` : "";
|
|
6937
|
+
const inputs = variable.inputs.length ? `[${variable.inputs.map(i => JSON.stringify(i)).join(", ")}], ` : "";
|
|
6938
|
+
const func = inlineFunc ?
|
|
6939
|
+
(_a = variable.func) === null || _a === void 0 ? void 0 : _a.toString() :
|
|
6940
|
+
funcId;
|
|
6941
|
+
this._defines.push(`main${observe}.define(${id}${inputs}${func});`);
|
|
6942
|
+
}
|
|
6943
|
+
error(msg) {
|
|
6944
|
+
}
|
|
6945
6945
|
}
|
|
6946
6946
|
|
|
6947
|
-
async function importFile(relativePath, baseUrl) {
|
|
6948
|
-
const path = join(baseUrl, relativePath);
|
|
6949
|
-
const content = await fetchEx(path).then(r => r.text());
|
|
6950
|
-
let notebook;
|
|
6951
|
-
if (endsWith(relativePath, ".ojsnb")) {
|
|
6952
|
-
notebook = JSON.parse(content);
|
|
6953
|
-
}
|
|
6954
|
-
else if (endsWith(relativePath, ".ojs")) {
|
|
6955
|
-
notebook = ojs2notebook(content);
|
|
6956
|
-
}
|
|
6957
|
-
else if (endsWith(relativePath, ".omd")) {
|
|
6958
|
-
notebook = omd2notebook(content);
|
|
6959
|
-
}
|
|
6960
|
-
const retVal = compile(notebook, baseUrl);
|
|
6961
|
-
retVal.dispose = () => { };
|
|
6962
|
-
retVal.write = (w) => {
|
|
6963
|
-
w.import(path);
|
|
6964
|
-
};
|
|
6965
|
-
return retVal;
|
|
6966
|
-
}
|
|
6967
|
-
// @ts-ignore - use precompiled notebook from observable
|
|
6968
|
-
async function importCompiledNotebook(partial) {
|
|
6969
|
-
const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
|
|
6970
|
-
let impMod = {
|
|
6971
|
-
default: function (runtime, inspector) {
|
|
6972
|
-
return undefined;
|
|
6973
|
-
}
|
|
6974
|
-
};
|
|
6975
|
-
try {
|
|
6976
|
-
impMod = await obfuscatedImport(url);
|
|
6977
|
-
}
|
|
6978
|
-
catch (e) {
|
|
6979
|
-
}
|
|
6980
|
-
const retVal = impMod.default;
|
|
6981
|
-
retVal.dispose = () => { };
|
|
6982
|
-
retVal.write = (w) => {
|
|
6983
|
-
w.import(url);
|
|
6984
|
-
};
|
|
6985
|
-
return retVal;
|
|
6986
|
-
}
|
|
6987
|
-
function createVariable(inspect, name, inputs, definition, inline = false) {
|
|
6988
|
-
let i;
|
|
6989
|
-
let v;
|
|
6990
|
-
const retVal = (module, inspector) => {
|
|
6991
|
-
i = inspect ? inspector(name) : undefined;
|
|
6992
|
-
v = module.variable(i);
|
|
6993
|
-
if (arguments.length > 1) {
|
|
6994
|
-
try {
|
|
6995
|
-
v.define(name, inputs, definition);
|
|
6996
|
-
}
|
|
6997
|
-
catch (e) {
|
|
6998
|
-
console.error(e === null || e === void 0 ? void 0 : e.message);
|
|
6999
|
-
}
|
|
7000
|
-
}
|
|
7001
|
-
return v;
|
|
7002
|
-
};
|
|
7003
|
-
retVal.dispose = () => {
|
|
7004
|
-
var _a;
|
|
7005
|
-
try {
|
|
7006
|
-
(_a = i === null || i === void 0 ? void 0 : i._node) === null || _a === void 0 ? void 0 : _a.remove();
|
|
7007
|
-
}
|
|
7008
|
-
catch (e) {
|
|
7009
|
-
}
|
|
7010
|
-
i = undefined;
|
|
7011
|
-
try {
|
|
7012
|
-
v === null || v === void 0 ? void 0 : v.delete();
|
|
7013
|
-
}
|
|
7014
|
-
catch (e) {
|
|
7015
|
-
}
|
|
7016
|
-
v = undefined;
|
|
7017
|
-
};
|
|
7018
|
-
retVal.write = (w) => {
|
|
7019
|
-
if (inline) {
|
|
7020
|
-
w.define({ id: name, inputs, func: definition }, inspect, true);
|
|
7021
|
-
}
|
|
7022
|
-
else {
|
|
7023
|
-
const id = w.function({ id: name, func: definition });
|
|
7024
|
-
w.define({ id: name, inputs, func: definition }, inspect, false, id);
|
|
7025
|
-
}
|
|
7026
|
-
};
|
|
7027
|
-
return retVal;
|
|
7028
|
-
}
|
|
7029
|
-
function createImportVariable(name, alias) {
|
|
7030
|
-
let v;
|
|
7031
|
-
const retVal = (main, otherModule) => {
|
|
7032
|
-
v = main.variable();
|
|
7033
|
-
v.import(name, alias, otherModule);
|
|
7034
|
-
};
|
|
7035
|
-
retVal.dispose = () => {
|
|
7036
|
-
v === null || v === void 0 ? void 0 : v.delete();
|
|
7037
|
-
};
|
|
7038
|
-
return retVal;
|
|
7039
|
-
}
|
|
7040
|
-
async function createModule(parsed, text, baseUrl) {
|
|
7041
|
-
const otherModule = [".", "/"].indexOf(parsed.src[0]) === 0 ?
|
|
7042
|
-
await importFile(parsed.src, baseUrl) :
|
|
7043
|
-
await importCompiledNotebook(parsed.src);
|
|
7044
|
-
const importVariables = [];
|
|
7045
|
-
const variables = [];
|
|
7046
|
-
parsed.specifiers.forEach(spec => {
|
|
7047
|
-
const viewof = spec.view ? "viewof " : "";
|
|
7048
|
-
importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
|
|
7049
|
-
if (spec.view) {
|
|
7050
|
-
importVariables.push(createImportVariable(spec.name, spec.alias));
|
|
7051
|
-
}
|
|
7052
|
-
});
|
|
7053
|
-
variables.push(createVariable(true, undefined, ["md"], md => {
|
|
7054
|
-
return md `\`\`\`JavaScript
|
|
7055
|
-
${text}
|
|
7056
|
-
\`\`\``;
|
|
7057
|
-
}));
|
|
7058
|
-
const retVal = (runtime, main, inspector) => {
|
|
7059
|
-
let mod = runtime.module(otherModule);
|
|
7060
|
-
if (parsed.injections.length) {
|
|
7061
|
-
mod = mod.derive(parsed.injections, main);
|
|
7062
|
-
}
|
|
7063
|
-
variables.forEach(v => v(main, inspector));
|
|
7064
|
-
importVariables.forEach(v => v(main, mod));
|
|
7065
|
-
return mod;
|
|
7066
|
-
};
|
|
7067
|
-
retVal.importVariables = importVariables;
|
|
7068
|
-
retVal.variables = variables;
|
|
7069
|
-
retVal.dispose = () => {
|
|
7070
|
-
importVariables.forEach(v => v.dispose());
|
|
7071
|
-
variables.forEach(v => v.dispose());
|
|
7072
|
-
otherModule.dispose();
|
|
7073
|
-
};
|
|
7074
|
-
retVal.write = (w) => {
|
|
7075
|
-
otherModule.write(w);
|
|
7076
|
-
w.importDefine(parsed);
|
|
7077
|
-
};
|
|
7078
|
-
return retVal;
|
|
7079
|
-
}
|
|
7080
|
-
async function createCell(node, baseUrl) {
|
|
7081
|
-
const modules = [];
|
|
7082
|
-
const variables = [];
|
|
7083
|
-
try {
|
|
7084
|
-
const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
|
|
7085
|
-
const parsed = parseCell(text);
|
|
7086
|
-
switch (parsed.type) {
|
|
7087
|
-
case "import":
|
|
7088
|
-
modules.push(await createModule(parsed, text, baseUrl));
|
|
7089
|
-
break;
|
|
7090
|
-
case "viewof":
|
|
7091
|
-
variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7092
|
-
variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7093
|
-
break;
|
|
7094
|
-
case "mutable":
|
|
7095
|
-
variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
|
|
7096
|
-
variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7097
|
-
variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7098
|
-
break;
|
|
7099
|
-
case "variable":
|
|
7100
|
-
variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
|
|
7101
|
-
break;
|
|
7102
|
-
}
|
|
7103
|
-
}
|
|
7104
|
-
catch (e) {
|
|
7105
|
-
}
|
|
7106
|
-
const retVal = (runtime, main, inspector) => {
|
|
7107
|
-
modules.forEach(imp => imp(runtime, main, inspector));
|
|
7108
|
-
variables.forEach(v => v(main, inspector));
|
|
7109
|
-
};
|
|
7110
|
-
retVal.modules = modules;
|
|
7111
|
-
retVal.variables = variables;
|
|
7112
|
-
retVal.dispose = () => {
|
|
7113
|
-
variables.forEach(v => v.dispose());
|
|
7114
|
-
modules.forEach(mod => mod.dispose());
|
|
7115
|
-
};
|
|
7116
|
-
retVal.write = (w) => {
|
|
7117
|
-
modules.forEach(imp => imp.write(w));
|
|
7118
|
-
variables.forEach(v => v.write(w));
|
|
7119
|
-
};
|
|
7120
|
-
return retVal;
|
|
7121
|
-
}
|
|
7122
|
-
function createFile(file) {
|
|
7123
|
-
function toString() { return globalThis.url; }
|
|
7124
|
-
return [file.name, { url: new URL(file.url), mimeType: file.mime_type, toString }];
|
|
7125
|
-
}
|
|
7126
|
-
async function compile(notebook, baseUrl = ".") {
|
|
7127
|
-
const files = notebook.files.map(f => createFile(f));
|
|
7128
|
-
const fileAttachments = new Map(files);
|
|
7129
|
-
let cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
|
|
7130
|
-
const retVal = (runtime, inspector) => {
|
|
7131
|
-
const main = runtime.module();
|
|
7132
|
-
main.builtin("FileAttachment", runtime.fileAttachments(name => {
|
|
7133
|
-
var _a;
|
|
7134
|
-
return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(name), mimeType: null };
|
|
7135
|
-
}));
|
|
7136
|
-
cells.forEach(cell => {
|
|
7137
|
-
cell(runtime, main, inspector);
|
|
7138
|
-
});
|
|
7139
|
-
return main;
|
|
7140
|
-
};
|
|
7141
|
-
retVal.fileAttachments = fileAttachments;
|
|
7142
|
-
retVal.cells = cells;
|
|
7143
|
-
retVal.appendCell = async (n, baseUrl = ".") => {
|
|
7144
|
-
const cell = await createCell(n, baseUrl);
|
|
7145
|
-
cells.push(cell);
|
|
7146
|
-
return cell;
|
|
7147
|
-
};
|
|
7148
|
-
retVal.disposeCell = async (cell) => {
|
|
7149
|
-
cells = cells.filter(c => c !== cell);
|
|
7150
|
-
cell.dispose();
|
|
7151
|
-
};
|
|
7152
|
-
retVal.dispose = () => {
|
|
7153
|
-
cells.forEach(cell => cell.dispose());
|
|
7154
|
-
cells = [];
|
|
7155
|
-
};
|
|
7156
|
-
retVal.write = (w) => {
|
|
7157
|
-
w.files(notebook.files);
|
|
7158
|
-
cells.forEach(cell => cell.write(w));
|
|
7159
|
-
};
|
|
7160
|
-
retVal.toString = (w = new Writer()) => {
|
|
7161
|
-
retVal.write(w);
|
|
7162
|
-
return w.toString().trim();
|
|
7163
|
-
};
|
|
7164
|
-
return retVal;
|
|
6947
|
+
async function importFile(relativePath, baseUrl) {
|
|
6948
|
+
const path = join(baseUrl, relativePath);
|
|
6949
|
+
const content = await fetchEx(path).then(r => r.text());
|
|
6950
|
+
let notebook;
|
|
6951
|
+
if (endsWith(relativePath, ".ojsnb")) {
|
|
6952
|
+
notebook = JSON.parse(content);
|
|
6953
|
+
}
|
|
6954
|
+
else if (endsWith(relativePath, ".ojs")) {
|
|
6955
|
+
notebook = ojs2notebook(content);
|
|
6956
|
+
}
|
|
6957
|
+
else if (endsWith(relativePath, ".omd")) {
|
|
6958
|
+
notebook = omd2notebook(content);
|
|
6959
|
+
}
|
|
6960
|
+
const retVal = compile(notebook, baseUrl);
|
|
6961
|
+
retVal.dispose = () => { };
|
|
6962
|
+
retVal.write = (w) => {
|
|
6963
|
+
w.import(path);
|
|
6964
|
+
};
|
|
6965
|
+
return retVal;
|
|
6966
|
+
}
|
|
6967
|
+
// @ts-ignore - use precompiled notebook from observable
|
|
6968
|
+
async function importCompiledNotebook(partial) {
|
|
6969
|
+
const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
|
|
6970
|
+
let impMod = {
|
|
6971
|
+
default: function (runtime, inspector) {
|
|
6972
|
+
return undefined;
|
|
6973
|
+
}
|
|
6974
|
+
};
|
|
6975
|
+
try {
|
|
6976
|
+
impMod = await obfuscatedImport(url);
|
|
6977
|
+
}
|
|
6978
|
+
catch (e) {
|
|
6979
|
+
}
|
|
6980
|
+
const retVal = impMod.default;
|
|
6981
|
+
retVal.dispose = () => { };
|
|
6982
|
+
retVal.write = (w) => {
|
|
6983
|
+
w.import(url);
|
|
6984
|
+
};
|
|
6985
|
+
return retVal;
|
|
6986
|
+
}
|
|
6987
|
+
function createVariable(inspect, name, inputs, definition, inline = false) {
|
|
6988
|
+
let i;
|
|
6989
|
+
let v;
|
|
6990
|
+
const retVal = (module, inspector) => {
|
|
6991
|
+
i = inspect ? inspector(name) : undefined;
|
|
6992
|
+
v = module.variable(i);
|
|
6993
|
+
if (arguments.length > 1) {
|
|
6994
|
+
try {
|
|
6995
|
+
v.define(name, inputs, definition);
|
|
6996
|
+
}
|
|
6997
|
+
catch (e) {
|
|
6998
|
+
console.error(e === null || e === void 0 ? void 0 : e.message);
|
|
6999
|
+
}
|
|
7000
|
+
}
|
|
7001
|
+
return v;
|
|
7002
|
+
};
|
|
7003
|
+
retVal.dispose = () => {
|
|
7004
|
+
var _a;
|
|
7005
|
+
try {
|
|
7006
|
+
(_a = i === null || i === void 0 ? void 0 : i._node) === null || _a === void 0 ? void 0 : _a.remove();
|
|
7007
|
+
}
|
|
7008
|
+
catch (e) {
|
|
7009
|
+
}
|
|
7010
|
+
i = undefined;
|
|
7011
|
+
try {
|
|
7012
|
+
v === null || v === void 0 ? void 0 : v.delete();
|
|
7013
|
+
}
|
|
7014
|
+
catch (e) {
|
|
7015
|
+
}
|
|
7016
|
+
v = undefined;
|
|
7017
|
+
};
|
|
7018
|
+
retVal.write = (w) => {
|
|
7019
|
+
if (inline) {
|
|
7020
|
+
w.define({ id: name, inputs, func: definition }, inspect, true);
|
|
7021
|
+
}
|
|
7022
|
+
else {
|
|
7023
|
+
const id = w.function({ id: name, func: definition });
|
|
7024
|
+
w.define({ id: name, inputs, func: definition }, inspect, false, id);
|
|
7025
|
+
}
|
|
7026
|
+
};
|
|
7027
|
+
return retVal;
|
|
7028
|
+
}
|
|
7029
|
+
function createImportVariable(name, alias) {
|
|
7030
|
+
let v;
|
|
7031
|
+
const retVal = (main, otherModule) => {
|
|
7032
|
+
v = main.variable();
|
|
7033
|
+
v.import(name, alias, otherModule);
|
|
7034
|
+
};
|
|
7035
|
+
retVal.dispose = () => {
|
|
7036
|
+
v === null || v === void 0 ? void 0 : v.delete();
|
|
7037
|
+
};
|
|
7038
|
+
return retVal;
|
|
7039
|
+
}
|
|
7040
|
+
async function createModule(parsed, text, baseUrl) {
|
|
7041
|
+
const otherModule = [".", "/"].indexOf(parsed.src[0]) === 0 ?
|
|
7042
|
+
await importFile(parsed.src, baseUrl) :
|
|
7043
|
+
await importCompiledNotebook(parsed.src);
|
|
7044
|
+
const importVariables = [];
|
|
7045
|
+
const variables = [];
|
|
7046
|
+
parsed.specifiers.forEach(spec => {
|
|
7047
|
+
const viewof = spec.view ? "viewof " : "";
|
|
7048
|
+
importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
|
|
7049
|
+
if (spec.view) {
|
|
7050
|
+
importVariables.push(createImportVariable(spec.name, spec.alias));
|
|
7051
|
+
}
|
|
7052
|
+
});
|
|
7053
|
+
variables.push(createVariable(true, undefined, ["md"], md => {
|
|
7054
|
+
return md `\`\`\`JavaScript
|
|
7055
|
+
${text}
|
|
7056
|
+
\`\`\``;
|
|
7057
|
+
}));
|
|
7058
|
+
const retVal = (runtime, main, inspector) => {
|
|
7059
|
+
let mod = runtime.module(otherModule);
|
|
7060
|
+
if (parsed.injections.length) {
|
|
7061
|
+
mod = mod.derive(parsed.injections, main);
|
|
7062
|
+
}
|
|
7063
|
+
variables.forEach(v => v(main, inspector));
|
|
7064
|
+
importVariables.forEach(v => v(main, mod));
|
|
7065
|
+
return mod;
|
|
7066
|
+
};
|
|
7067
|
+
retVal.importVariables = importVariables;
|
|
7068
|
+
retVal.variables = variables;
|
|
7069
|
+
retVal.dispose = () => {
|
|
7070
|
+
importVariables.forEach(v => v.dispose());
|
|
7071
|
+
variables.forEach(v => v.dispose());
|
|
7072
|
+
otherModule.dispose();
|
|
7073
|
+
};
|
|
7074
|
+
retVal.write = (w) => {
|
|
7075
|
+
otherModule.write(w);
|
|
7076
|
+
w.importDefine(parsed);
|
|
7077
|
+
};
|
|
7078
|
+
return retVal;
|
|
7079
|
+
}
|
|
7080
|
+
async function createCell(node, baseUrl) {
|
|
7081
|
+
const modules = [];
|
|
7082
|
+
const variables = [];
|
|
7083
|
+
try {
|
|
7084
|
+
const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
|
|
7085
|
+
const parsed = parseCell(text);
|
|
7086
|
+
switch (parsed.type) {
|
|
7087
|
+
case "import":
|
|
7088
|
+
modules.push(await createModule(parsed, text, baseUrl));
|
|
7089
|
+
break;
|
|
7090
|
+
case "viewof":
|
|
7091
|
+
variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7092
|
+
variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7093
|
+
break;
|
|
7094
|
+
case "mutable":
|
|
7095
|
+
variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
|
|
7096
|
+
variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
|
|
7097
|
+
variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
|
|
7098
|
+
break;
|
|
7099
|
+
case "variable":
|
|
7100
|
+
variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
|
|
7101
|
+
break;
|
|
7102
|
+
}
|
|
7103
|
+
}
|
|
7104
|
+
catch (e) {
|
|
7105
|
+
}
|
|
7106
|
+
const retVal = (runtime, main, inspector) => {
|
|
7107
|
+
modules.forEach(imp => imp(runtime, main, inspector));
|
|
7108
|
+
variables.forEach(v => v(main, inspector));
|
|
7109
|
+
};
|
|
7110
|
+
retVal.modules = modules;
|
|
7111
|
+
retVal.variables = variables;
|
|
7112
|
+
retVal.dispose = () => {
|
|
7113
|
+
variables.forEach(v => v.dispose());
|
|
7114
|
+
modules.forEach(mod => mod.dispose());
|
|
7115
|
+
};
|
|
7116
|
+
retVal.write = (w) => {
|
|
7117
|
+
modules.forEach(imp => imp.write(w));
|
|
7118
|
+
variables.forEach(v => v.write(w));
|
|
7119
|
+
};
|
|
7120
|
+
return retVal;
|
|
7121
|
+
}
|
|
7122
|
+
function createFile(file) {
|
|
7123
|
+
function toString() { return globalThis.url; }
|
|
7124
|
+
return [file.name, { url: new URL(file.url), mimeType: file.mime_type, toString }];
|
|
7125
|
+
}
|
|
7126
|
+
async function compile(notebook, baseUrl = ".") {
|
|
7127
|
+
const files = notebook.files.map(f => createFile(f));
|
|
7128
|
+
const fileAttachments = new Map(files);
|
|
7129
|
+
let cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
|
|
7130
|
+
const retVal = (runtime, inspector) => {
|
|
7131
|
+
const main = runtime.module();
|
|
7132
|
+
main.builtin("FileAttachment", runtime.fileAttachments(name => {
|
|
7133
|
+
var _a;
|
|
7134
|
+
return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(name), mimeType: null };
|
|
7135
|
+
}));
|
|
7136
|
+
cells.forEach(cell => {
|
|
7137
|
+
cell(runtime, main, inspector);
|
|
7138
|
+
});
|
|
7139
|
+
return main;
|
|
7140
|
+
};
|
|
7141
|
+
retVal.fileAttachments = fileAttachments;
|
|
7142
|
+
retVal.cells = cells;
|
|
7143
|
+
retVal.appendCell = async (n, baseUrl = ".") => {
|
|
7144
|
+
const cell = await createCell(n, baseUrl);
|
|
7145
|
+
cells.push(cell);
|
|
7146
|
+
return cell;
|
|
7147
|
+
};
|
|
7148
|
+
retVal.disposeCell = async (cell) => {
|
|
7149
|
+
cells = cells.filter(c => c !== cell);
|
|
7150
|
+
cell.dispose();
|
|
7151
|
+
};
|
|
7152
|
+
retVal.dispose = () => {
|
|
7153
|
+
cells.forEach(cell => cell.dispose());
|
|
7154
|
+
cells = [];
|
|
7155
|
+
};
|
|
7156
|
+
retVal.write = (w) => {
|
|
7157
|
+
w.files(notebook.files);
|
|
7158
|
+
cells.forEach(cell => cell.write(w));
|
|
7159
|
+
};
|
|
7160
|
+
retVal.toString = (w = new Writer()) => {
|
|
7161
|
+
retVal.write(w);
|
|
7162
|
+
return w.toString().trim();
|
|
7163
|
+
};
|
|
7164
|
+
return retVal;
|
|
7165
7165
|
}
|
|
7166
7166
|
|
|
7167
7167
|
export { Writer, compile, download, ojs2notebook, omd2notebook };
|