-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathattributeDerivatives.ts
More file actions
131 lines (125 loc) · 3.75 KB
/
Copy pathattributeDerivatives.ts
File metadata and controls
131 lines (125 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { BlockAttributes } from "block-attributes";
import produce from "immer";
import { findIntroducedSymbols } from "literate-elm";
import { AttributeDerivatives, BlockOutputFormat, OutputFormat } from "./types";
let unusedAutogeneratedContextId = 0;
export function extractAttributeDerivatives(
attributes: Readonly<BlockAttributes>,
): AttributeDerivatives | null {
const attributesWithMixIns = { ...attributes };
if (attributesWithMixIns.s === true || attributesWithMixIns.siding === true) {
attributesWithMixIns.isolated = true;
attributesWithMixIns.follows = "default";
}
if (attributesWithMixIns.isolated) {
attributesWithMixIns.context = `_autogenerated__${unusedAutogeneratedContextId}`;
unusedAutogeneratedContextId += 1;
}
const result: AttributeDerivatives = {
contextName:
typeof attributesWithMixIns.context !== "undefined"
? normalizeExpression(attributesWithMixIns.context)
: "default",
outputFormats: [],
interactive: true,
archive: false,
outputExpressionsByFormat: {},
id: attributesWithMixIns.id,
follows: attributesWithMixIns.follows,
};
let isLitVis = false;
for (const key in attributesWithMixIns) {
if (attributesWithMixIns.hasOwnProperty(key)) {
const value = attributesWithMixIns[key];
switch (key) {
case "l":
case "literate":
if (value === false) {
return null;
}
isLitVis = true;
if (value !== "hidden" && attributesWithMixIns["hide"] !== true) {
result.outputFormats.push("l");
}
break;
case "v":
case "visualize":
isLitVis = true;
addOutputExpressions(result, "v", value);
break;
case "r":
case "raw":
isLitVis = true;
addOutputExpressions(result, "r", value);
break;
case "j":
case "json":
isLitVis = true;
addOutputExpressions(result, "j", value);
break;
case "m":
case "markdown":
isLitVis = true;
addOutputExpressions(result, "m", value);
break;
case "archive":
result.archive = !!value;
break;
case "interactive":
result.interactive = !!value;
break;
}
}
}
if (isLitVis) {
return result;
}
return null;
}
/**
* Looks through outputExpressionsByFormat and replaces true (i.e. auto)
* with a lists of symbols introduced in the code block.
* Returns a new object if any changes.
* @param derivatives
* @param code
*/
export function resolveExpressions(
derivatives: Readonly<AttributeDerivatives>,
code: string,
) {
const introducedSymbols = findIntroducedSymbols(code);
const introducedNames = introducedSymbols.map((s) => s.name);
return produce(derivatives, (draft: AttributeDerivatives) => {
draft.outputFormats.forEach((referenceFormat) => {
if (
referenceFormat !== "l" &&
!draft.outputExpressionsByFormat[referenceFormat]
) {
draft.outputExpressionsByFormat[referenceFormat] = introducedNames;
}
});
});
}
function normalizeExpression(value) {
return `${value}`.trim();
}
function addOutputExpressions(
derivatives: AttributeDerivatives,
type: OutputFormat,
value,
) {
const expressionsToAdd: string[] = [];
if (value instanceof Array) {
value.forEach((v) => {
expressionsToAdd.push(normalizeExpression(v));
});
} else if (value !== true) {
expressionsToAdd.push(normalizeExpression(value));
}
if (expressionsToAdd.length || value === true) {
derivatives.outputFormats.push((type as any) as BlockOutputFormat);
}
if (expressionsToAdd.length) {
derivatives.outputExpressionsByFormat[type] = expressionsToAdd;
}
}