yes 2
This commit is contained in:
parent
ac2dd66f36
commit
b006e0f672
106
hyde.rb
106
hyde.rb
|
@ -1,33 +1,111 @@
|
|||
module Hyde
|
||||
class Servlet
|
||||
def initialize (path, &block_optional)
|
||||
|
||||
# Interchangeable pattern matching
|
||||
module PatternMatching
|
||||
def prep_path(path)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
@path = normalize(path) if path.kind_of? String
|
||||
@path = path if path.kind_of? Regexp
|
||||
end
|
||||
def match?(path)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
split_path = path.split("/")
|
||||
puts "#{path}, #{@path}, #{normalize_input(path)}"
|
||||
if @path.kind_of? Regexp then
|
||||
anchored_regex = Regexp.new("^"+@path.to_s+"$")
|
||||
return anchored_regex.match split_path[0] unless @full_regexp
|
||||
return anchored_regex.match normalize_input(path)
|
||||
else
|
||||
return split_path[0] == @path
|
||||
end
|
||||
end
|
||||
def normalize_input(path)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
(path.split "/").filter { |x| x != "" }.join("/")
|
||||
end
|
||||
def normalize(path)
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
# remove multiple slashes in one spot
|
||||
path = (path.split "/").filter {|x| x != ""}.join("/")
|
||||
# globbing behaviour simulated with regexp
|
||||
if path.match /(?<!\\)[\*\?\[]/ then
|
||||
path = Regexp.new(path
|
||||
.gsub(/[\^\$\.\|\+\(\)\{\}]/,"\\\\\\0")
|
||||
.gsub(/(?<!\\)\*\*/,".*")
|
||||
.gsub(/(?<![.\\])\*/,"[^/]*")
|
||||
.gsub(/(?<!\\)\?/,".")
|
||||
.gsub(/(?<!\\)\[\!/,"[^")
|
||||
)
|
||||
end
|
||||
return path
|
||||
end
|
||||
end
|
||||
|
||||
# Handler classes
|
||||
class Probe
|
||||
include Hyde::PatternMatching
|
||||
def initialize (path, &block_optional)
|
||||
prep_path path
|
||||
@lock_methods = true
|
||||
self.instance_eval &block_optional if block_given?
|
||||
@lock_methods = false
|
||||
end
|
||||
def match(path)
|
||||
puts (match? path)
|
||||
if match? path then
|
||||
exec if defined? exec
|
||||
end
|
||||
end
|
||||
end
|
||||
class PrintProbe < Hyde::Probe
|
||||
def match(path)
|
||||
puts (match? path)
|
||||
if match? path then
|
||||
puts "#{path} matched!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Handler invocation functions
|
||||
module Handlers
|
||||
def probe(*a, **b, &block)
|
||||
@chain.append Hyde::Probe.new *a, **b, &block
|
||||
end
|
||||
def printProbe(*a, **b, &block)
|
||||
@chain.append Hyde::PrintProbe.new *a, **b, &block
|
||||
end
|
||||
end
|
||||
|
||||
class Pathspec
|
||||
def initialize (path, &block, root: nil)
|
||||
@path = (path.split "/").filter {|x| x != ""} if path.kind_of? String
|
||||
@path = [path] if path.kind_of? Regexp
|
||||
include Hyde::PatternMatching
|
||||
include Hyde::Handlers
|
||||
def initialize (path, root_path: nil, full_regexp: false, &block)
|
||||
prep_path path
|
||||
@chain = []
|
||||
@root_override = root
|
||||
@root_override = root_path
|
||||
@remap = false
|
||||
self.instance_eval &block
|
||||
@lock_methods = true
|
||||
@full_regexp = full_regexp
|
||||
self.instance_eval &block
|
||||
@lock_methods = false
|
||||
end
|
||||
def path(*a, **b, &block)
|
||||
@chain.append Hyde::Pathspec.new *a, **b, &block
|
||||
end
|
||||
def root(path)
|
||||
@root_override = root
|
||||
@root_override = path
|
||||
end
|
||||
def remap(path)
|
||||
@root_override = root
|
||||
@root_override = path
|
||||
@remap = true
|
||||
end
|
||||
def match(path)
|
||||
|
||||
end
|
||||
def match?(path)
|
||||
|
||||
raise Exception, "Not permitted" if @lock_methods
|
||||
if match? path then
|
||||
cut_path = normalize_input(path).sub(@path, "")
|
||||
next_pathspec = @chain.find { |x| x.match? cut_path }
|
||||
puts next_pathspec.inspect
|
||||
next_pathspec.match cut_path if next_pathspec
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
require_relative "hyde"
|
||||
path = Hyde::Pathspec.new "/" do
|
||||
path "about" do
|
||||
printProbe "test"
|
||||
printProbe "test_*"
|
||||
end
|
||||
path "docs" do
|
||||
printProbe "test"
|
||||
printProbe "test_*"
|
||||
end
|
||||
end
|
||||
path.match "/about/speen"
|
||||
path.match "/about/test"
|
||||
path.match "/about/test_2"
|
||||
path.match "/docs/speen"
|
||||
path.match "/docs/test"
|
||||
path.match "/docs/test_2"
|
Loading…
Reference in New Issue