lib/yard/server/router.rb in yard-0.9.0 vs lib/yard/server/router.rb in yard-0.9.1
- old
+ new
@@ -6,12 +6,12 @@
# == Subclassing Notes
# To create a custom router, subclass this class and pass it into the adapter
# options through {Adapter#initialize} or by directly modifying {Adapter#router}.
#
# The most general customization is to change the URL prefixes recognized by
- # routing, which can be done by overriding {#docs_prefix}, {#list_prefix}
- # and {#search_prefix}.
+ # routing, which can be done by overriding {#docs_prefix}, {#list_prefix},
+ # {#static_prefix}, and {#search_prefix}.
#
# == Implementing Custom Caching
# By default, the Router class performs static disk-based caching on all
# requests through the +#check_static_cache+. To override this behaviour,
# or create your own caching mechanism, mixin your own custom module with
@@ -20,10 +20,11 @@
# @example Creating a subclassed router
# # Adds 'my' to all routing prefixes
# class MyRouter < YARD::Server::Router
# def docs_prefix; 'mydocs' end
# def list_prefix; 'mylist' end
+ # def static_prefix; 'mystatic' end
# def search_prefix; 'mysearch' end
# end
#
# # Using it:
# WebrickAdapter.new(libraries, :router => MyRouter).start
@@ -43,20 +44,20 @@
def initialize(adapter)
self.adapter = adapter
end
# Perform routing on a specific request, serving the request as a static
- # file through {Commands::StaticFileCommand} if no route is found.
+ # file through {Commands::RootRequestCommand} if no route is found.
#
# @param [Adapter Dependent] request the request object
# @return [Array(Numeric,Hash,Array)] the Rack-style server response data
def call(request)
self.request = request
if result = (check_static_cache || route)
result
else
- StaticFileCommand.new(adapter.options).call(request)
+ RootRequestCommand.new(adapter.options).call(request)
end
end
# @group Route Prefixes
@@ -67,10 +68,13 @@
def list_prefix; 'list' end
# @return [String] the URI prefix for all search requests
def search_prefix; 'search' end
+ # @return [String] the URI prefix for all static assets (templates)
+ def static_prefix; 'static' end
+
# @group Routing Methods
# @return [Array(LibraryVersion, Array<String>)] the library followed
# by the rest of the path components in the request path. LibraryVersion
# will be nil if no matching library was found.
@@ -99,19 +103,20 @@
# @return [nil] if no route is matched
def route(path = request.path)
path = path.gsub(%r{//+}, '/').gsub(%r{^/|/$}, '')
return route_index if path.empty? || path == docs_prefix
case path
- when /^(#{docs_prefix}|#{list_prefix}|#{search_prefix})(\/.*|$)/
+ when /^(#{docs_prefix}|#{list_prefix}|#{search_prefix}|#{static_prefix})(\/.*|$)/
prefix = $1
paths = $2.gsub(%r{^/|/$}, '').split('/')
library, paths = *parse_library_from_path(paths)
return unless library
return case prefix
when docs_prefix; route_docs(library, paths)
when list_prefix; route_list(library, paths)
when search_prefix; route_search(library, paths)
+ when static_prefix; route_static(library, paths)
end
end
nil
end
@@ -159,18 +164,23 @@
def route_search(library, paths)
return unless paths.empty?
SearchCommand.new(final_options(library, paths)).call(request)
end
+ def route_static(library, paths)
+ StaticFileCommand.new(final_options(library, paths)).call(request)
+ end
+
# @group Utility Methods
# Adds extra :library/:path option keys to the adapter options.
# Use this method when passing options to a command.
#
# @param (see #route_docs)
# @return [Hash] finalized options
def final_options(library, paths)
- adapter.options.merge(:library => library, :path => paths.join('/'))
+ path = File.cleanpath(paths.join('/')).gsub(%r{^(\.\./)+}, '')
+ adapter.options.merge(:library => library, :path => path)
end
end
end
end