This commit is contained in:
Yessiest 2023-04-12 15:42:07 +04:00
parent ac2dd66f36
commit b006e0f672
2 changed files with 109 additions and 14 deletions

104
hyde.rb
View File

@ -1,33 +1,111 @@
module Hyde module Hyde
class Servlet # Interchangeable pattern matching
def initialize (path, &block_optional) 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
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 class Pathspec
def initialize (path, &block, root: nil) include Hyde::PatternMatching
@path = (path.split "/").filter {|x| x != ""} if path.kind_of? String include Hyde::Handlers
@path = [path] if path.kind_of? Regexp def initialize (path, root_path: nil, full_regexp: false, &block)
prep_path path
@chain = [] @chain = []
@root_override = root @root_override = root_path
@remap = false @remap = false
@lock_methods = true
@full_regexp = full_regexp
self.instance_eval &block self.instance_eval &block
@lock_methods = false
end end
def path(*a, **b, &block) def path(*a, **b, &block)
@chain.append Hyde::Pathspec.new *a, **b, &block @chain.append Hyde::Pathspec.new *a, **b, &block
end end
def root(path) def root(path)
@root_override = root @root_override = path
end end
def remap(path) def remap(path)
@root_override = root @root_override = path
@remap = true @remap = true
end end
def match(path) def match(path)
raise Exception, "Not permitted" if @lock_methods
end if match? path then
def match?(path) 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 end
end end

17
test_hyde.rb Normal file
View File

@ -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"