Class: MasterView::MasterViewListener

Inherits:
Object
  • Object
show all
Includes:
DirectiveHelpers, REXML::SAX2Listener
Defined in:
lib/masterview/parser.rb

Constant Summary

Constants included from DirectiveHelpers

DirectiveHelpers::CRLF, DirectiveHelpers::ERB_CONTENT_END, DirectiveHelpers::ERB_CONTENT_START, DirectiveHelpers::ERB_END, DirectiveHelpers::ERB_EVAL, DirectiveHelpers::ERB_EVAL_END, DirectiveHelpers::ERB_EVAL_START, DirectiveHelpers::ERB_START

Instance Method Summary collapse

Methods included from DirectiveHelpers

#delete_last_in_parent, #find_last_in_parent, #find_string_val_in_string_hash, #lowercase_attribute_keys, #lowercase_attribute_keys_and_values, #merge_into_embedded_hash, #parse, #parse_eval_into_array, #parse_eval_into_hash, #remove_prepended_strings, #render_partial_name_to_file_name, #symbolize_sort_and_serialize_hash_to_str

Constructor Details

#initialize(options = {}) ⇒ MasterViewListener

Returns a new instance of MasterViewListener.



406
407
408
# File 'lib/masterview/parser.rb', line 406

def initialize( options = {} )
  @renderer = Renderer.new(options)
end

Instance Method Details

#cdata(content) ⇒ Object



436
437
438
# File 'lib/masterview/parser.rb', line 436

def cdata(content)
  @renderer.append_content(:cdata, content)
end

#characters(text) ⇒ Object



428
429
430
# File 'lib/masterview/parser.rb', line 428

def characters(text)
  @renderer.append_content(:characters, text)
end

#comment(comment) ⇒ Object



432
433
434
# File 'lib/masterview/parser.rb', line 432

def comment(comment)
  @renderer.append_content(:comment, comment)
end

#doctype(name, pub, sys, long_name, uri) ⇒ Object



418
419
420
# File 'lib/masterview/parser.rb', line 418

def doctype(name, pub, sys, long_name, uri)
  #todo
end

#end_documentObject



444
445
446
# File 'lib/masterview/parser.rb', line 444

def end_document
  #todo
end

#end_element(uri, localname, qname) ⇒ Object



440
441
442
# File 'lib/masterview/parser.rb', line 440

def end_element(uri, localname, qname)
  @renderer.pop_tag
end

#generate_replace(value) ⇒ Object



455
456
457
# File 'lib/masterview/parser.rb', line 455

def generate_replace(value)
  @renderer.append_raw ERB_EVAL+value+ERB_END
end

#handle_gen_partial(attributes) ⇒ Object

handle a mv:gen_partial attribute, which calls generate and outputs a token it takes an optional :dir => 'foo/bar' which is prepended to partial path, otherwise it just uses what is in partial. This creates a generate attribute value which will be used later. Parameters value = attribute value for gen_partial attributes = all remaining attributes hash



466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/masterview/parser.rb', line 466

def handle_gen_partial(attributes)
  value = @renderer.keyword_expander.resolveAttrAndDelete(attributes, @renderer.mv_ns+'gen_partial')
  if value 
    prepend_dir = find_string_val_in_string_hash(value, :dir) #only used for masterview
    partial = find_string_val_in_string_hash(value, :partial) 
    return if partial.nil?
    path = render_partial_name_to_file_name(partial, @renderer.default_extension)
    path = File.join(prepend_dir, path) if prepend_dir
    generate_attribute = attributes[@renderer.mv_ns+'generate'] || '' # check if we need to add to existing generate
    generate_attribute = path + (generate_attribute.blank? ? '' : ', '+generate_attribute)
    attributes[@renderer.mv_ns+'generate'] = generate_attribute 
    @renderer.append_raw( ERB_EVAL+'render( '+value+' )'+ERB_END ) 
  end
end

#push_levels(attributes) ⇒ Object



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
# File 'lib/masterview/parser.rb', line 481

def push_levels(attributes)
  handle_gen_partial(attributes)
  
  gen_replace = @renderer.keyword_expander.resolveAttrAndDelete(attributes, @renderer.mv_ns+'gen_replace') #get and delete from map
  generate_replace( gen_replace ) unless gen_replace.nil?

  gen = @renderer.keyword_expander.resolveAttrAndDelete(attributes, @renderer.mv_ns+'generate') #get and delete from map
  if gen
    omit_comment = @renderer.options[:omit_comment] || OmitGeneratedComments || File.extname(gen) != MasterView::IOMgr.erb.default_extension
    attributes[@renderer.mv_ns+'insert_generated_comment'] =  @renderer.template_full_pathname.to_s unless omit_comment #add the comment directive, so it will be written to each gen'd file
    render_level = nil
    gen_values = parse_eval_into_hash(gen, :normal)

    #Log.debug { 'generate_hash='+gen_values.inspect }

    gen_values.each do |key,value|
      mode_type = key.to_sym
      arr_values = (value.is_a?(Enumerable)) ? value : [value] #if not enumerable add it to array
      value.each do |path|
        path.strip!
        #Log.debug { ('pushing mode='+mode_type.to_s+' path='+path).indent(2*@renderer.render_levels.size) } 
        render_level ||= RenderLevel.new
        render_level.push RenderMode.new(path, mode_type)
      end
    end
    @renderer.push_level(render_level) unless render_level.nil?
  end

end

#start_documentObject



414
415
416
# File 'lib/masterview/parser.rb', line 414

def start_document
  #todo
end

#start_element(uri, localname, qname, attributes) ⇒ Object



422
423
424
425
426
# File 'lib/masterview/parser.rb', line 422

def start_element(uri, localname, qname, attributes)
  unescape_attributes!(attributes)
  push_levels(attributes)
  @renderer.push_tag(qname, attributes)
end

#unescape_attributes!(attributes) ⇒ Object



448
449
450
451
452
453
# File 'lib/masterview/parser.rb', line 448

def unescape_attributes!(attributes)
  attributes.each do |name, value|
    value.replace CGI::unescapeHTML(value)
    value.gsub!(''', '\'')
  end
end

#xmldecl(version, encoding, standalone) ⇒ Object



410
411
412
# File 'lib/masterview/parser.rb', line 410

def xmldecl(version, encoding, standalone)
  #todo
end