Class: Roger::Resolver
- Inherits:
-
Object
- Object
- Roger::Resolver
- Defined in:
- lib/roger/resolver.rb
Overview
The resolver is here to resolve urls to paths and sometimes vice-versa
Constant Summary collapse
- EXTENSION_MAP =
Maps output extensions to template extensions to find source files.
{ "html" => %w( rhtml markdown mkd md ad adoc asciidoc rdoc textile ), "csv" => %w( rcsv ), # These are generic template languages nil => %w( erb erubis str ) }.freeze
Instance Attribute Summary collapse
-
#load_paths ⇒ Object
readonly
Returns the value of attribute load_paths.
Instance Method Summary collapse
- #find_template(url, options = {}) ⇒ Object (also: #url_to_path)
-
#initialize(paths) ⇒ 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(paths) ⇒ Resolver
Returns a new instance of Resolver.
31 32 33 34 35 36 |
# File 'lib/roger/resolver.rb', line 31 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_paths ⇒ Object (readonly)
Returns the value of attribute load_paths.
29 30 31 |
# File 'lib/roger/resolver.rb', line 29 def load_paths @load_paths end |
Instance Method Details
#find_template(url, options = {}) ⇒ Object Also known as: url_to_path
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/roger/resolver.rb', line 44 def find_template(url, = {}) = { prefer: "html" }.update() orig_path, _qs, _anch = strip_query_string_and_anchor(url.to_s) output = nil load_paths.find do |load_path| path = File.join(load_path, orig_path) output = find_template_path(path, ) end output end |
#path_to_url(path, relative_to = nil) ⇒ Object
Convert a disk path on file to an url
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/roger/resolver.rb', line 63 def path_to_url(path, relative_to = nil) # Find the parent path we're in path = Pathname.new(path).realpath base = load_paths.find { |lp| path.to_s =~ /\A#{Regexp.escape(lp.realpath.to_s)}/ } path = path.relative_path_from(base).cleanpath if relative_to relative_path_to_url(path, relative_to, base).to_s else "/#{path}" end end |
#strip_query_string_and_anchor(url) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/roger/resolver.rb', line 94 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/roger/resolver.rb', line 77 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 =~ %r{\A/[^/]} path, qs, anch = strip_query_string_and_anchor(url) # Get disk path if true_path = url_to_path(path, exact_match: true) path = path_to_url(true_path, relative_to_path) path += qs if qs path += anch if anch path else false end end |