/
site_node_engine.rb
1156 lines (983 loc) · 41 KB
/
site_node_engine.rb
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
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
353
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2009 Pascal Rettig.
require "mime/types"
require 'digest/sha1'
=begin rdoc
SiteNodeEngine is the class that is responsible for assembling the various paragraphs and frameworks
and doing what's necessary to render the page.
Most of the time it's not a class that you'll have to use, but as it forms the backbone of Webiva,
it's important to know a little bit about it:
At the most basic level, if you need to render a specific site node, you could do something like the following:
begin
@page = SiteNode.find(@site_node_id)
engine = SiteNodeEngine.new(@page,:display => session[:cms_language], :path => path_args)
# Call engine.run with the controller you're in and the current EndUser object
@output = engine.run(self,myself)
# catch a couple of exceptions and render a 404
rescue ActiveRecord::RecordNotFound, SiteNodeEngine::NoActiveVersionException, SiteNodeEngine::MissingPageException => e
render :text => 'Page not found', :status => 404
return
end
if @output.redirect?
redirect_to(@output.redirect, :status => 301)
elsif @output.document?
handle_document_node(@output,@page)
elsif @output.page?
render :text => render_output(@page,@output)
end
The last render comment would just render the body of the page (without a <html> or <head> tag,
see layouts/page.rhtml for what's necessary to render a full html page.
If you'd like to use SiteNodeEngine but generate your own body in a standard-ish rails controller
you can subclass ModuleAppController and add whatever routes you need to routes.rb. If create a site
tree that mimics your routes and a "Module application" paragraph your application will rendered on
whatever pages are necessary.
=end
class SiteNodeEngine
# These methods are include in the ModuleAppController and aid
# in rendering site nodes
module Controller
# Return a page given an array of page arguments and a site_version_id
def find_page_from_path(path_elems,site_version_id)
path_elems = path_elems.clone
path_args = []
page = nil
search_path = "/" + path_elems.join("/")
loop do
page = SiteNode.find(:first,
:conditions => ['node_path = ? AND node_type IN ("P","M","D","J") AND site_version_id=?',search_path,site_version_id],
:order => 'position DESC')
break if page || path_elems.length == 0
path_args.unshift(path_elems.slice!(-1))
search_path = "/" + path_elems.join('/')
end
return page,path_args
end
# Correctly render a document node which is the result of a rendering a page where
# a paragraph calls ParagraphRenderer#data_paragraph or the rendering of a DocumentNode
def handle_document_node(output,page)
# if we have data, just output the data
if output.text?
render output.options
return
elsif output.data?
send_data(output.data,output.options)
return
# if we have just a filename, assume its an inline file like an image
elsif output.file?
filename = output.file
mime_types = MIME::Types.type_for(filename)
if USE_X_SEND_FILE
x_send_file(filename,
:type => mime_types[0] ? mime_types[0].to_s : 'text/plain',
:disposition => 'inline'
)
else
send_file(filename,
:type => mime_types[0] ? mime_types[0].to_s : 'text/plain',
:disposition => 'inline'
)
end
return
# If we have a domain file, send that
elsif output.domain_file?
domain_file = output.domain_file
filename = domain_file.filename
name = domain_file.name
# Otherwise we have a document node (domain file), that needs to be sent
else
domain_file = page.domain_file
if domain_file.folder?
args = "/" + output.path_args.join("/").to_s.strip
domain_file = DomainFile.find_by_file_path(domain_file.file_path + args)
if domain_file
filename = domain_file.filename
name = domain_file.name
else
raise MissingPageException.new(page,@output.language)
end
else
filename = domain_file.filename
name = page.title # domain_file.name
end
end
mime_types = MIME::Types.type_for(filename)
if USE_X_SEND_FILE
x_send_file(filename,
:type => mime_types[0] ? mime_types[0].to_s : 'text/plain',
:disposition => domain_file.image? ? 'inline' : 'attachment',
:filename => name)
else
send_file(filename,
:type => mime_types[0] ? mime_types[0].to_s : 'text/plain',
:disposition => domain_file.image? ? 'inline' : 'attachment',
:filename => name)
end
end
# Takes a paragraph and runs it through the renderer (calling the
# appropriate renderer method), doesn't actually render the
# output if the paragraph is rendering a partial
def compile_paragraph(site_node,revision,paragraph,opts={})
# Get the type of paragraph
paragraph = PageParagraph.thaw(paragraph) if paragraph.is_a?(Hash)
paragraph.language = opts[:language]
# Handle any paragraph inputs
# return nil unless we have all the inputs we need
return nil unless set_page_connections(paragraph,opts)
opts[:language] ||= ( Locale.language ? Locale.language.code : Configuration.languages[0] )
opts[:page_path] ||= site_node.node_path
rendering_module = paragraph.display_module
# We'll have something like /editor/menu
if rendering_module
return render_module_paragraph(site_node,revision,paragraph,opts)
else
display_type = paragraph.display_type
if paragraph.display_body.blank? && paragraph.display_body == '' && !opts[:edit]
return ParagraphRenderer::ParagraphOutput.new(self,:text => "")
elsif opts[:edit]
return ParagraphRenderer::ParagraphOutput.new(self,:text => "<div class='#{paragraph.display_type}_paragraph'>" + strip_script_tags(paragraph.display) + "</div>")
else
return ParagraphRenderer::ParagraphOutput.new(self,:text => "<div class='#{paragraph.display_type}_paragraph'>" + paragraph.display + "</div>")
end
end
end
def render_module_paragraph(site_node,revision,paragraph,opts)
display_type = paragraph.display_type
cls = paragraph.renderer
info = paragraph.info
output = nil
cache_hash=''
output = fetch_from_paragraph_cache(info,paragraph,opts) if info[:paragraph_cache] && !opts[:edit]
return output if output # early out with cached paragraph
rnd = cls.new(myself.user_class,self,paragraph,site_node,revision,opts)
set_page_connection_hash(rnd,paragraph) if opts[:ajax] && params[:page_connection_hash]
rnd.capture_data = true if opts[:capture]
if paragraph.site_feature_id && !opts[:edit]
# If we're not in the editor, include the feature css
# We have the css attribute from paragraph cached from the thaw
if paragraph.render_css.nil? ? (paragraph.site_feature && !paragraph.site_feature.rendered_css.blank?) : paragraph.render_css
rnd.require_css("/stylesheet/f#{paragraph.site_feature_id}.css")
end
end
## Actually Call the method on the renderer ##
rnd.send(display_type)
if info[:paragraph_cache] && !opts[:edit]
output = rnd.output
output_hash = { :html => render_paragraph(site_node,revision,output,opts),
:includes => output.includes,
:page_connections => output.page_connections }
DataCache.put_container("Paragraph","#{paragraph.display_module}:#{opts[:language]}:#{myself.user_class_id}:#{cache_hash}",output_hash)
return ParagraphRenderer::CachedOutput.new(output_hash[:html],output_hash[:includes],output_hash[:page_connections])
end
# Add in the rendered css for the editor
editor_add_rendered_css!(rnd,paragraph) if opts[:edit]
return rnd.output
end
def fetch_from_paragraph_cach(info,paragraph,opts={})
cache_hash = cls.send(info[:paragraph_cache],paragraph,myself.user_class)
# If this user class doesn't have a cache hash
if cache_hash
output_hash = DataCache.get_container("Paragraph","#{paragraph.display_module}:#{opts[:language]}:#{myself.user_class_id}:#{cache_hash}")
output = ParagraphRenderer::CachedOutput.new(output_hash[:html],output_hash[:includes],output_hash[:page_connections]) if output_hash
end
end
def set_page_connection_hash(rnd,paragraph)
pch = (session[:page_connection_hash] || {})[paragraph.id.to_s + "_" + params[:page_connection_hash]]
rnd.set_page_connection_hash(pch) if pch
end
def editor_add_rendered_css!(rnd,paragraph)
if paragraph.site_feature_id && paragraph.site_feature
rnd.output.render_args[:css] = paragraph.site_feature.rendered_css unless paragraph.site_feature.rendered_css.blank?
end
if rnd.output.is_a?(ParagraphRenderer::ParagraphOutput) && rnd.output.includes && rnd.output.includes[:css]
css_files = rnd.output.includes[:css]
rnd.output.render_args[:css] ||= ''
css_files.each do |fl|
begin # TODO - extract this and fix path generation
fl += ".css" unless fl[-4..-1] == ".css"
fl = "/stylesheets/" + fl if fl[0..0] != '/'
css = IO.readlines(File.join(Rails.root,"public",fl)).join
rnd.output.render_args[:css] << "\n"
rnd.output.render_args[:css] << css
rescue Exception => e
# chomp
raise e if Rails.env != 'production'
end
end
end
end
def set_page_connections(paragraph,opts={})
if !opts[:edit] && paragraph.connections && paragraph.connections[:inputs].is_a?(Hash)
opts[:connections] ||= {}
paragraph.connections[:inputs].each do |input_key,input|
if input[0].to_s == "0"
case input[1].to_s
when 'page_arg_0':
paragraph.set_page_connections(input_key => params[:path][0])
when 'page_arg_1':
paragraph.set_page_connections(input_key => params[:path][1])
when 'page_arg_2':
paragraph.set_page_connections(input_key => params[:path][2])
when 'user_target':
paragraph.set_page_connections(input_key => myself)
when 'logged_in_target':
paragraph.set_page_connections(input_key => myself.id ? myself : nil)
when 'title':
if opts[:connections][:title]
paragraph.set_page_connections(input_key => opts[:connections][:title])
elsif !opts[:edit]
return nil
end
when 'title_str':
if opts[:connections][:title_str]
paragraph.set_page_connections(input_key => opts[:connections][:title_str] )
elsif !opts[:edit]
return nil
end
end
else
if opts[:connections][input[0].to_s] && opts[:connections][input[0].to_s].has_key?(input[1].to_sym)
paragraph.set_page_connections(input_key.to_sym => opts[:connections][input[0].to_s][input[1].to_sym])
elsif !opts[:edit] && !opts[:ajax]
return nil
#paragraph.info[:cache] = false # We might not have the right connections if we are in the pre-compile step
end
end
end
end
true
end
def strip_script_tags(txt)
txt.gsub(/\<script.*?\<\/script\>/im,'')
end
# Renders a paragraph, returns a string containing the outputed html
# will compile the paragraph if necessary
def render_paragraph(site_node,revision,paragraph,opts = {})
opts[:edit] = true if opts[:editor]
if(paragraph.is_a?(PageParagraph) || paragraph.is_a?(Hash))
paragraph = compile_paragraph(site_node,revision,paragraph,opts)
end
cls= paragraph.class
cls_name = cls.to_s
if cls_name == "ParagraphRenderer::ParagraphOutput"
return '' if paragraph.render_args[:nothing]
css = paragraph.render_args.delete(:css)
str = ''
if paragraph.render_args[:text]
str = paragraph.render_args[:text]
elsif paragraph.render_args[:inline]
@paragraph = paragraph
str = render_to_string(paragraph.render_args)
elsif paragraph.render_args[:partial]
paragraph.render_args[:partial] = paragraph.render_args[:partial]
paragraph.render_args[:locals] ||= {}
paragraph.render_args[:locals][:renderer] = paragraph.rnd unless paragraph.render_args[:locals][:renderer]
@paragraph = paragraph
str = render_to_string(paragraph.render_args)
elsif paragraph.render_args[:rjs]
paragraph.render_args[:partial] = paragraph.render_args.delete(:rjs)
paragraph.render_args[:locals] ||= {}
paragraph.render_args[:locals][:renderer] = paragraph.rnd unless paragraph.render_args[:locals][:renderer]
@paragraph = paragraph
str = render_to_string(paragraph.render_args)
str = "<script type='text/javascript'>" + str + "</script>" unless opts[:ajax]
str
elsif paragraph.render_args[:parent_rjs]
paragraph.render_args[:partial] = paragraph.render_args.delete(:parent_rjs)
paragraph.render_args[:locals] ||= {}
paragraph.render_args[:locals][:renderer] = paragraph.rnd unless paragraph.render_args[:locals][:renderer]
@paragraph = paragraph
script = render_to_string(paragraph.render_args)
script = (script || '').
gsub('\\', '\\\\\\').
gsub(/\r\n|\r|\n/, '\\n').
gsub(/['"]/, '\\\\\&').
gsub('</script>','</scr"+"ipt>')
str = <<-EOF
<script type='text/javascript'>
with(window.parent) { setTimeout(function() { window.eval('#{script}'); loc.replace('about:blank'); }, 1) }
</script>
EOF
else
raise 'Invalid Paragraph Rendering'
end
# Add on the Site Feature CSS only if we're in edit mode, otherwise it'll come in on an include
if opts[:edit]
str = strip_script_tags(str)
raise str.inspect if str.include?('<script')
end
str = "<style>#{css}</style>" + str if opts[:edit] && css
return str
elsif cls_name == "ParagraphRenderer::CachedOutput"
return paragraph.output
elsif paragraph.is_a?(String)
opts[:edit] ? strip_script_tags(paragraph) : paragraph
elsif paragraph.nil?
"Nil"
""
else
""
end
end
def webiva_post_process_paragraph(txt) #:nodoc:
@post_process_form_token_str ||= "<input name='authenticity_token' type='hidden' value='#{ form_authenticity_token.to_s}' />"
txt.gsub!("<CMS:AUTHENTICITY_TOKEN/>",@post_process_form_token_str)
txt
end
# Make sure any including controller
# can render paragraphs from templates
def self.append_features(base) #:nodoc:
super
base.helper_method :compile_paragraph, :render_paragraph, :strip_script_tags
base.helper_method :webiva_post_process_paragraph, :render_output
base.hide_action :find_page_from_path
base.hide_action :handle_document_node
base.hide_action :render_paragraph
base.hide_action :compile_paragraph
base.hide_action :render_output
base.hide_action :webiva_post_process_paragraph
end
# Renders the output object of SiteNodeEngine given the page SiteNode
# and the SiteNodeEngine::PageOutput object
def render_output(page,output_obj)
raise "Not a PageOutput" unless output_obj.is_a?(PageOutput)
output = ''
output_obj.html do |blk|
if blk.is_a?(String)
output += webiva_post_process_paragraph(blk)
elsif blk.is_a?(Hash)
blk[:paragraphs].each do |para|
if para.is_a?(String)
output += webiva_post_process_paragraph(para)
else
para_id = para.is_a?(ParagraphRenderer::ParagraphOutput) ? "id='cmspara_#{para.rnd.paragraph.id}'" : ""
output+= "<div class='paragraph' #{para_id}>#{webiva_post_process_paragraph(render_paragraph page, output_obj.revision, para)}</div>"
end
end
end
end
output
end
end
# Base class for all different SiteNodeEngine outputs
class Output
attr_accessor :status
attr_accessor :paction
attr_accessor :title
def initialize
@head = []
end
def redirect?
false
end
def document?
false
end
def page?
false
end
end
# An instance of this is returned fron SiteNodeEngine#run if the
# running the SiteNode results in a redirect
class RedirectOutput < Output #:nodoc:all
attr_accessor :redirect
def redirect?
true
end
end
# An instance of this is returned fron SiteNodeEngine#run if the
# running the SiteNode results in a HTML page
class PageOutput < Output #:nodoc:all
attr_accessor :language
attr_accessor :title
attr_accessor :site_template_id
attr_accessor :css_site_template_id
attr_accessor :head
attr_accessor :body
attr_accessor :revision
attr_accessor :includes
attr_accessor :page_connections
attr_accessor :meta_description
attr_accessor :meta_keywords
attr_accessor :content_nodes
def initialize
super
@includes = {}
end
def html_set_attribute(part, value={})
@includes[part.to_sym] ||= {}
@includes[part.to_sym].merge! value
end
def html_include(part, value=[])
@includes[part.to_sym] ||= []
value = [value] unless value.is_a?(Array)
@includes[part.to_sym] += value
end
def page?
true
end
def html(&block)
self.body.each do |bd|
yield bd
end
end
end
# An instance of this is returned fron SiteNodeEngine#run if the
# running the SiteNode results in a date output or a file output
class DocumentOutput < Output
def initialize(options=nil)
@options= options || {}
end
attr_reader :options
def file?
@options.has_key?(:file)
end
def file
@options[:file]
end
def domain_file?
@options.has_key?(:domain_file)
end
def data?
@options.has_key?(:data)
end
def domain_file
@options[:domain_file]
end
def path_args
@options[:path_args]
end
def language
@options[:language]
end
def data
@options[:data]
end
def text?
@options.has_key?(:text)
end
def text
@options[:text]
end
def document?
true
end
end
# Exception that is raised when the site node doesn't
# have an active version to display
class NoActiveVersionException < RuntimeError
attr :container
attr :language
def initialize(container,language)
@container = container
@language = language
end
end
# Exception that is raised if there's not page matching
# the path passed in
class MissingPageException < RuntimeError
attr :container
attr :language
def initialize(container,language)
@container = container
@language = language
end
end
attr_reader :container
attr_accessor :revision, :mode, :active_template, :language, :user, :path_args, :page_information
attr_reader :controller
# To create a SiteNodeEngine, you need to pass it a container (usually a SiteNode, but
# could also be a framework PageModifier in the editor)
#
# Options:
# [:edit]
# Set to true if we are in the editor
# [:capture]
# Set to true to capture the output of the renderer instead of actually rendering a feature
# [:display]
# The language to display if possible
def initialize(container,options = {} )
@container = container
@capture_data = options[:capture]
@path_args = options[:path] || []
if options[:edit]
@mode = 'edit'
# This is for editing
if options[:edit].is_a?(PageRevision)
@revision = options[:edit]
else
@revision = @container.page_revisions.find_by_id(options[:edit])
end
@language = @revision.language if @revision
else
@mode = 'display'
# Otherwise this is for display
# if we have a cached copy
# create @page_information array
@language = options[:display]
@language ||= ( Locale.language ? Locale.language.code : Configuration.languages[0] )
end
end
# Run an individual paragraph (instead of the entire container)
def run_paragraph(paragraph,controller,user,options = {})
nd = generate_page_information(controller,user)
if !paragraph.info[:ajax] && (@mode != 'edit')
raise 'Not an ajax paragraph'
end
@result = controller.compile_paragraph(@container.is_a?(SiteNode) ? @container : @container.site_node,
@page_information.revision,
paragraph,
:ajax => @mode=='edit' ? false : true,
:editor => @mode == 'edit',
:language => @language,
:capture => @capture_data
)
result_cls = @result.class.to_s
if result_cls == "ParagraphRenderer::ParagraphOutput"
return @result
else
return nil
end
end
# Run the site node engine, given an ActiveController and a user
# this will compile all paragraph and return the appropriate subclass of
# SiteNodeEngine::Output
def run(controller,user)
nd = generate_page_information(controller,user)
@handlers = generate_node_engine_handlers()
@handlers.each do |handler|
@output = handler.before_page
if @output.is_a?(SiteNodeEngine::Output)
return @output
end
end
# Check for a redirect node or a page information redirect
if @output = handle_redirected_output(nd)
return @output
end
# If we are rendering a document node, return it
if @container.is_a?(SiteNode) && @container.node_type=='D'
doc = DocumentOutput.new(:path_args => @path_args, :language => @language )
doc.paction = user.action("/document/download",:identifier => @container.domain_file.file_path)
return doc
end
# Precompile any remaining paragraphs (Ignore when in edit mode)
# This is done so any action can get handled ahead of time and redirects are handled properly
if @mode != 'edit'
# See how many page argument the page paragraphs are expecting
max_path_level = calculate_max_path_level
if max_path_level < @path_args.length && @container.node_type != 'M'
raise MissingPageException.new(@container,@language), "Page Not Found"
end
page_connections = {}
loop_cnt = 0
unrendered = -1
last_unrendered = 0
repeat_count = 0
first = true
while first || ( unrendered > 0 && repeat_count < 3)
first = false
if last_unrendered == unrendered
repeat_count += 1
else
repeat_count = 0
end
last_unrendered = unrendered
loop_cnt += 1
unrendered = 0
@page_information.render_elements.each do |part|
if part.is_a?(Hash)
(0..part[:paragraphs].length).each do |idx|
if part[:paragraphs][idx].is_a?(Hash) || part[:paragraphs][idx].is_a?(PageParagraph)
part[:paragraphs][idx] = PageParagraph.thaw(part[:paragraphs][idx]) if part[:paragraphs][idx].is_a?(Hash)
# Title string is special because any paragraph could
# generate one, so only if we're stuck and unrendered
# to we need to generated one
if page_connections[:title_str].blank? && repeat_count > 1
page_connections[:title_str] = @page_information[:title_str] = create_page_title(nd)
page_connections[:title] = @page_information[:title]
end
result = controller.compile_paragraph(@container.is_a?(SiteNode) ?
@container : @container.site_node,
@page_information.revision,
part[:paragraphs][idx],
:page_path => @page_path,
:language => @language,
:connections => page_connections,
:capture => @capture_data,
:repeat_count => repeat_count)
# We may not have a result if the page connections
# aren't fullfilled yet
if result
if result.is_a?(ParagraphRenderer::ParagraphRedirect)
@output = RedirectOutput.new
@output.status = result.args.delete(:status) if result.args.is_a?(Hash)
@output.paction = result.paction if result.paction
@output.redirect = result.args
return @output # exit early if we can
elsif result.is_a?(ParagraphRenderer::ParagraphData)
# if we just have data, we need to do a direct output
return DocumentOutput.new(result.render_args)
end
if result.is_a?(ParagraphRenderer::ParagraphOutput)
page_connections.merge!(result.page_connections || {})
# Get any remaining includes
result.includes.each do |inc_type,inc_value|
if inc_value.is_a?(Hash)
@page_information[:includes][inc_type] ||= {}
@page_information[:includes][inc_type].merge! inc_value
else
@page_information[:includes][inc_type] ||= []
@page_information[:includes][inc_type] += inc_value
end
end
if result.content_nodes
@page_information[:content_nodes] ||= []
@page_information[:content_nodes] += result.content_nodes
end
@page_information[:paction] = result.paction if result.paction
end
@page_information[:title].merge!(result.page_title) if result.page_title
part[:paragraphs][idx] = result
else
unrendered += 1
end
end
end
end
end
end
if unrendered > 0
#raise page_connections.inspect + ': Unrendered Paragraphs'
end
end
# Finally, it we made it here, lets output a page
@output = PageOutput.new
@output.revision = @page_information.revision
@output.status = '200'
@output.language = @language
# Create the title if not generated in compile phase
@page_information[:title_str] = create_page_title(nd) if @page_information.title_str.blank?
@output.title = @page_information.title_str
@output.meta_keywords = @page_information.revision.meta_keywords.blank? ? nil : @page_information.revision.meta_keywords
@output.meta_description = @page_information.revision.meta_description.blank? ? nil : @page_information.revision.meta_description
@output.site_template_id = @page_information.site_template_id
@output.css_site_template_id = @page_information.css_site_template_id
@output.body = @page_information.render_elements
@output.includes = @page_information.includes
@output.includes[:html_tag] ||= {}
@output.includes[:html_tag]['xml:lang'] = @output.language
@output.includes[:html_tag]['lang'] = @output.language
@output.head = @page_information.head
@output.paction = @page_information.paction
@output.content_nodes = @page_information.content_nodes
@output
end
protected
def handle_redirected_output(nd) #:nodoc:
if nd.node_type == 'J'
@output = RedirectOutput.new
@output.status = 'Redirect'
@output.redirect = nd.redirect_detail.destination
return @output
# If we have a page level redirect,
#
elsif @mode != 'edit' && @page_information.redirect_to
@output = RedirectOutput.new
@output.status = 'Redirect'
@output.redirect = @page_information.redirect_to
return @output
end
return nil
end
def generate_node_engine_handlers #:nodoc:
@node_engine_handlers ||= [ NodeEngine::BuiltinHandler.new(self) ]
end
# Create the @page_information object - which contains all the info
# necessary to render the object
def generate_page_information(controller,user)
@controller = controller
@user = user
@cached_info = false
handlers = generate_node_engine_handlers
@page_context = {}
# Add in context from other handlers
handlers.each { |hndler| hndler.add_context(@page_context) }
@page_context.stringify_keys!
# If we are viewing (not editing) and
if(@mode != 'edit' && CMS_CACHE_ACTIVE)
# see if this container is in the cache
sorted_context_arr = @page_context.to_a.sort { |elm1,elm2| elm1[0] <=> elm2[0] }
@path_hash_info =Digest::SHA1.hexdigest(sorted_context_arr.to_sentence)
@page_information = DataCache.get_container(@container,@path_hash_info)
# If so we do, turn it into a propert page information object
if @page_information
@page_information = NodeEngine::PageInformation.new(@page_information)
@cached_info = true
end
end
nd = @container.is_a?(SiteNode) ? @container : @container.site_node
@page_path = nd.node_path
if @path_args.length > 0
@page_path += "/" + @path_args.join("/")
end
# Unless we already have a cache of page information
unless @page_information || nd.node_type == 'J'
compile_page_information(user)
end
if nd.node_type == 'J'
@page_information = NodeEngine::PageInformation.new
end
# Cache the page information, if it's not already cached and we do have a cache active
# (Ignore when in edit mode)
if !@cached_info && @mode != 'edit' && CMS_CACHE_ACTIVE
DataCache.put_container(@container,@path_hash_info,@page_information.to_hash)
end
return nd
end
# Compile the page information if we don't already have it in the cache
def compile_page_information(user)
module_rendering = @container.is_a?(SiteNode) && @container.node_type=='M'
document_rendering = @container.is_a?(SiteNode) && @container.node_type=='D'
@page_information = NodeEngine::PageInformation.new
@page_information[:context] = @page_context.clone
# Handle Modules
if module_rendering
# We need our revision from a framework
dispatcher = @container.dispatcher
paragraphs = dispatcher.run(@path_args)
@revision = @container.page_revisions.new(:revision_type => 'real', :language => @language)
elsif document_rendering
paragraphs= []
else
# Handle Pages / Frameworks
unless @revision
@revision = @container.page_revisions.find(:first,
:conditions => "revision_type = 'real' AND active=1",
:order => "language=#{SiteNode.quote_value(@language)} DESC"
)
end
raise NoActiveVersionException.new(@container,@language), "No valid version found" unless @revision
paragraphs = @revision.page_paragraphs.to_a
end
if @container.is_a?(SiteNode)
site_node = @container
nd = @container
else
site_node = @container.site_node
nd = @container.site_node
end
# Handle case where we have no paragraphs
if paragraphs.length == 0 && @mode != 'edit'
replacement_page_path = nil
site_node.children.each do |page|
if page.node_type == 'J'
replacement_page_path = page.redirect_detail.destination
break;
else
rev = page.page_revisions.find(:first,
:conditions => "revision_type = 'real' AND active=1",
:order => "language=#{SiteNode.quote_value(@language)} DESC"
)
if rev && rev.page_paragraphs.length > 0
replacement_page_path = page.node_path
break;
end
end
end
if replacement_page_path
@page_information[:redirect_to] = replacement_page_path
return
end
end
paragraphs.reverse_each do |para|
@page_information.zone_paragraphs[para.zone_idx] ||= []
# Make sure we don't add extra framework paragraphs
# If we have a clear
if(para.display_type == 'clear' && !@page_information.zone_clears[para.zone_idx])
@page_information.zone_clears[para.zone_idx] = @revision.id
@page_information.zone_locks[para.zone_idx] = false
end
@page_information.zone_paragraphs[para.zone_idx].unshift(para)
end
while nd
mods = nd.modifiers(nd == site_node,@container.is_a?(SiteNode) ? nil : @container.position) # get the modifiers (only the pre ones if this is the node we are looking at)
self.add_modifiers(mods)
nd = nd.parent
end
unless document_rendering
if !@page_information.site_template_id
@active_template = SiteTemplate.find(:first) || SiteTemplate.create(:name => 'Default Template'.t,:template_html => "<cms:zone name='Main'/>")
@page_information[:site_template_id] = @active_template.id
end
@page_information[:css_site_template_id] = @active_template.parent_id ? @active_template.parent_id : @active_template.id
@page_information[:head] = SiteTemplate.render_template_head(@page_information[:css_site_template_id],@language)
if @mode == 'edit'
@page_information[:revision] = @revision
else
# Just save the attributes unless we are editing
@page_information[:revision] = DefaultsHashObject.new(@revision.attributes)
end
self.render_page_elements()
end
end
# Get a the page_information render_elements - an array of static
# content and paragraph attribute hashes
def render_page_elements()
@active_template = SiteTemplate.find(@page_information.site_template_id) unless @active_template
variables = @revision.variables || {}
# Generate an array of strings (for static content),
# and hashs (for zones with paragraphs)
parts = self.generate_template_output(variables)
# List of javascript and CSS includes
includes = {}
# Compile the parts together where possible for speedier rendering
if @mode != 'edit'
compiled_parts = [""] # start off with a blank string
parts.each do |part|
if part.is_a?(String)
if compiled_parts[-1].is_a?(String)
compiled_parts[-1] += part
else
compiled_parts << part
end
elsif part[:paragraphs]
part[:paragraphs].each do |para|
# Pre process the paragraph for storing in page information
pre_process_paragraph(compiled_parts,para,part[:zone_idx],includes)
end
else # we have a variable
value = @active_template.render_variable(part[:variable],part[:value],@language)
if compiled_parts[-1].is_a?(String)