-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelm-mustache.js
537 lines (457 loc) · 14.8 KB
/
elm-mustache.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
const Mustache = require("mustache")
module.exports = {
makeTypesafeModule : makeTypesafeModule,
makeFullModule : makeFullModule,
_makeModule : _makeModule,
}
function makeTypesafeModule(moduleName, ast) {
let s = ""
for (let part of _makeModule(moduleName, "typesafe", ast, false)) {
s = s + part
}
return s
}
function makeFullModule(moduleName, ast) {
let s = ""
for (let part of _makeModule(moduleName, "full", ast, false)) {
s = s + part
}
return s
}
function *_makeModule(moduleName, type, ast, test) {
if (test) {
var tests = ast
}
yield "module " + moduleName + " exposing (..)\n"
+ "\n"
if (type === "typesafe") {
yield "import Mustache exposing (htmlEscape, lookup, interpolate, section, invertedSection)\n"
+ "\n"
}
if (type === "full") {
yield "import Json.Decode as D exposing (Value)\n"
+ "import Json.Encode as E\n"
+ "import Mustache exposing (htmlEscape, lookup, interpolate, section, invertedSection)\n"
+ "\n"
}
if (typeof tests !== "undefined") {
yield "import Test exposing (Test, test)\n"
+ "import Expect exposing (equal)\n"
+ "\n"
}
yield "comment : String\n"
+ "comment = \"\"\n"
+ "\n"
+ "setDelimiter : String -> String -> String\n"
+ "setDelimiter _ _ = \"\"\n"
+ "\n"
if (typeof tests === "undefined") {
const variables = {}
for (let line of makeRenderFunction("render", "Context", ast, variables, type)) {
yield line
}
yield "\n\n"
if (type === "typesafe") {
for (let line of makeContextType("Context", variables)) {
yield line
}
yield "\n\n"
}
return
}
for (let i = 0; i < tests.length; i++) {
const test = tests[i]
const testName = test.name
const testDesc = test.desc
const data = test.data
const template = test.template
const expected = test.expected
const ast = Mustache.parse(template)
const renderName = "render" + i
const contextName = "Context" + i
const suiteName = "suite" + i
const variables = {}
yield "\n\n{- "
+ testName
+ "\nThe template is:\n\n---\n"
+ template.replaceAll("\t", "\\t")
+ "\n---\nThe data is:\n\n---\n"
+ JSON.stringify(data, null, 4)
+ "\n---\n-}\n\n"
for (let line of makeRenderFunction(renderName, contextName, ast, variables, type)) {
yield line
}
yield "\n\n"
if (type === "typesafe") {
for (let line of makeContextType(contextName, variables)) {
yield line
}
yield "\n\n"
}
for (let line of makeTest(renderName, contextName, suiteName, data, expected, testName + " — " + testDesc, type)) {
yield line
}
yield "\n"
}
}
function *makeRenderFunction(renderName, contextName, ast, variables, type) {
if (type === "typesafe") {
yield renderName + " : " + contextName + " a -> String\n"
+ renderName + " c =\n "
}
else {
yield renderName + " : Value -> String\n"
+ renderName + " json = (\\context0 ->\n "
}
for (const line of makeRenderBody(ast, variables, type, 0)) {
yield line
}
if (type === "full") {
yield ") [json]"
}
}
function *makeRenderBody(ast, variables, type, depth) {
for (let i = 0; i < ast.length; i++) {
const currentSpan = ast[i]
const nodeType = currentSpan[0]
let content = currentSpan[1]
if (type === "typesafe" && ["name", "&", "#", "^"].includes(nodeType)) {
addVariable(variables, content, nodeType)
}
//
// nodeType === "text"
//
if (nodeType === "text") {
yield elmMultilineString(content)
}
//
// nodeType === "name"
//
else if (nodeType === "name" && content !== "." && type === "typesafe") {
yield "htmlEscape c." + elmVariableCase(content)
}
else if (nodeType === "name" && content === "." && type === "typesafe") {
yield "htmlEscape c.implicit"
}
else if (nodeType === "name" && content !== "." && type === "full") {
yield "htmlEscape (interpolate (lookup context" + depth + " [" +
content
.split(".")
.map(elmString)
.join(", ")
+ "]))"
}
else if (nodeType === "name" && content === "." && type === "full") {
yield "htmlEscape (interpolate (lookup context" + depth + " []))"
}
//
// nodeType === "&"
//
else if (nodeType === "&" && content !== "." && type === "typesafe") {
yield "c." + elmVariableCase(content)
}
else if (nodeType === "&" && content === "." && type === "typesafe") {
yield "c.implicit"
}
else if (nodeType === "&" && content !== "." && type === "full") {
yield "interpolate (lookup context" + depth + " [" +
content
.split(".")
.map(elmString)
.join(", ")
+ "])"
}
else if (nodeType === "&" && content === "." && type === "full") {
yield "interpolate (lookup context" + depth + " [])"
}
//
// nodeType === "#"
//
else if (nodeType === "#" && type === "typesafe") {
if (content === ".") {
yield "(if c.implicit then ("
} else {
yield "(if c." + elmVariableCase(content) + " then ("
}
if (currentSpan[4].length === 0) {
currentSpan[4].push (["text", ""])
}
for (const line of makeRenderBody(currentSpan[4], variables, type, depth + 1)) {
yield line
}
yield ") else \"\")"
}
else if (nodeType === "#" && type === "full") {
var path
if (content === ".") {
path = ""
} else {
path = content
.split(".")
.map(elmString)
.join(", ")
}
yield "(section context" + depth + " [" + path + "] (\\context" + (depth + 1) + " ->"
if (currentSpan[4].length === 0) {
currentSpan[4].push (["text", ""])
}
for (const line of makeRenderBody(currentSpan[4], variables, type, depth + 1)) {
yield line
}
yield "))"
}
//
// nodeType === "^"
//
else if (nodeType === "^" && type === "typesafe") {
if (content === ".") {
yield "(if not c.implicit then ("
} else {
yield "(if not c." + elmVariableCase(content) + " then ("
}
if (currentSpan[4].length === 0) {
currentSpan[4].push (["text", ""])
}
for (const line of makeRenderBody(currentSpan[4], variables, type, depth + 1)) {
yield line
}
yield ") else \"\")"
}
else if (nodeType === "^" && type === "full") {
var path
if (content === ".") {
path = ""
} else {
path = content
.split(".")
.map(elmString)
.join(", ")
}
yield "(invertedSection context" + depth + " [" + path + "] (\\context" + (depth + 1) + " ->"
if (currentSpan[4].length === 0) {
currentSpan[4].push (["text", ""])
}
for (const line of makeRenderBody(currentSpan[4], variables, type, depth + 1)) {
yield line
}
yield "))"
}
else if (nodeType === "!") {
yield "comment"
}
else if (nodeType === "=") {
const [left, right] = content.split(" ")
yield "(setDelimiter \"" + left + "\" \"" + right + "\")"
}
else if (nodeType === ">") {
throw new Error("Partials are not supported")
}
else {
throw new Error ("unknown node type" + nodeType)
}
if (typeof ast[i + 1] !== "undefined") {
yield " ++ "
}
}
}
function *makeContextType(contextName, variables) {
let length = 0
for (var varName in variables) {
if (Object.prototype.hasOwnProperty.call(variables, varName)) {
length ++
}
}
if (length === 0) {
yield "type alias " + contextName + " a = { dummy : a }\n"
return
}
yield "type alias " + contextName + " a =\n"
+ " { a"
let delim = "\n | "
for (var varName in variables) {
if (Object.prototype.hasOwnProperty.call(variables, varName)) {
const varType = variables[varName]
if (varType === "name") {
yield delim + elmVariableCase(varName) + " : String"
} else if (varType === "&") {
yield delim + elmVariableCase(varName) + " : String"
} else if (varType === "#") {
yield delim + elmVariableCase(varName) + " : Bool"
} else if (varType === "^") {
yield delim + elmVariableCase(varName) + " : Bool"
} else {
throw new Error("Unexpected varType: " + varType + " (varName is " + varName + ")")
}
delim = "\n , "
}
}
yield "\n }"
}
function *makeTest(renderName, contextName, suiteName, data, expected, desc, type) {
var dataStr
if (type === "typesafe") {
if (["boolean", "number", "string"].includes(typeof data)) {
dataStr = "{ implicit = " + elmLiteral(data, 4, false) + " }"
}
else if (typeof data === "object") {
dataStr = elmLiteral(data, 4, false)
}
else {
throw new Error(data)
}
}
else {
dataStr = elmJson(data, 4, true)
}
yield suiteName + " : Test\n"
+ suiteName + " = test \"\"\"" + desc + "\"\"\" <|\n"
+ " \\_ ->\n"
+ " "+ dataStr + "\n"
+ " |> " + renderName + "\n"
+ " |> equal " + elmMultilineString(expected) + "\n"
}
function elmJson(data, indent, needsParen) {
if (data === null) {
return "E.null"
}
else if (data === true) {
return "(E.bool True)"
}
else if (data === false) {
return "(E.bool False)"
}
if (typeof data === "number" && Number.isInteger(data)) {
return "(E.int " + data + ")"
}
if (typeof data === "number" && !Number.isInteger(data)) {
return "(E.float " + data + ")"
}
if (typeof data === "string") {
return "(E.string " + elmMultilineString(data) + ")"
}
else if (typeof data === "object" && Array.isArray(data)) {
let whitespace = " ".repeat(indent)
const a = []
for (const elem of data) {
a.push(elmJson(elem, indent + 2, false))
}
if (a.length === 0) {
return "(E.list identity [])"
}
if (a.length === 1) {
return "(E.list identity [" + a[0] + "])"
}
return "(E.list identity [ " + a.join("\n" + whitespace + ", ") + "\n" + whitespace + "])"
}
else if (typeof data === "object") {
const whitespace = " ".repeat(indent)
const a = []
for (let key of keys(data)) {
a.push("( " + elmString(key) + "\n " + whitespace + " , " + elmJson(data[key], indent + 4, false) + "\n" + whitespace + " )")
}
if (a.length === 0) {
return "(E.object [])"
}
return "(E.object\n" + whitespace + " [ " + a.join("\n " + whitespace + ", ") + "\n" + whitespace + " ])"
}
else {
throw new Error("Can't make a " + typeof data + " into an Elm Value")
}
}
function elmLiteral(data, indent, needsParen) {
if (data === true) {
return "True"
}
if (data === false) {
return "False"
}
if (typeof data === "number" && Number.isInteger(data)) {
if (needsParen) {
return "(String.fromInt " + data + ")"
}
return "String.fromInt " + data
}
if (typeof data === "number" && !Number.isInteger(data)) {
if (needsParen) {
return "(String.fromFloat " + data + ")"
}
return "String.fromFloat " + data
}
if (typeof data === "string") {
return elmMultilineString(data)
}
else if (typeof data === "object" && Array.isArray(data)) {
let whitespace = " ".repeat(indent)
const a = []
for (const elem of data) {
a.push(elmLiteral(elem, indent + 2, false))
}
if (a.length === 0) {
return "[]"
}
if (a.length === 1) {
return "[" + a[0] + "]"
}
return "[ " + a.join("\n" + whitespace + ", ") + "\n" + whitespace + "]"
}
else if (typeof data === "object") {
data = flattenObject(data)
let whitespace = " ".repeat(indent)
const a = []
for (var field of keys(data)) {
a.push(elmVariableCase(field) + " =\n " + whitespace +
elmLiteral(data[field], indent + 4, false))
}
if (a.length === 0) {
return "{ dummy = () }"
}
return "{ " + a.join("\n" + whitespace + ", ") + "\n" + whitespace + "}"
}
else {
throw new Error("Can't make a " + typeof data + " into an Elm literal")
}
}
function addVariable(variables, varName, varType) {
if (typeof variables[varName] !== "undefined" && variables[varName] !== varType) {
throw new Error("Incompatible variable types; " + variables[varName] + " and " + varType)
}
variables[varName] = varType
}
function elmString(s) {
return "\"" + s.replaceAll("\"", "\\\"").replaceAll("\n", "\\n") + "\""
}
function elmMultilineString(s) {
return "\"\"\"" + s.replaceAll("\"", "\\\"") + "\"\"\""
}
// TODO
function elmVariableCase(name) {
const leading = name.charCodeAt(0)
if(leading >= 48 && leading <= 57) {
name = "f" + name
}
// TODO Implicit iterators
if (name === ".") {
return "implicit"
}
return name.replaceAll(".", "_")
}
function keys(obj) {
const a = []
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
a.push(key)
}
}
return a
}
function flattenObject(obj, ctx=[], root={}) {
for (key of keys(obj)) {
value = obj[key]
if (typeof value === "object" && !Array.isArray(value)) {
flattenObject(value, ctx = ctx.concat(key), root)
} else {
root[ctx.concat(key).join("_")] = value
}
}
return root
}