template_generator.rb
11.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
module Kanjai
module TemplateGenerator
def self.subpart_generate(domain, page_template, type, elements, hash_value = nil, page_content_id = nil)
@doc = Nokogiri::HTML::DocumentFragment.parse ""
@original_hash_value = hash_value
@original_hash_value ||= {}
@page_content_id = page_content_id
Nokogiri::HTML::Builder.with(@doc) do |subparts|
TemplateGenerator.method("#{type}_generator").call(domain, elements, subparts, @original_hash_value[0])
end
@doc.css("img[@src]").each do |x|
image_link = x['src']
unless image_link.match(/^(http:|https:)/)
x['src'] = page_template.template_content_url + image_link
end
end
@doc.to_html
end
def self.menu_generator(domain, elements, subparts, hash_value, current_page = nil)
elements.each do |item|
case item[:name]
when 'text'
content = item[:source]
if current_page
content = content.gsub('###link###', current_page.menu_url('http://', lang = nil))
.gsub('###title###', current_page.lang_attributes(domain.page_langs.default, :title))
end
subparts << content
when 'repeat'
#get template for normal state, active state and separator
normal_state = item[:children].select{|item2| item2[:name] == 'normal' }.first
active_state = item[:children].select{|item2| item2[:name] == 'active' }.first
separator = item[:children].select{|item2| item2[:name] == 'separator' }.first
next_level = item[:children].select{|item2| item2[:name] == 'level' }.first
normal_state_template = active_state_template = separator_template = ''
if normal_state
doc_normal_state = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(doc_normal_state) do |repeat_part|
self.menu_generator(normal_state[:children], repeat_part, nil)
end
normal_state_template = doc_normal_state.to_html
end
if active_state
doc_active_state = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(doc_active_state) do |repeat_part|
self.menu_generator(active_state[:children], repeat_part, nil)
end
active_state_template = doc_active_state.to_html
end
if separator
doc_separator = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(doc_separator) do |repeat_part|
self.menu_generator(separator[:children], repeat_part, nil)
end
separator_template = doc_separator.to_html
end
page_collection = Page.joins(:domain).where(kanjai_domains: {id: domain.id}).order(:position)
if item[:attributes]["ids"].present?
page_collection = page_collection.where('id in (?)', item[:attributes]["ids"])
end
unless item[:attributes]["root_id"].nil?
root_id = item[:attributes]["root_id"].to_s
root_id = nil if root_id.empty?
page_collection = page_collection.where(:parent_id => root_id)
end
unless current_page.nil?
page_collection = page_collection.where(:parent_id => current_page.id)
end
begin
if !!UserSession
if UserSession.current_user
page_collection = page_collection.where(:private_flag => [true, false])
.where(show_public_only: false)
else
page_collection = page_collection.where(private_flag: false)
end
end
rescue
page_collection = page_collection.where(private_flag: false)
end
page_collection.each do |page|
if page.children.length > 0 and next_level
subparts << item[:source]
self.menu_generator(next_level[:children], subparts, nil, page)
else
if session[:url] == page.lang_attributes(I18n.locale, :url)
if active_state_template.present?
subparts << active_state_template.gsub('###link###', page.menu_url(session[:scheme], lang = nil))
.gsub('###title###', page.lang_attributes(I18n.locale, :title))
end
else
if normal_state_template.present?
subparts << normal_state_template.gsub('###link###', page.menu_url(session[:scheme], lang = nil))
.gsub('###title###', page.lang_attributes(I18n.locale, :title))
end
end
if separator_template.present? and page != page_collection.last
subparts << separator_template
end
end
end
else
attributes = item[:attributes]
if current_page
p attributes
end
subparts.send(item[:name], attributes) do |next_subparts|
self.menu_generator(item[:children], next_subparts, nil, current_page)
end
end
end
end
def self.language_generator(domain, elements, subparts, hash_value)
elements.each do |item|
case item[:name]
when 'text'
lang = domain.page_langs.find_by_code(session[:current_locale])
content = item[:source]
content = content.gsub('###CURRENT_LANG_TITLE###', lang.title)
.gsub('###CURRENT_LANG_CODE###', lang.code)
subparts << content
when 'repeat'
#get template for normal state, active state and separator
item_state = item[:children].select{|item2| item2[:name] == 'item' }.first
active_state = item[:children].select{|item2| item2[:name] == 'active' }.first
if item_state
doc_item_state = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(doc_item_state) do |repeat_part|
self.language_generator(item_state[:children], repeat_part, nil)
end
item_state_template = doc_item_state.to_html
end
if active_state
doc_active_state = Nokogiri::HTML::DocumentFragment.parse ""
Nokogiri::HTML::Builder.with(doc_active_state) do |repeat_part|
self.language_generator(active_state[:children], repeat_part, nil)
end
active_state_template = doc_active_state.to_html
end
page_data = PageDatum.where(:url => session[:url], :lang => session[:current_locale]).first
if page_data
page = page_data.page
end
domain.page_langs.all.each do |lang|
if lang.code.to_s == session[:current_locale].to_s && active_state_template
content = active_state_template.gsub('###LANG_CODE###', lang.code)
.gsub('###LANG_TITLE###', lang.title)
elsif item_state_template.present?
content = item_state_template.gsub('###LANG_CODE###', lang.code)
.gsub('###LANG_TITLE###', lang.title)
end
if content
if page
content = content.gsub('###PAGE_LINK###', page.menu_url(session[:scheme], lang.code))
else
content = content.gsub('###PAGE_LINK###', '/')
end
subparts << content
end
end
else
attributes = item[:attributes]
subparts.send(item[:name], attributes) do |next_subparts|
self.language_generator(item[:children], next_subparts, nil)
end
end
end
end
def self.content_generator(domain, elements, subparts, hash_value)
elements.each do |item|
case item[:name]
when 'text'
subparts << self.replace_text_marker(item[:source], hash_value)
when 'repeat'
@original_hash_value.each do |key, value|
if key > 0
if(key == 1)
value['###FIRST_ITEM_ACTIVE###'] = 'active'
else
value['###FIRST_ITEM_ACTIVE###'] = ''
end
value['###REPEAT_NUMBER###'] = key.to_s
self.content_generator(domain, item[:children], subparts, value)
end
end
else
attributes = self.replace_attributes_marker(item[:attributes], hash_value)
if item[:name] == 'select'
subparts.select(attributes) do |next_subparts|
self.content_generator(domain, item[:children], next_subparts, hash_value)
end
elsif item[:name] == 'p'
if item[:children].count > 0
subparts.p(attributes, self.replace_text_marker(item[:children][0][:source], hash_value).html_safe)
end
else
subparts.send(item[:name], attributes) do |next_subparts|
self.content_generator(domain, item[:children], next_subparts, hash_value)
end
end
end
end
end
def self.system_generator(domain, elements, subparts, hash_value)
str = ''
controller_item = elements.select{|item| item[:name] == 'controller' }.first
action_item = elements.select{|item| item[:name] == 'action' }.first
if controller_item and action_item
controller_name_item = controller_item[:children].first
action_name_item = action_item[:children].first
if controller_name_item and action_name_item
controller_name = controller_name_item[:source]
action_name = action_name_item[:source]
controller = controller_name.constantize.new
controller.send action_name
controller_variables = controller.instance_variables
hash = {}
controller_variables.each do |variable|
hash[variable.to_s[1..-1]] = controller.instance_variable_get(variable.to_s)
end
str = controller_name.constantize.render action_name.to_sym, assigns: hash
end
end
subparts << str
end
def self.replace_text_marker(text, hash_value)
new_text = text.dup
if !hash_value.nil? and !text.nil?
hash_value.each do |key, value|
if !key.nil? and !value.nil?
new_text.gsub!(key, value.gsub(/\r\n/, "#newline#"))
end
end
end
new_text
end
def self.replace_attributes_marker(attributes, hash_value)
new_attributes = attributes.deep_dup
attributes.each do |key, text|
if !hash_value.nil? and !text.nil?
hash_value.each do |key2, value2|
if !key2.nil? and !value2.nil?
new_attributes[key].gsub!(key2, value2)
new_attributes[key].gsub!('###FORM_ACTION###', "/#{I18n.locale}/form/#{@page_content_id}")
end
end
end
end
new_attributes
end
end
end