template_parser.rb
5.49 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
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
require 'nokogiri'
require 'json'
module Kanjai
class TemplateParser
def initialize(html_source)
@html_source = html_source
end
def get_elements(elements)
mas = []
elements.each do |item|
new_item = item.dup
new_item.delete(:children)
mas << new_item if need_elements.include?(new_item[:name])
mas = mas + get_need_elements(item[:children], need_elements)
end
mas
end
def find_subparts
result = []
@html_source.scan(/<!--###START element[\w\W]+?-->/).each do |item|
name, type, code = item.scan(/<!--###START\s+element\(name="([^"]+)",\s+type="([\w\s]+)",\s+id="(.+)"\)\s+###/)[0]
if name.present? and type.present?
source = item.gsub(/<!--###START\s+element\(name="([^"]+)",\s+type="([\w\s]+)",\s+id="(.+)"\)\s+###/, '')
source = source.gsub('-->', '')
source = source.gsub("\n", '')
source = source.gsub("\r", '')
source_dom = Nokogiri::HTML::fragment(source)
@field_options = []
conditions = []
elements = html_to_hash(source_dom)
source_dom.traverse do |node|
if node.name.to_s == 'condition' && node.attributes && node.attributes['id'] && node.attributes['itemname']
repeatItemId = ""
test = node.parent
while test
if test.name == 'repeat' && test.attributes["id"].present?
repeatItemId = test.attributes["id"].value
break
end
test = test.parent
end
conditions << {
id: node.attributes['id'].value,
name: node.attributes['itemname'].value,
repeat_id: repeatItemId,
type: node.attributes['type'] ? node.attributes['type'].value : 'enable'
}
end
end
#field_options = elements.select {|item| (item[:name] == 'element' or item[:name] == 'repeat' ) }
#field_options = get_elements(elements)
#elements.delete_if {|item| item[:name] == 'element' }
result << {
name: name,
type: type,
code: code,
source: source,
elements: elements,
field_options: @field_options,
conditions: conditions
}
end
end
result
end
def find_static_elements
result = []
@html_source.scan(/{"static":[\w\W\{]+?}}/).each do |str|
element_hash = JSON.parse(str)
result << {:name => identifier(element_hash["static"]), :itemName => element_hash["static"]["itemName"] , :type => element_hash["static"]["type"], :source => str}
end
result
end
def without_subparts
@html_source.gsub!(/<!--###START element[\w\W]+?-->/, '')
static_elements = self.find_static_elements
static_elements.each do |item|
@html_source.gsub!(item[:source], item[:name])
end
return @html_source
end
private
def html_to_hash(root)
mas = []
root.children.each do |item|
children = html_to_hash(item)
attr = {}
item.attributes.each do |key, value|
attr[key] = value.value
end
if item.name == 'repeat' && item.attributes["id"].present?
repeatItemId = ""
test = item.parent
while test
if test.name == 'repeat' && test.attributes["id"].present?
repeatItemId = test.attributes["id"].value
break
end
test = test.parent
end
@field_options << {:name => item.name, id: item.attributes["id"].value, :itemName => item.attributes["itemname"]&.value, repeatItemId: repeatItemId, :attributes => attr, :source => nil, :children => {}}
end
if item.name == "text"
source = item.text
else
source = nil
end
repeat = "false"
repeatItemId = ""
test = item
while test
if test.name == 'repeat' && test.attributes["id"].present?
repeat = "true"
repeatItemId = test.attributes["id"].value
break
end
test = test.parent
end
source.to_s.scan(/{"element":[\w\W\{]+?}}/).each do |str|
element_hash = JSON.parse(str)
@field_options << {:name => identifier(element_hash["element"]), :itemName => element_hash["element"]["itemName"] , main: element_hash["element"]["main"].to_i, :attributes => {'type' => element_hash["element"]["type"], 'repeat' => repeat, 'repeatItemId' => repeatItemId}, :source => nil, :children => {}}
source.gsub!(str, identifier(element_hash["element"]))
end
attr.each do |key, value|
value.to_s.scan(/{"element":[\w\W\{]+?}}/).each do |str|
element_hash = JSON.parse(str)
@field_options << {:name => identifier(element_hash["element"]), :itemName => element_hash["element"]["itemName"] , main: element_hash["element"]["main"].to_i, :attributes => {'type' => element_hash["element"]["type"], 'repeat' => repeat, 'repeatItemId' => repeatItemId}, :source => nil, :children => {}}
value.gsub!(str, identifier(element_hash["element"]))
end
end
mas << {:name => item.name, :attributes => attr, :source => source, :children => children}
end
mas
end
def identifier(el)
el['id'].present? ? el['id'] : el['title']
end
end
end