Class: HtmlMockup::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/html_mockup/resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ Resolver

Returns a new instance of Resolver.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# File 'lib/html_mockup/resolver.rb', line 6

def initialize(paths)
  raise ArgumentError, "Resolver base path can't be nil" if paths.nil?

  # Convert to paths
  @load_paths = [paths].flatten.map{|p| Pathname.new(p) }
end

Instance Attribute Details

#load_pathsObject (readonly)

Returns the value of attribute load_paths.



4
5
6
# File 'lib/html_mockup/resolver.rb', line 4

def load_paths
  @load_paths
end

Instance Method Details

#find_template(url, options = {}) ⇒ Object Also known as: url_to_path

Parameters:

  • url (String)

    The url to resolve to a path

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :exact_match (true, false)

    Wether or not to match exact paths, this is mainly used in the path_to_url method to match .js, .css, etc files.

  • :preferred_extension (String)

    The part to chop off and re-add to search for more complex double-extensions. (Makes it possible to have context aware partials)



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
# File 'lib/html_mockup/resolver.rb', line 18

def find_template(url, options = {})
  orig_path, qs, anch = strip_query_string_and_anchor(url.to_s)

  options = {
    :exact_match => false,
    :preferred_extension => "html"  
  }.update(options)

  paths = self.load_paths.map{|base| File.join(base, orig_path) }
  
  paths.find do |path|
    if options[:exact_match] && File.exist?(path)
      return Pathname.new(path)
    end      
    
    # It's a directory, add "/index"
    if File.directory?(path)
      path = File.join(path, "index")
    end
    
    # 2. If it's preferred_extension, we strip of the extension
    if path =~ /\.#{options[:preferred_extension]}\Z/
      path.sub!(/\.#{options[:preferred_extension]}\Z/, "")
    end
    
    extensions = Tilt.default_mapping.template_map.keys + Tilt.default_mapping.lazy_map.keys

    # We have to re-add preferred_extension again as we have stripped it in in step 2!
    extensions += extensions.map{|ext| "#{options[:preferred_extension]}.#{ext}"}

    if found_extension = extensions.find { |ext| File.exist?(path + "." + ext) }
      return Pathname.new(path + "." + found_extension)
    end

    false #Next iteration
  end
end

#path_to_url(path, relative_to = nil) ⇒ Object

Convert a disk path on file to an url



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/html_mockup/resolver.rb', line 59

def path_to_url(path, relative_to = nil)

  # Find the parent path we're in
  path = Pathname.new(path).realpath
  base = self.load_paths.find{|lp| path.to_s =~ /\A#{Regexp.escape(lp.realpath.to_s)}/ }

  path = path.relative_path_from(base).cleanpath
  
  if relative_to
    if relative_to.to_s =~ /\A\//
      relative_to = Pathname.new(File.dirname(relative_to.to_s)).relative_path_from(base).cleanpath
    else
      relative_to = Pathname.new(File.dirname(relative_to.to_s))
    end
    path = Pathname.new("/" + path.to_s).relative_path_from(Pathname.new("/" + relative_to.to_s))
    path.to_s
  else
    "/" + path.to_s
  end
  
end

#strip_query_string_and_anchor(url) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/html_mockup/resolver.rb', line 98

def strip_query_string_and_anchor(url)
  url = url.dup
  
  # Strip off anchors
  anchor = nil
  url.gsub!(/(#.+)\Z/) do |r|
    anchor = r
    ""
  end
          
  # Strip off query strings
  query = nil
  url.gsub!(/(\?.+)\Z/) do |r|
    query = r
    ""
  end
  
  [url, query, anchor]
end

#url_to_relative_url(url, relative_to_path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/html_mockup/resolver.rb', line 81

def url_to_relative_url(url, relative_to_path)
  # Skip if the url doesn't start with a / (but not with //)
  return false unless url =~ /\A\/[^\/]/
  
  path, qs, anch = strip_query_string_and_anchor(url)

  # Get disk path
  if true_path =  self.url_to_path(path, :exact_match => true)
    path = self.path_to_url(true_path, relative_to_path)
    path += qs if qs
    path += anch if anch
    path
  else
    false
  end
end