-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.lua
More file actions
85 lines (51 loc) · 2.03 KB
/
codegen.lua
File metadata and controls
85 lines (51 loc) · 2.03 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
local parsetree = require("parsetree")
local codegen = {}
local function span(content, opt)
local classNames = {}
if opt.format then
classNames[#classNames + 1] = "mcformat-" .. opt.format
end
if opt.class then
classNames[#classNames + 1] = opt.class
end
local class = #classNames > 0 and ' class="' .. table.concat(classNames, " ") .. '"' or ""
local style = opt.style and ' style="' .. opt.style .. '"' or ""
return "<span" .. class .. style .. ">" .. content .. "</span>"
end
local function genComponents(components)
local result = ""
for _, component in ipairs(components) do
local generateResult = codegen.generate(component)
result = result .. generateResult
end
return result
end
function codegen.generate(node)
local result = ""
if node.kind == "MinecraftTextNode" then
local componentResult = genComponents(node.components)
result = result .. componentResult
elseif node.kind == "PlainTextNode" then
result = result .. node.content
elseif node.kind == "NewlineNode" then
result = result .. "<br>"
elseif node.kind == "NamedColorNode" then
local componentResult = genComponents(node.components)
result = result .. span(componentResult, { format = node.color })
elseif node.kind == "HexColorNode" then
local componentResult = genComponents(node.components)
result = result .. span(componentResult, { style = "color: #" .. node.color .. ";" })
elseif node.kind == "DecorationNode" then
local componentResult = genComponents(node.components)
result = result .. span(componentResult, { format = node.decoration })
elseif node.kind == "ShowTextNode" then
local tooltipResult = codegen.generate(node.tooltip)
local mctooltip = span(tooltipResult, { class = "mctooltip" })
local componentResult = genComponents(node.components)
result = result .. span(componentResult .. mctooltip, {})
else
error("This should not be reached")
end
return result
end
return codegen