Class: MasterView::Parser

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

Class Method Summary collapse

Class Method Details

.escape_erb(html) ⇒ Object



578
579
580
581
582
# File 'lib/masterview/parser.rb', line 578

def self.escape_erb(html)
  html = html.gsub(/<%/, InlineErbStart)
  html.gsub!(/%>/, InlineErbEnd)
  html
end

.parse(template, options = DefaultParserOptions.clone) ⇒ 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 output (:output_dir, :namespace, :serializer)



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/masterview/parser.rb', line 545

def self.parse( template, options = DefaultParserOptions.clone)
  begin
  if options[:tidy]
    template = self.tidy(template)
  elsif options[:escape_erb]
    template = self.escape_erb(template)
  end
  parser = REXML::Parsers::SAX2Parser.new( template )
  parser.listen( MasterViewListener.new(options) )
  parser.parse
  rescue Exception => e
    if RescueExceptions
      Log.error { "Failure to parse template. Exception="+e }
      Log.debug { e.backtrace.join("\n") }
    else
      raise
    end
  end
end

.parse_file(template_file_path, output_dir, options = DefaultParserOptions.clone) ⇒ Object

parse a MasterView template by first reading from file and render output. template_file_path param is file path to template options are the optional parameters which control the output (:output_dir, :namespace)



533
534
535
536
537
538
539
540
# File 'lib/masterview/parser.rb', line 533

def self.parse_file( template_file_path, output_dir, options = DefaultParserOptions.clone)
  Log.debug { "Parsing file=#{File.expand_path(template_file_path)} output_dir=#{File.expand_path(output_dir)}" }
  options[:template_path]=File.expand_path(template_file_path)
  options[:output_dir] = output_dir
  template = File.new( template_file_path )
  template = template.readlines.join if options[:tidy] || options[:escape_erb]
  self.parse( template, options )
end

.tidy(html) ⇒ Object



565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/masterview/parser.rb', line 565

def self.tidy(html)
  Tidy.path = TidyPath unless Tidy.path
  xml = Tidy.open do |tidy|
    tidy.options.output_xml = true
    tidy.options.indent = true
    tidy.options.wrap = 0
    xml = tidy.clean(html)
  end
  xml = self.escape_erb(xml)
  Log.debug { 'tidy corrected xml='+xml }
  xml
end