Class: HtmlMockup::Resolver
- Inherits:
-
Object
- Object
- HtmlMockup::Resolver
- Defined in:
- lib/html_mockup/resolver.rb
Instance Method Summary collapse
- #find_template(url, exact_match = false) ⇒ Object (also: #url_to_path)
-
#initialize(path) ⇒ Resolver
constructor
A new instance of Resolver.
-
#path_to_url(path, relative_to = nil) ⇒ Object
Convert a disk path on file to an url.
- #strip_query_string_and_anchor(url) ⇒ Object
- #url_to_relative_url(url, relative_to_path) ⇒ Object
Constructor Details
#initialize(path) ⇒ Resolver
Returns a new instance of Resolver.
4 5 6 7 |
# File 'lib/html_mockup/resolver.rb', line 4 def initialize(path) raise ArgumentError, "Resolver base path can't be nil" if path.nil? @base = Pathname.new(path) end |
Instance Method Details
#find_template(url, exact_match = false) ⇒ Object Also known as: url_to_path
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/html_mockup/resolver.rb', line 11 def find_template(url, exact_match = false) path, qs, anch = strip_query_string_and_anchor(url.to_s) path = File.join(@base, path) if 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 .html,we strip of the extension if path =~ /\.html\Z/ path.sub!(/\.html\Z/, "") end extensions = Tilt.mappings.keys + Tilt.mappings.keys.map{|ext| "html.#{ext}"} if found_extension = extensions.find { |ext| File.exist?(path + "." + ext) } Pathname.new(path + "." + found_extension) end end |
#path_to_url(path, relative_to = nil) ⇒ Object
Convert a disk path on file to an url
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/html_mockup/resolver.rb', line 40 def path_to_url(path, relative_to = nil) path = Pathname.new(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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/html_mockup/resolver.rb', line 75 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/html_mockup/resolver.rb', line 58 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, true) path = self.path_to_url(true_path, relative_to_path) path += qs if qs path += anch if anch path else false end end |