template_parser.rb
3.99 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
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 = []
p '!!!!!!!'
@html_source.scan(/<!--###START element[\w\W]+?-->/).each do |item|
p '!!!!!!!!!!!!!!'
name, type, code = item.scan(/<!--###START\s+element\(name="([^"]+)",\s+type="([\w\s]+)"\)\s+###/)[0]
if name.present? and type.present?
source = item.gsub(/<!--###START\s+element\(name="([^"]+)",\s+type="([\w\s]+)"\)\s+###/, '')
source = source.gsub('-->', '')
source = source.gsub("\n", '')
source = source.gsub("\r", '')
source_dom = Nokogiri::HTML::fragment(source)
@field_options = []
elements = html_to_hash(source_dom)
#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: name.parameterize,
source: source,
elements: elements,
field_options: @field_options
}
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 => element_hash["static"]["title"], :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'
@field_options << {:name => item.name, :itemName => nil, :attributes => attr, :source => nil, :children => {}}
end
if item.name == "text"
source = item.text
else
source = nil
end
repeat = "false"
test = item
while test
if test.name == 'repeat'
repeat = "true"
end
test = test.parent
end
source.to_s.scan(/{"element":[\w\W\{]+?}}/).each do |str|
element_hash = JSON.parse(str)
@field_options << {:name => element_hash["element"]["title"], :itemName => element_hash["element"]["itemName"] , main: element_hash["element"]["main"].to_i, :attributes => {'type' => element_hash["element"]["type"], 'repeat' => repeat}, :source => nil, :children => {}}
source.gsub!(str, element_hash["element"]["title"])
end
attr.each do |key, value|
value.to_s.scan(/{"element":[\w\W\{]+?}}/).each do |str|
element_hash = JSON.parse(str)
@field_options << {:name => element_hash["element"]["title"], :itemName => element_hash["element"]["itemName"] , main: element_hash["element"]["main"].to_i, :attributes => {'type' => element_hash["element"]["type"], 'repeat' => repeat}, :source => nil, :children => {}}
value.gsub!(str, element_hash["element"]["title"])
end
end
mas << {:name => item.name, :attributes => attr, :source => source, :children => children}
end
mas
end
end
end