-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
102 lines (77 loc) · 2.26 KB
/
test.lua
File metadata and controls
102 lines (77 loc) · 2.26 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
local PrettyPrint = require("pretty-print")
local Object = require("core").Object
local html = [[
<@TEST1 test='hi'>hi</@TEST1>
<@IN>
absa <@CHILD>aaaaaaa</@CHILD>
</@IN>
]]
-- ---@class fuzzy.FTML.Node: luvit.core.Object
-- local Node = Object:extend()
-- ---@param name string
-- function Node:initialize(name)
-- ---@type (fuzzy.FTML.Node|string)[]
-- self.children = {}
-- ---@type fuzzy.FTML.Node[]
-- self.nodes = setmetatable(self.children, {
-- __index = function(t, k)
-- return rawget(t, k)
-- end,
-- __newindex = function(t, k, v)
-- rawset(t, k, v)
-- end
-- })
-- if not name or name == "" then error("name is empty") end
-- if type(name) ~= "string" then error("name is not a string") end
-- self.name = name
-- ---@type fuzzy.FTML.Node
-- self.parent = nil
-- end
-- ---@param condition string|fun(node: string|fuzzy.FTML.Node): boolean
-- ---@param recursive boolean?
-- function Node:find(condition, recursive)
-- for i, node in pairs(self.children) do
-- if type(condition) == "function" then
-- if condition(node) then
-- return node
-- end
-- else
-- if node.name == condition then
-- return node
-- end
-- end
-- end
-- end
-- ---@param condition string|fun(node: string|fuzzy.FTML.Node): boolean
-- function Node:exists(condition)
-- return self:find(condition) ~= nil
-- end
-- ---@param node string|fuzzy.FTML.Node
-- function Node:add(node)
-- if type(node) == "table" then
-- node.parent = self
-- end
-- table.insert(self.children, node)
-- return node
-- end
local root = Node:new("__root")
local selector = root
for raw, closed_char, tag, content in html:gmatch("((/?)(@.-)>([^<]*)<)") do
-- print("raw {", raw, "}", "closed_char {", closed_char, "}", "name {", name, "}", "content {", content ,"}")
local name, raw_attr = tag:match("([@%w_%-]+)%s*(.*)")
if closed_char == "/" then
local node = selector.parent:find(name)
if not node then error("end tag with no start tag") end
selector = node.parent
else
if selector then
local node = Node:new(name)
node:add(content)
selector:add(node)
selector = node
end
end
::continue::
end
-- html:gsub("")
print(PrettyPrint.dump(root.nodes))