parse.lua

main 61 lines · 1.5 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1local parseAttrPropList = function(str)
2 local obj = {}
3 for _, style_v in pairs(str:split(';')) do
4 local attr = style_v:trim():split(':')
5 local name = attr[1]
6 if name then
7 local value = attr[2]:trim()
8 value = tonumber(value) or value
9 obj[name:trim()] = value
10 end
11 end
12 return obj
13end
14
15local parseStyle = function(widget, el)
16 local styleStr = el.attributes.style:trim()
17 if styleStr == '' then
18 return
19 end
20
21 local style = parseAttrPropList(styleStr)
22 widget:mergeStyle(style)
23 el.style = style
24end
25
26local parseLayout = function(widget, el)
27 local layout = parseAttrPropList(el.attributes.layout)
28 widget:mergeStyle({ layout = layout })
29end
30
31local function parseExpression(content, controller)
32 local lastPos = nil
33 while true do
34 local pos = content:find('{{', lastPos)
35 if not pos then
36 break
37 end
38
39 lastPos = content:find('}}', lastPos)
40
41 local script = content:sub(pos + 2, lastPos - 1)
42
43 local res = nil
44 if content:sub(pos - 2, pos - 1) == 'tr' then
45 pos = pos - 2
46 res = tr(script)
47 else
48 local f = loadstring('return function(self) return ' .. script .. ' end')
49 res = f()(controller)
50 end
51
52 if res then
53 content = table.concat { content:sub(1, pos - 1), res, content:sub(lastPos + 2) }
54 end
55 end
56
57 return content
58end
59
60return parseStyle, parseLayout, parseExpression
61
Top