Class: MasterView::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/masterview/parser.rb

Overview

The Parser processes a template document containing MasterView directives markup.

Processing options can be specified to control pre-processing of the template (e.g., :tidy to run HTML Tidy) as well as the operation of the parse operation.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(template, options = {}) ⇒ Object

Parse a MasterView template and render output.

template param is actual template source passed in as string or array. options are the optional parameters which control the template parsing (:tidy) and rendering output (:output_mio_tree, :namespace, :serializer)



683
684
685
686
# File 'lib/masterview/parser.rb', line 683

def self.parse( template, options={} )
  mv_parser = self.new
  mv_parser.process(template, options)
end

.parse_mio(template_mio, output_mio_tree, options = {}) ⇒ Object

Parse a template document contained in an IO source.



669
670
671
672
673
674
675
# File 'lib/masterview/parser.rb', line 669

def self.parse_mio( template_mio, output_mio_tree, options={} )
  options = options.clone()  # don't munge the client's copy of this
  options[:template_pathname] = template_mio.pathname
  options[:output_mio_tree] = output_mio_tree
  template = template_mio.read
  self.parse( template, options )
end

Instance Method Details

#ensure_standard_options_configured(options) ⇒ Object

ensure that default policies are configured



715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/masterview/parser.rb', line 715

def ensure_standard_options_configured(options)  #:nodoc:

  # install the standard processing mechanisms unless overridden by client
  options[:directive_load_path] = DirectiveLoadPath.current unless options.include?(:directive_load_path)
  options[:listeners] ||= [ TemplateProcessing::SAXParserListener ]
  options[:rescue_exceptions] = RescueExceptions unless options.include?(:rescue_exceptions)

  DefaultParserOptions.each_pair {|key, value|
    options[key] = value unless options.include?(key)
  }

end

#process(template, options) ⇒ Object

process the template



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/masterview/parser.rb', line 689

def process(template, options)

  options = options.clone()  # don't munge the client's copy of this
  ensure_standard_options_configured(options)

  begin
    sax_parser = MasterView::REXMLSax2ParserClass.new( template )
    options[:listeners].each do |listener|
      if listener.is_a? Class
        sax_parser.listen( listener.new(options) )
      else
        sax_parser.listen(listener)
      end
    end
    sax_parser.parse
  rescue Exception => ex
    if options[:rescue_exceptions]
      Log.error { "Failure to parse template. Exception="+ex }
      Log.debug { ex.backtrace.join("\n") }
    else
      raise
    end
  end
end