mustermann 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +429 -665
- data/lib/mustermann/ast/boundaries.rb +44 -0
- data/lib/mustermann/ast/compiler.rb +13 -7
- data/lib/mustermann/ast/expander.rb +22 -12
- data/lib/mustermann/ast/node.rb +69 -5
- data/lib/mustermann/ast/param_scanner.rb +20 -0
- data/lib/mustermann/ast/parser.rb +138 -19
- data/lib/mustermann/ast/pattern.rb +59 -7
- data/lib/mustermann/ast/template_generator.rb +28 -0
- data/lib/mustermann/ast/transformer.rb +2 -2
- data/lib/mustermann/ast/translator.rb +22 -8
- data/lib/mustermann/ast/validation.rb +4 -3
- data/lib/mustermann/composite.rb +101 -0
- data/lib/mustermann/expander.rb +2 -2
- data/lib/mustermann/identity.rb +56 -0
- data/lib/mustermann/pattern.rb +187 -10
- data/lib/mustermann/pattern_cache.rb +49 -0
- data/lib/mustermann/regexp.rb +1 -0
- data/lib/mustermann/regexp_based.rb +18 -1
- data/lib/mustermann/regular.rb +4 -1
- data/lib/mustermann/simple_match.rb +5 -0
- data/lib/mustermann/sinatra.rb +22 -5
- data/lib/mustermann/to_pattern.rb +11 -6
- data/lib/mustermann/version.rb +1 -1
- data/lib/mustermann.rb +95 -20
- data/mustermann.gemspec +1 -14
- data/spec/ast_spec.rb +14 -0
- data/spec/composite_spec.rb +147 -0
- data/spec/expander_spec.rb +15 -0
- data/spec/identity_spec.rb +44 -0
- data/spec/mustermann_spec.rb +21 -2
- data/spec/pattern_spec.rb +7 -3
- data/spec/regular_spec.rb +25 -0
- data/spec/sinatra_spec.rb +184 -9
- data/spec/to_pattern_spec.rb +49 -0
- metadata +15 -180
- data/.gitignore +0 -18
- data/.rspec +0 -2
- data/.travis.yml +0 -10
- data/.yardopts +0 -1
- data/Gemfile +0 -2
- data/LICENSE +0 -22
- data/Rakefile +0 -6
- data/internals.md +0 -64
- data/lib/mustermann/ast/tree_renderer.rb +0 -29
- data/lib/mustermann/rails.rb +0 -17
- data/lib/mustermann/shell.rb +0 -29
- data/lib/mustermann/simple.rb +0 -35
- data/lib/mustermann/template.rb +0 -47
- data/spec/rails_spec.rb +0 -521
- data/spec/shell_spec.rb +0 -108
- data/spec/simple_spec.rb +0 -236
- data/spec/support/coverage.rb +0 -16
- data/spec/support/env.rb +0 -16
- data/spec/support/expand_matcher.rb +0 -27
- data/spec/support/match_matcher.rb +0 -39
- data/spec/support/pattern.rb +0 -39
- data/spec/support.rb +0 -5
- data/spec/template_spec.rb +0 -814
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/.yardopts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
- README.md internals.md
|
data/Gemfile
DELETED
data/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
Copyright (c) 2013 Konstantin Haase
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person
|
|
4
|
-
obtaining a copy of this software and associated documentation
|
|
5
|
-
files (the "Software"), to deal in the Software without
|
|
6
|
-
restriction, including without limitation the rights to use,
|
|
7
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
copies of the Software, and to permit persons to whom the
|
|
9
|
-
Software is furnished to do so, subject to the following
|
|
10
|
-
conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be
|
|
13
|
-
included in all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
17
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
18
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
20
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
21
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
data/internals.md
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# Internal API
|
|
2
|
-
|
|
3
|
-
This document describes how to use [Mustermann](README.md)'s internal API.
|
|
4
|
-
|
|
5
|
-
It is a secondary goal to keep the internal API as stable as possible, in a state where it would well be possible to interface with it.
|
|
6
|
-
However, the internal API is not covered by Semantic Versioning. As a rule of thumb, no backwards incompatible changes should be introduced to the API in minor releases (starting from 1.0.0).
|
|
7
|
-
|
|
8
|
-
Should the internal API gain widespread/production use, we might consider moving parts of it over into the public API.
|
|
9
|
-
|
|
10
|
-
Here is a quick example of what you can do with this:
|
|
11
|
-
|
|
12
|
-
``` ruby
|
|
13
|
-
require 'mustermann/ast/pattern'
|
|
14
|
-
|
|
15
|
-
class MyPattern < Mustermann::AST::Pattern
|
|
16
|
-
on("~") { |c| node(:capture, buffer[1]) if expect(/\{(\w+)\}/) }
|
|
17
|
-
on("+") { |c| node(:named_splat, buffer[1]) if expect(/\{(\w+)\}/) }
|
|
18
|
-
on("?") { |c| node(:optional, node(:capture, buffer[1])) if expect(/\{(\w+)\}/) }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
pattern = MyPattern.new("/+{prefix}/~{page}/?{optional}")
|
|
22
|
-
pattern.params("/a/") # => nil
|
|
23
|
-
pattern.params("/a/b/") # => { "prefix" => "a", "page" => "b", "optional" => nil }
|
|
24
|
-
pattern.params("/a/b/c") # => { "prefix" => "a", "page" => "b", "optional" => "c" }
|
|
25
|
-
pattern.params("/a/b/c/") # => { "prefix" => "a/b", "page" => "c", "optional" => nil }
|
|
26
|
-
|
|
27
|
-
pattern.expand(prefix: "a", page: "foo") # => "/a/foo/"
|
|
28
|
-
pattern.expand(prefix: "a/b", page: "c/d") # => "/a/b/c%2Fd/"
|
|
29
|
-
|
|
30
|
-
require 'mustermann'
|
|
31
|
-
Mustermann.register(:my_pattern, MyPattern, load: false)
|
|
32
|
-
Mustermann.new('/+{prefix}/~{page}/?{optional}', type: :my_pattern) # => #<MyPattern:"/+{prefix}/~{page}/?{optional}">
|
|
33
|
-
|
|
34
|
-
require 'sinatra/base'
|
|
35
|
-
class MyApp < Sinatra::Base
|
|
36
|
-
register Mustermann
|
|
37
|
-
set :pattern, type: :my_pattern
|
|
38
|
-
|
|
39
|
-
get '/hello/~{name}' do
|
|
40
|
-
"Hello #{params[:name].capitalize}!"
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
require 'mustermann/ast/tree_renderer'
|
|
45
|
-
ast = MyPattern::Parser.parse(pattern.to_s)
|
|
46
|
-
puts Mustermann::AST::TreeRenderer.render(ast)
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Pattern Registration
|
|
51
|
-
|
|
52
|
-
...
|
|
53
|
-
|
|
54
|
-
## Build Your Own Pattern
|
|
55
|
-
|
|
56
|
-
...
|
|
57
|
-
|
|
58
|
-
## Patterns Based on Regular Expressions
|
|
59
|
-
|
|
60
|
-
...
|
|
61
|
-
|
|
62
|
-
## AST-Based Patterns
|
|
63
|
-
|
|
64
|
-
...
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
require 'mustermann/ast/translator'
|
|
2
|
-
|
|
3
|
-
module Mustermann
|
|
4
|
-
module AST
|
|
5
|
-
# Turns an AST into a human readable string.
|
|
6
|
-
# @!visibility private
|
|
7
|
-
class TreeRenderer < Translator
|
|
8
|
-
# @example
|
|
9
|
-
# Mustermann::AST::TreeRenderer.render Mustermann::Sinatra::Parser.parse('/foo')
|
|
10
|
-
#
|
|
11
|
-
# @!visibility private
|
|
12
|
-
def self.render(ast)
|
|
13
|
-
new.translate(ast)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
translate(Object) { inspect }
|
|
17
|
-
translate(Array) { map { |e| "\n" << t(e) }.join.gsub("\n", "\n ") }
|
|
18
|
-
translate(:node) { "#{t.type(node)} #{t(payload)}" }
|
|
19
|
-
translate(:with_look_ahead) { "#{t.type(node)} #{t(head)} #{t(payload)}" }
|
|
20
|
-
|
|
21
|
-
# Turns a class name into a node identifier.
|
|
22
|
-
#
|
|
23
|
-
# @!visibility private
|
|
24
|
-
def type(node)
|
|
25
|
-
node.class.name[/[^:]+$/].split(/(?<=.)(?=[A-Z])/).map(&:downcase).join(?_)
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
data/lib/mustermann/rails.rb
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
require 'mustermann/ast/pattern'
|
|
2
|
-
|
|
3
|
-
module Mustermann
|
|
4
|
-
# Rails style pattern implementation.
|
|
5
|
-
#
|
|
6
|
-
# @example
|
|
7
|
-
# Mustermann.new('/:foo', type: :rails) === '/bar' # => true
|
|
8
|
-
#
|
|
9
|
-
# @see Mustermann::Pattern
|
|
10
|
-
# @see file:README.md#rails Syntax description in the README
|
|
11
|
-
class Rails < AST::Pattern
|
|
12
|
-
on(nil, ?)) { |c| unexpected(c) }
|
|
13
|
-
on(?*) { |c| node(:named_splat) { scan(/\w+/) } }
|
|
14
|
-
on(?() { |c| node(:optional, node(:group) { read unless scan(?)) }) }
|
|
15
|
-
on(?:) { |c| node(:capture) { scan(/\w+/) } }
|
|
16
|
-
end
|
|
17
|
-
end
|
data/lib/mustermann/shell.rb
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
require 'mustermann/pattern'
|
|
2
|
-
require 'mustermann/simple_match'
|
|
3
|
-
|
|
4
|
-
module Mustermann
|
|
5
|
-
# Matches strings that are identical to the pattern.
|
|
6
|
-
#
|
|
7
|
-
# @example
|
|
8
|
-
# Mustermann.new('/*.*', type: :shell) === '/bar' # => false
|
|
9
|
-
#
|
|
10
|
-
# @see Mustermann::Pattern
|
|
11
|
-
# @see file:README.md#shell Syntax description in the README
|
|
12
|
-
class Shell < Pattern
|
|
13
|
-
# @param (see Mustermann::Pattern#initialize)
|
|
14
|
-
# @return (see Mustermann::Pattern#initialize)
|
|
15
|
-
# @see (see Mustermann::Pattern#initialize)
|
|
16
|
-
def initialize(string, **options)
|
|
17
|
-
@flags = File::FNM_PATHNAME | File::FNM_DOTMATCH
|
|
18
|
-
@flags |= File::FNM_EXTGLOB if defined? File::FNM_EXTGLOB
|
|
19
|
-
super(string, **options)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
# @param (see Mustermann::Pattern#===)
|
|
23
|
-
# @return (see Mustermann::Pattern#===)
|
|
24
|
-
# @see (see Mustermann::Pattern#===)
|
|
25
|
-
def ===(string)
|
|
26
|
-
File.fnmatch? @string, unescape(string), @flags
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
data/lib/mustermann/simple.rb
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
require 'mustermann/regexp_based'
|
|
2
|
-
|
|
3
|
-
module Mustermann
|
|
4
|
-
# Sinatra 1.3 style pattern implementation.
|
|
5
|
-
#
|
|
6
|
-
# @example
|
|
7
|
-
# Mustermann.new('/:foo', type: :simple) === '/bar' # => true
|
|
8
|
-
#
|
|
9
|
-
# @see Mustermann::Pattern
|
|
10
|
-
# @see file:README.md#simple Syntax description in the README
|
|
11
|
-
class Simple < RegexpBased
|
|
12
|
-
supported_options :greedy, :space_matches_plus
|
|
13
|
-
|
|
14
|
-
def compile(greedy: true, uri_decode: true, space_matches_plus: true, **options)
|
|
15
|
-
pattern = @string.gsub(/[^\?\%\\\/\:\*\w]/) { |c| encoded(c, uri_decode, space_matches_plus) }
|
|
16
|
-
pattern.gsub!(/((:\w+)|\*)/) do |match|
|
|
17
|
-
match == "*" ? "(?<splat>.*?)" : "(?<#{$2[1..-1]}>[^/?#]+#{?? unless greedy})"
|
|
18
|
-
end
|
|
19
|
-
/\A#{Regexp.new(pattern)}\Z/
|
|
20
|
-
rescue SyntaxError, RegexpError => error
|
|
21
|
-
type = error.message["invalid group name"] ? CompileError : ParseError
|
|
22
|
-
raise type, error.message, error.backtrace
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def encoded(char, uri_decode, space_matches_plus)
|
|
26
|
-
return Regexp.escape(char) unless uri_decode
|
|
27
|
-
parser = URI::Parser.new
|
|
28
|
-
encoded = Regexp.union(parser.escape(char), parser.escape(char, /./).downcase, parser.escape(char, /./).upcase)
|
|
29
|
-
encoded = Regexp.union(encoded, encoded('+', true, true)) if space_matches_plus and char == " "
|
|
30
|
-
encoded
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
private :compile, :encoded
|
|
34
|
-
end
|
|
35
|
-
end
|
data/lib/mustermann/template.rb
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
require 'mustermann/ast/pattern'
|
|
2
|
-
|
|
3
|
-
module Mustermann
|
|
4
|
-
# URI template pattern implementation.
|
|
5
|
-
#
|
|
6
|
-
# @example
|
|
7
|
-
# Mustermann.new('/{foo}') === '/bar' # => true
|
|
8
|
-
#
|
|
9
|
-
# @see Mustermann::Pattern
|
|
10
|
-
# @see file:README.md#template Syntax description in the README
|
|
11
|
-
# @see http://tools.ietf.org/html/rfc6570 RFC 6570
|
|
12
|
-
class Template < AST::Pattern
|
|
13
|
-
on ?{ do |char|
|
|
14
|
-
variable = proc do
|
|
15
|
-
match = expect(/(?<name>\w+)(?:\:(?<prefix>\d{1,4})|(?<explode>\*))?/)
|
|
16
|
-
node(:variable, match[:name], prefix: match[:prefix], explode: match[:explode])
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
operator = buffer.scan(/[\+\#\.\/;\?\&\=\,\!\@\|]/)
|
|
20
|
-
expression = node(:expression, [variable[]], operator: operator) { variable[] if scan(?,) }
|
|
21
|
-
expression if expect(?})
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
on(?}) { |c| unexpected(c) }
|
|
25
|
-
|
|
26
|
-
# @!visibility private
|
|
27
|
-
def compile(*args, **options)
|
|
28
|
-
@split_params = {}
|
|
29
|
-
super(*args, split_params: @split_params, **options)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# @!visibility private
|
|
33
|
-
def map_param(key, value)
|
|
34
|
-
return super unless variable = @split_params[key]
|
|
35
|
-
value = value.split variable[:separator]
|
|
36
|
-
value.map! { |e| e.sub(/\A#{key}=/, '') } if variable[:parametric]
|
|
37
|
-
value.map! { |e| super(key, e) }
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# @!visibility private
|
|
41
|
-
def always_array?(key)
|
|
42
|
-
@split_params.include? key
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
private :compile, :map_param, :always_array?
|
|
46
|
-
end
|
|
47
|
-
end
|