Class: Spider::Layout
Constant Summary
collapse
- COMPILED_FOLDER =
'_c'
- @@named_layouts =
{}
Constants inherited
from Template
Template::ERBRegexp, Template::ExpressionOutputRegexp, Template::GettextRegexp, Template::SceneVarRegexp
Instance Attribute Summary collapse
Attributes inherited from Template
#_action, #_action_to, #_widget_action, #asset_profiles, #assets, #compiled, #content, #definer_class, #id_path, #mode, #overrides, #owner, #owner_class, #path, #request, #response, #runtime_overrides, #subtemplate_of, #subtemplates, #widgets
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Template
#add_overrides, #add_subtemplate, #add_widget, #add_widget_template, allow_blocks, allowed_blocks, #apply_override, #apply_overrides, #apply_widget_proc, asset_types, #bind, cache, cache_path, #compile, define_named_asset, define_runtime_asset, #do_widgets_before, #exec, find_resource, #find_widget, #get_el, get_named_asset, get_registered_class, #init_done?, #initialize, #inspect, #load, load, #load_subtemplate, #loaded?, named_assets, #override_tags, override_tags, #overrides_for, #parse_asset, parse_asset_element, #process_tags, real_path, #real_path, register, register_namespace, registered, registered?, #run, #run_block, #run_widgets, runtime_assets, scan_scene_vars, scan_text, #with_widget
Methods included from Logger
add, check_request_level, close, close_all, datetime_format, datetime_format=, #debug, debug, debug?, #debug?, enquire_loggers, #error, error, #error?, error?, fatal, #fatal, fatal?, #fatal?, info, #info, info?, #info?, #log, log, method_missing, open, reopen, request_level, send_to_loggers, set_request_level, unknown, #unknown, warn, #warn, warn?, #warn?
Instance Attribute Details
#asset_set ⇒ Object
Returns the value of attribute asset_set.
8
9
10
|
# File 'lib/spiderfw/templates/layout.rb', line 8
def asset_set
@asset_set
end
|
#single_layout ⇒ Object
Returns the value of attribute single_layout.
9
10
11
|
# File 'lib/spiderfw/templates/layout.rb', line 9
def single_layout
@single_layout
end
|
allow_blocks :HTML, :Text, :Render, :Yield, :If, :TagIf, :Each, :Pass, :Widget
7
8
9
|
# File 'lib/spiderfw/templates/layout.rb', line 7
def template
@template
end
|
Class Method Details
.clear_compiled_folder! ⇒ Object
476
477
478
|
# File 'lib/spiderfw/templates/layout.rb', line 476
def self.clear_compiled_folder!
FileUtils.rm_rf(Dir.glob(File.join(self.compiled_folder_path, '*')))
end
|
.compiled_folder_path ⇒ Object
.named_layouts ⇒ Object
199
200
201
|
# File 'lib/spiderfw/templates/layout.rb', line 199
def named_layouts
@@named_layouts
end
|
.register_layout(name, file) ⇒ Object
195
196
197
|
# File 'lib/spiderfw/templates/layout.rb', line 195
def register_layout(name, file)
@@named_layouts[name] = file
end
|
Instance Method Details
#all_assets ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/spiderfw/templates/layout.rb', line 205
def all_assets
tpl_assets = @template.is_a?(Layout) ? @template.all_assets : @template.assets
assets = self.assets + tpl_assets
if @only_asset_profiles
assets = assets.select{ |ass| ass[:profiles] && !(ass[:profiles] & @only_asset_profiles).empty? }
end
if @no_asset_profiles
assets = assets.select{ |ass| !ass[:profiles] || (ass[:profiles] & @no_asset_profiles).empty? }
end
assets
end
|
#asset_gettext_messages_file(path) ⇒ Object
223
224
225
226
227
|
# File 'lib/spiderfw/templates/layout.rb', line 223
def asset_gettext_messages_file(path)
dir = File.dirname(path)
name = File.basename(path, '.*')
File.join(dir, "#{name}.i18n.json")
end
|
#compile_asset(ass) ⇒ Object
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
|
# File 'lib/spiderfw/templates/layout.rb', line 229
def compile_asset(ass)
return ass unless ass[:src] && ass[:path]
if ass[:type] == :css
ext = File.extname(ass[:path])
compile_exts = ['.scss', '.sass', '.less']
if compile_exts.include?(ext)
ass_type = nil
if ext == '.less'
ass_type = :less
if @compile_less.nil?
@compile_less = false
if Spider.conf.get('css.compile_less')
begin
require 'spiderfw/templates/resources/less'
@compile_less = true
rescue LoadError
Spider.logger.error("Unable to compile LESS. Please install less-js gem and a JS backend.")
end
end
end
unless @compile_less
ass[:rel] = 'stylesheet/less'
return ass
end
elsif ['.scss', '.sass'].include?(ext)
ass_type = :sass
end
dir = File.dirname(ass[:path])
base = File.basename(ass[:path], ext)
newname = "#{base}.css"
parts = dir.split(File::SEPARATOR)
if type_i = parts.index(ass_type.to_s)
parts[type_i] = File.join(ass[:type].to_s, ass_type.to_s)
destdir = parts.join(File::SEPARATOR)
else
destdir = dir
end
FileUtils.mkdir_p(destdir)
dest = File.join(destdir, newname)
if Spider.conf.get('css.compile')
begin
compiler_class = if ass_type == :sass
require 'spiderfw/templates/resources/sass'
Spider::SassCompiler
elsif ass_type == :less
Spider::LessCompiler
end
compiler = compiler_class.new(ass[:app].pub_path)
compiler.compile(ass[:path], dest)
rescue Exception => exc
if ext == '.less'
msg = "Unable to compile LESS file #{ass[:path]}."
msg += "Please ensure you have a JS backend (see https://github.com/sstephenson/execjs)"
elsif ext == '.scss' || ext == '.sass'
msg = "Unable to compile SASS file #{ass[:path]}."
msg += "Please ensure that you have the 'sass' (and optionally 'compass') gems installed."
end
Spider.logger.error(msg)
if Spider.runmode == "production" && File.exist?(dest)
Spider.logger.error(exc)
else
raise
end
end
end
ass[:path] = dest
srcdir = File.dirname(ass[:src])
if destdir != dir
srcdir = srcdir.sub(ass_type.to_s, File.join(ass[:type].to_s, ass_type.to_s))
end
ass[:src] = File.join(srcdir, newname)
end
end
return ass
end
|
#compress_css(cpr) ⇒ Object
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
# File 'lib/spiderfw/templates/layout.rb', line 354
def compress_css(cpr)
require 'yui/compressor'
pub_dest = self.class.compiled_folder_path
name = cpr[:name]
already_compressed = Dir.glob(pub_dest+'/'+name+'.*.css')
unless already_compressed.empty?
return File.basename(already_compressed.first)
end
tmp_combined = Spider.paths[:tmp]+'/_'+name+'.css'
File.open(tmp_combined, 'w') do |f|
cpr[:assets].each do |a|
path = a[:path]
src_dir = File.dirname(path)
app = a[:app]
if app
if app.is_a?(Spider::Home)
app_relative_path = nil
app_path = app.path
else
app_relative_path = a[:app].relative_path
app_path = app.path
end
elsif path.index(Spider::SpiderController.pub_path) == 0
app_relative_path = 'spider'
app_path = Spider::SpiderController.pub_path
end
app_pathname = Pathname.new(app_path)
pub_app = "#{pub_dest}/#{app_relative_path}"
FileUtils.mkdir_p(pub_app)
src_files = Spider::ContentUtils.resolve_css_includes(path)
src = ""
src_files.each do |src_file|
src += IO.read(src_file)+"\n"
end
src.gsub!(/^\s*@import(?:\surl\(|\s)(['"]?)([^\?'"\)\s]+)(\?(?:[^'"\)]*))?\1\)?(?:[^?;]*);?/i, "")
src.scan(/url\([\s"']*([^\)"'\s]*)[\s"']*\)/m).uniq.collect do |url|
url = url.first
next if url =~ %r{^/} || url =~ %r{^[a-z]+://}
url, cb = url.split('?', 2)
path = ""
url_src = File.expand_path(File.join(src_dir, url))
src_pathname = Pathname.new(url_src)
src_rel = nil
begin
src_rel = src_pathname.relative_path_from(app_pathname)
rescue ArgumentError
raise "Can't combine CSS if paths go outside app: #{url} in #{path}"
end
url_dest = File.join(pub_app, src_rel.to_s)
FileUtils.mkdir_p(File.dirname(url_dest))
cachebuster = Spider.conf.get('css.cachebuster')
new_url = app_relative_path ? "#{app_relative_path}/#{src_rel}" : src_rel
if File.file?(url_src)
mtime = File.mtime(url_src).to_i
if cachebuster && File.exist?(url_dest) && mtime > File.mtime(url_dest).to_i
if cachebuster == :soft
FileUtils.cp(url_src, url_dest)
new_url += "?cb=#{mtime}"
elsif cachebuster == :hard || cachebuster == :hardcopy
url_dir = File.dirname(url)
url_ext = File.extname(url)
url_basename = File.basename(url, url_ext)
url_dest_dir = File.dirname(url_dest)
cb_file_name = "#{url_basename}-cb#{mtime}#{url_ext}"
new_url = "#{url_dir}/#{cb_file_name}"
if cachebuster == :hard
FileUtils.cp(url_src, url_dest)
else
FileUtils.cp(url_src, "#{url_dest_dir}/#{cb_file_name}")
end
end
else
FileUtils.cp(url_src, url_dest)
end
else
Spider.logger.error("CSS referenced file not found: #{url_src}")
end
if cb
url += "?#{cb}"
new_url += "?#{cb}"
end
src.gsub!(/\([\s"']*#{Regexp.quote(url)}[\s"']*\)/m, "(#{new_url})")
end
f.write(src+"\n")
end
end
version = 0
curr = Dir.glob(pub_dest+"/._#{name}.*.css")
unless curr.empty?
curr.each do |f|
currname = File.basename(f)
if currname =~ /(\d+)\.js$/
version = $1.to_i if $1.to_i > version
File.unlink(f)
end
end
end
version += 1
compiled_name = "#{name}.#{version}.css"
combined = "#{pub_dest}/._#{compiled_name}"
dest = "#{pub_dest}/#{compiled_name}"
FileUtils.cp(tmp_combined, combined)
File.unlink(tmp_combined)
compressor = ::YUI::CssCompressor.new("charset" => "UTF-8")
io = open(combined, 'r')
cjs = compressor.compress(io)
open(dest, 'w') do |f|
f << cjs
end
return compiled_name
end
|
#compress_javascript(cpr) ⇒ Object
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/spiderfw/templates/layout.rb', line 307
def compress_javascript(cpr)
require 'yui/compressor'
pub_dest = self.class.compiled_folder_path
name = cpr[:name]
already_compressed = Dir.glob(pub_dest+'/'+name+'.*.js')
unless already_compressed.empty?
return File.basename(already_compressed.first)
end
tmp_combined = Spider.paths[:tmp]+'/_'+name+'.js'
File.open(tmp_combined, 'w') do |f|
cpr[:assets].each.each do |a|
f.write IO.read(a[:path])+"\n"
end
end
version = 0
curr = Dir.glob(pub_dest+"/._#{name}.*.js")
unless curr.empty?
curr.each do |f|
currname = File.basename(f)
if currname =~ /(\d+)\.js$/
version = $1.to_i if $1.to_i > version
File.unlink(f)
end
end
end
version += 1
compiled_name = "#{name}.#{version}.js"
combined = "#{pub_dest}/._#{compiled_name}"
dest = "#{pub_dest}/#{compiled_name}"
FileUtils.cp(tmp_combined, combined)
File.unlink(tmp_combined)
compressor = ::YUI::JavaScriptCompressor.new("charset" => "UTF-8")
io = open(combined, 'r')
cjs = compressor.compress(io)
open(dest, 'w') do |f|
f << cjs
end
return compiled_name
end
|
#init(scene) ⇒ Object
12
13
14
15
16
17
|
# File 'lib/spiderfw/templates/layout.rb', line 12
def init(scene)
super
@template = @template.is_a?(Template) ? @template : Template.new(@template)
@template.init(scene) unless @template.init_done?
end
|
#no_asset_profiles(*profiles) ⇒ Object
29
30
31
|
# File 'lib/spiderfw/templates/layout.rb', line 29
def no_asset_profiles(*profiles)
@no_asset_profiles = profiles
end
|
#only_asset_profiles(*profiles) ⇒ Object
25
26
27
|
# File 'lib/spiderfw/templates/layout.rb', line 25
def only_asset_profiles(*profiles)
@only_asset_profiles = profiles
end
|
#prepare_asset(ass, compress_assets = {}, js_translations = {}) ⇒ Object
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
|
# File 'lib/spiderfw/templates/layout.rb', line 107
def prepare_asset(ass, compress_assets={}, js_translations={})
type = ass[:type].to_sym
assets = {:css => [], :js => []}
pub_dest = nil
use_cdn = Spider.conf.get('assets.use_cdn')
compress_config = case type
when :js
'javascript.compress'
when :css
'css.compress'
end
no_compress = @scene.__is_error_page || !Spider.conf.get(compress_config) || \
ass[:no_compress] || ass[:runtime] || ass[:if_ie_lte] || ass[:media] || (use_cdn && ass[:cdn])
if no_compress
if ass[:runtime]
assets[type] << {:src => Spider::Template.runtime_assets[ass[:runtime]].call(@request, @response, @scene)}
else
assets[type] << ass
end
else
unless pub_dest
pub_dest = self.class.compiled_folder_path
FileUtils.mkdir_p(pub_dest)
end
if comp = ass[:compressed_path] name = File.basename(comp)
if ass[:compressed_rel_path] dir = File.dirname(ass[:compressed_rel_path])
if ass[:copy_dir] start = dir
if ass[:copy_dir].is_a?(Fixnum) ass[:copy_dir].downto(0) do |i|
start = File.dirname(start)
end
end
dst_dir = File.join(pub_dest, start)
unless File.dirname(start) == '.' || File.directory?(File.dirname(dst_dir))
FileUtils.mkdir_p(File.join(pub_dest, File.dirname(dst_dir)))
end
unless File.directory?(dst_dir)
FileUtils.cp_r(File.join(ass[:app].pub_path, start), dst_dir)
end
else
FileUtils.mkdir_p(File.join(pub_dest, dir))
FileUtils.cp(comp, File.join(pub_dest, dir)) unless File.exist?(File.join(pub_dest, dir))
end
src = dir+'/'+name
else
unless File.exist?(File.join(pub_dest, name))
FileUtils.cp(comp, pub_dest)
end
src = name
end
ass[:src] = Spider.home.controller.pub_url+'/'+COMPILED_FOLDER+'/'+src
assets[type] << ass
else name = ass[:compress] || @cname
unless compress_assets[type][name]
cpr = {:name => name, :assets => [], :cpr => true}
assets[type] << cpr
compress_assets[type][name] = cpr
end
compress_assets[type][name][:assets] << ass
end
end
if ass[:gettext] && type == :js
msg_path = asset_gettext_messages_file(ass[:path])
if File.exists?(msg_path)
js_messages = JSON.parse(File.read(msg_path))
Spider::GetText.in_domain(ass[:app].short_name) do
js_messages.each{ |msg|
next if js_translations.key?(msg)
js_translations[msg] = _(msg)
}
end
else
Spider.logger.warn("Javascript Gettext file #{msg_path} not found")
end
end
assets
end
|
#prepare_assets ⇒ Object
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
|
# File 'lib/spiderfw/templates/layout.rb', line 33
def prepare_assets
@template_assets = { :css => [], :js => [] }
assets = {:css => [], :js => []}
compress_assets = {:js => {}, :css => {}}
seen = {}
js_translations = {}
use_cdn = Spider.conf.get('assets.use_cdn')
cname = File.basename(@path, '.layout.shtml')
cname = File.basename(cname, '.shtml')
cname += "-#{@asset_set}" if @asset_set
@cname = cname
@content[:yield_to] = @template
if !/(admin|login|error|portal|simple|generic|cms|stampa)/.match(cname).blank? || self.single_layout
all_assets.each do |ass|
seen_check = ass[:runtime] || ass[:src]
next if ass[:src].blank? && !ass[:runtime]
next if seen[seen_check]
seen[seen_check] = true
ass[:app] = Spider.home if ass[:app] == :home
ass = compile_asset(ass)
res = prepare_asset(ass, compress_assets, js_translations)
assets[:css] += res[:css]
assets[:js] += res[:js]
end
assets[:css] = assets[:css].sort_by { |hsh| hsh[:order].to_i }
assets[:js] = assets[:js].sort_by { |hsh| hsh[:order].to_i }
if @compile_less == false
less = Spider::Template.get_named_asset('less')
less.each do |ass|
res = prepare_asset(parse_asset(ass[:type], ass[:src], ass).first)
assets[:css] += res[:css]
assets[:js] += res[:js]
end
end
assets[:js].each do |ass|
if ass[:cpr]
compressed = compress_javascript(ass)
@template_assets[:js] << Spider.home.controller.pub_url+'/'+COMPILED_FOLDER+'/'+compressed
else
ass[:src] = ass[:cdn] if ass[:cdn] && use_cdn
@template_assets[:js] << ass[:src]
end
end
assets[:css].each do |ass|
if ass[:cpr]
compressed = compress_css(ass)
@template_assets[:css] << Spider.home.controller.pub_url+'/'+COMPILED_FOLDER+'/'+compressed
else
ass[:src] = ass[:cdn] if ass[:cdn] && use_cdn
is_dyn = ass[:if_ie_lte] || ass[:media] || ass[:rel]
@template_assets[:css] << (is_dyn ? ass : ass[:src])
end
end
@scene.assets = @template_assets
end
@scene.extend(LayoutScene)
if js_translations.empty?
@scene.js_translations = ""
else
@scene.js_translations = "var translations = #{js_translations.to_json}"
end
@assets_prepared = true
end
|
#render(*args) ⇒ Object
19
20
21
22
23
|
# File 'lib/spiderfw/templates/layout.rb', line 19
def render(*args)
$PUB_URL = Spider.home.controller.pub_url
prepare_assets unless @assets_prepared
super
end
|