Class: Orange::Parser

Inherits:
Resource show all
Defined in:
lib/orange-core/resources/parser.rb

Instance Method Summary collapse

Methods inherited from Resource

call_me, #do_view, #find_extras, #init, #initialize, #nests, #options, #orange, #orange_name, #routable, #set_orange, set_orange, #view, #view_opts, #viewable, viewable

Methods included from ClassInheritableAttributes

#cattr_accessor, #cattr_reader, #cattr_writer, eval_in_accessor_module, fetch_value, store_value

Constructor Details

This class inherits a constructor from Orange::Resource

Instance Method Details

#afterLoadObject



8
9
10
11
12
13
14
# File 'lib/orange-core/resources/parser.rb', line 8

def afterLoad
  orange.add_pulp Orange::Pulp::ParserPulp
  @template_dirs = [File.join(orange.core_dir, 'templates')]
  @view_dirs = [File.join(orange.core_dir, 'views')]
  orange.plugins.each{|p| @template_dirs << p.templates if p.has_templates? }
  orange.plugins.each{|p| @view_dirs << p.views if p.has_views? }
end

#haml(file, packet_binding, *vars, &block) ⇒ Object



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
# File 'lib/orange-core/resources/parser.rb', line 23

def haml(file, packet_binding, *vars, &block)
  
  opts = vars.extract_options!
  # Initial info
  temp = opts.delete(:template)
  opts[:resource_name] = opts[:resource].orange_name.to_s if 
      opts[:resource] && opts[:resource].respond_to?(:orange_name)
  resource = (opts[:resource_name] || '').downcase
  
  opts.merge :orange => orange
  if packet_binding.is_a? Orange::Packet
    context = packet_binding['route.context'].to_s
    unless temp
      packet_binding['parser.haml-templates'] ||= {}
      haml_engine = packet_binding['parser.haml-templates']["#{context}-#{resource}-#{file}"] || false
    end
  end
  unless haml_engine
  
    string = false
    if temp
      string ||= read_if_exists('templates', file) 
      @template_dirs.reverse_each do |templates_dir|
        string ||= read_if_exists(templates_dir, file)
      end unless string
    end
  
    if context
      #Check for context specific overrides
      string ||= read_if_exists('views', resource, context+"."+file) if resource
      string ||= read_if_exists('views', context+"."+file) unless resource
      @view_dirs.reverse_each do |views_dir|
        string ||= read_if_exists(views_dir, resource, context+"."+file) if resource
        string ||= read_if_exists(views_dir, context+"."+file) unless resource
      end unless string
    end
  
    # Check for standard views
    string ||= read_if_exists('views', resource, file) if resource
    string ||= read_if_exists('views', file) unless resource
    @view_dirs.reverse_each do |views_dir|
      string ||= read_if_exists(views_dir, resource, file) if resource
      string ||= read_if_exists(views_dir, file) unless resource
    end unless string
  
    # Check for default resource views
    string ||= read_if_exists('views', 'default_resource', context+"."+file) if context
    string ||= read_if_exists('views', 'default_resource', file)
    @view_dirs.reverse_each do |views_dir|
      string ||= read_if_exists(views_dir, 'default_resource', context+"."+file) if context
      string ||= read_if_exists(views_dir, 'default_resource', file)
    end unless string
    raise LoadError, "Couldn't find haml file '#{file}'" unless string
    
    haml_engine = Haml::Engine.new(string)
    if packet_binding.is_a? Orange::Packet
      packet_binding['parser.haml-templates', {}]["#{context}-#{resource}-#{file}"] = haml_engine
    end
  end
  opts[:opts] = opts.dup
  out = haml_engine.render(packet_binding, opts, &block)
end

#hpricot(text) ⇒ Object



91
92
93
94
# File 'lib/orange-core/resources/parser.rb', line 91

def hpricot(text)
  require 'hpricot'
  Hpricot(text)
end

#json(text) ⇒ Object



100
101
102
# File 'lib/orange-core/resources/parser.rb', line 100

def json(text)
  Crack::JSON.parse(text)
end

#read_if_exists(*args) ⇒ Object



86
87
88
89
# File 'lib/orange-core/resources/parser.rb', line 86

def read_if_exists(*args)
  return File.read(File.join(*args)) if File.exists?(File.join(*args))
  false
end

#xml(text) ⇒ Object



96
97
98
# File 'lib/orange-core/resources/parser.rb', line 96

def xml(text)
  Crack::XML.parse(text)
end

#yaml(file) ⇒ Object



16
17
18
19
20
21
# File 'lib/orange-core/resources/parser.rb', line 16

def yaml(file)
  return nil unless File.exists?(file)
  string = File.read(file) 
  string.gsub!('__ORANGE__', orange.app_dir)
  out = YAML::load(string)
end