mustermann-contrib 1.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +1239 -0
  3. data/examples/highlighting.rb +35 -0
  4. data/highlighting.png +0 -0
  5. data/irb.png +0 -0
  6. data/lib/mustermann/cake.rb +18 -0
  7. data/lib/mustermann/express.rb +37 -0
  8. data/lib/mustermann/file_utils.rb +217 -0
  9. data/lib/mustermann/file_utils/glob_pattern.rb +39 -0
  10. data/lib/mustermann/fileutils.rb +1 -0
  11. data/lib/mustermann/flask.rb +198 -0
  12. data/lib/mustermann/grape.rb +35 -0
  13. data/lib/mustermann/pyramid.rb +28 -0
  14. data/lib/mustermann/rails.rb +46 -0
  15. data/lib/mustermann/shell.rb +56 -0
  16. data/lib/mustermann/simple.rb +50 -0
  17. data/lib/mustermann/string_scanner.rb +313 -0
  18. data/lib/mustermann/strscan.rb +1 -0
  19. data/lib/mustermann/template.rb +62 -0
  20. data/lib/mustermann/uri_template.rb +1 -0
  21. data/lib/mustermann/versions.rb +46 -0
  22. data/lib/mustermann/visualizer.rb +38 -0
  23. data/lib/mustermann/visualizer/highlight.rb +137 -0
  24. data/lib/mustermann/visualizer/highlighter.rb +37 -0
  25. data/lib/mustermann/visualizer/highlighter/ad_hoc.rb +94 -0
  26. data/lib/mustermann/visualizer/highlighter/ast.rb +102 -0
  27. data/lib/mustermann/visualizer/highlighter/composite.rb +45 -0
  28. data/lib/mustermann/visualizer/highlighter/dummy.rb +18 -0
  29. data/lib/mustermann/visualizer/highlighter/regular.rb +104 -0
  30. data/lib/mustermann/visualizer/pattern_extension.rb +68 -0
  31. data/lib/mustermann/visualizer/renderer/ansi.rb +23 -0
  32. data/lib/mustermann/visualizer/renderer/generic.rb +46 -0
  33. data/lib/mustermann/visualizer/renderer/hansi_template.rb +34 -0
  34. data/lib/mustermann/visualizer/renderer/html.rb +50 -0
  35. data/lib/mustermann/visualizer/renderer/sexp.rb +37 -0
  36. data/lib/mustermann/visualizer/tree.rb +63 -0
  37. data/lib/mustermann/visualizer/tree_renderer.rb +78 -0
  38. data/mustermann-contrib.gemspec +19 -0
  39. data/spec/cake_spec.rb +90 -0
  40. data/spec/express_spec.rb +209 -0
  41. data/spec/file_utils_spec.rb +119 -0
  42. data/spec/flask_spec.rb +361 -0
  43. data/spec/flask_subclass_spec.rb +368 -0
  44. data/spec/grape_spec.rb +747 -0
  45. data/spec/pattern_extension_spec.rb +49 -0
  46. data/spec/pyramid_spec.rb +101 -0
  47. data/spec/rails_spec.rb +647 -0
  48. data/spec/shell_spec.rb +147 -0
  49. data/spec/simple_spec.rb +268 -0
  50. data/spec/string_scanner_spec.rb +271 -0
  51. data/spec/template_spec.rb +841 -0
  52. data/spec/visualizer_spec.rb +199 -0
  53. data/theme.png +0 -0
  54. data/tree.png +0 -0
  55. metadata +126 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6ef2cc24604b7944b3b4e9a97fd7eacd331f4af
4
+ data.tar.gz: d4d0031aff198de4a190cda57c5cf25a0bade3b5
5
+ SHA512:
6
+ metadata.gz: 339f192ae2f720a0a5882d962c855c2d9f6285524db013b2fe56fb0e351cf88c13cbbff9c2d2cc84f42d740204f4c8ccf1ae965db577d5c11e8cd9211f5b279d
7
+ data.tar.gz: 42293a657b0de5cda8d6a317108f59a138542fa2c88f0e317b02022d71e425ff7780e021dc42edd7e5df4ab6b3808a0841e8ab2239a90e3d0c1a48801b7dc267
data/README.md ADDED
@@ -0,0 +1,1239 @@
1
+ # The Amazing Mustermann - Contrib Edition
2
+
3
+ This is a meta gem that depends on all mustermann gems.
4
+
5
+ ``` console
6
+ $ gem install mustermann-contrib
7
+ Successfully installed mustermann-1.0.0
8
+ Successfully installed mustermann-contrib-1.0.0
9
+ ...
10
+ ```
11
+
12
+ Also handy for your `Gemfile`:
13
+
14
+ ``` ruby
15
+ gem 'mustermann-contrib'
16
+ ```
17
+
18
+ Alternatively, you can use latest HEAD from github:
19
+
20
+ ```ruby
21
+ github 'sinatra/mustermann' do
22
+ gem 'mustermann'
23
+ gem 'mustermann-contrib'
24
+ end
25
+ ```
26
+
27
+ Below is the documentation for all extensions, including:
28
+
29
+ * mustermann-cake
30
+ * mustermann-express
31
+ * mustermann-fileutils
32
+ * mustermann-flask
33
+ * mustermann-grape
34
+ * mustermann-pyramid
35
+ * mustermann-rails
36
+ * mustermann-shell
37
+ * mustermann-simple
38
+ * mustermann-strscan
39
+ * mustermann-uri-template
40
+ * mustermann-visualizer
41
+
42
+ # CakePHP Syntax for Mustermann
43
+
44
+ This gem implements the `cake` pattern type for Mustermann. It is compatible with [CakePHP](http://cakephp.org/) 2.x and 3.x.
45
+
46
+ ## Overview
47
+
48
+ **Supported options:**
49
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, and `ignore_unknown_options`.
50
+
51
+ **External documentation:**
52
+ [CakePHP 2.0 Routing](http://book.cakephp.org/2.0/en/development/routing.html),
53
+ [CakePHP 3.0 Routing](http://book.cakephp.org/3.0/en/development/routing.html)
54
+
55
+ CakePHP patterns feature captures and unnamed splats. Captures are prefixed with a colon and splats are either a single asterisk (parsing segments into an array) or a double asterisk (parsing segments as a single string).
56
+
57
+ ``` ruby
58
+ require 'mustermann/cake'
59
+
60
+ Mustermann.new('/:name/*', type: :cake).params('/a/b/c') # => { name: 'a', splat: ['b', 'c'] }
61
+ Mustermann.new('/:name/**', type: :cake).params('/a/b/c') # => { name: 'a', splat: 'b/c' }
62
+
63
+ pattern = Mustermann.new('/:name')
64
+
65
+ pattern.respond_to? :expand # => true
66
+ pattern.expand(name: 'foo') # => '/foo'
67
+
68
+ pattern.respond_to? :to_templates # => true
69
+ pattern.to_templates # => ['/{name}']
70
+ ```
71
+
72
+ ## Syntax
73
+
74
+ <table>
75
+ <thead>
76
+ <tr>
77
+ <th>Syntax Element</th>
78
+ <th>Description</th>
79
+ </tr>
80
+ </thead>
81
+ <tbody>
82
+ <tr>
83
+ <td><b>:</b><i>name</i></td>
84
+ <td>
85
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
86
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
87
+ </td>
88
+ </tr>
89
+ <tr>
90
+ <td><b>*</b></td>
91
+ <td>
92
+ Captures anything in a non-greedy fashion. Capture is named splat.
93
+ It is always an array of captures, as you can use it more than once in a pattern.
94
+ </td>
95
+ </tr>
96
+ <tr>
97
+ <td><b>**</b></td>
98
+ <td>
99
+ Captures anything in a non-greedy fashion. Capture is named splat.
100
+ It is always an array of captures, as you can use it more than once in a pattern.
101
+ The value matching a single <tt>**</tt> will be split at slashes when parsed into <tt>params</tt>.
102
+ </td>
103
+ </tr>
104
+ <tr>
105
+ <td><b>/</b></td>
106
+ <td>
107
+ Matches forward slash. Does not match URI encoded version of forward slash.
108
+ </td>
109
+ </tr>
110
+ <tr>
111
+ <td><i>any other character</i></td>
112
+ <td>Matches exactly that character or a URI encoded version of it.</td>
113
+ </tr>
114
+ </tbody>
115
+ </table>
116
+
117
+
118
+ # Express Syntax for Mustermann
119
+
120
+ This gem implements the `express` pattern type for Mustermann. It is compatible with [Express](http://expressjs.com/) and [pillar.js](https://pillarjs.github.io/).
121
+
122
+ ## Overview
123
+
124
+ **Supported options:**
125
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, and `ignore_unknown_options`.
126
+
127
+ **External documentation:**
128
+ [path-to-regexp](https://github.com/pillarjs/path-to-regexp#path-to-regexp),
129
+ [live demo](http://forbeslindesay.github.io/express-route-tester/)
130
+
131
+ Express patterns feature named captures (with repetition support via suffixes) that start with a colon and can have an optional regular expression constraint or unnamed captures that require a constraint.
132
+
133
+ ``` ruby
134
+ require 'mustermann/express'
135
+
136
+ Mustermann.new('/:name/:rest+', type: :express).params('/a/b/c') # => { name: 'a', rest: 'b/c' }
137
+
138
+ pattern = Mustermann.new('/:name', type: :express)
139
+
140
+ pattern.respond_to? :expand # => true
141
+ pattern.expand(name: 'foo') # => '/foo'
142
+
143
+ pattern.respond_to? :to_templates # => true
144
+ pattern.to_templates # => ['/{name}']
145
+ ```
146
+
147
+ ## Syntax
148
+
149
+ <table>
150
+ <thead>
151
+ <tr>
152
+ <th>Syntax Element</th>
153
+ <th>Description</th>
154
+ </tr>
155
+ </thead>
156
+ <tbody>
157
+ <tr>
158
+ <td><b>:</b><i>name</i></td>
159
+ <td>
160
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
161
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
162
+ </td>
163
+ </tr>
164
+ <tr>
165
+ <td><b>:</b><i>name</i><b>+</b></td>
166
+ <td>
167
+ Captures one or more segments (with segments being separated by forward slashes).
168
+ Capture is named <i>name</i>.
169
+ Capture behavior can be modified with <tt>capture</tt> option.
170
+ </td>
171
+ </tr>
172
+ <tr>
173
+ <td><b>:</b><i>name</i><b>*</b></td>
174
+ <td>
175
+ Captures zero or more segments (with segments being separated by forward slashes).
176
+ Capture is named <i>name</i>.
177
+ Capture behavior can be modified with <tt>capture</tt> option.
178
+ </td>
179
+ </tr>
180
+ <tr>
181
+ <td><b>:</b><i>name</i><b>?</b></td>
182
+ <td>
183
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
184
+ Also matches an empty string.
185
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
186
+ </td>
187
+ </tr>
188
+ <tr>
189
+ <td><b>:</b><i>name</i><b>(</b><i>regexp</i><b>)</b></td>
190
+ <td>
191
+ Captures anything matching the <i>regexp</i> regular expression. Capture is named <i>name</i>.
192
+ Capture behavior can be modified with <tt>capture</tt>.
193
+ </td>
194
+ </tr>
195
+ <tr>
196
+ <td><b>(</b><i>regexp</i><b>)</b></td>
197
+ <td>
198
+ Captures anything matching the <i>regexp</i> regular expression. Capture is named splat.
199
+ Capture behavior can be modified with <tt>capture</tt>.
200
+ </td>
201
+ </tr>
202
+ <tr>
203
+ <td><b>/</b></td>
204
+ <td>
205
+ Matches forward slash. Does not match URI encoded version of forward slash.
206
+ </td>
207
+ </tr>
208
+ <tr>
209
+ <td><i>any other character</i></td>
210
+ <td>Matches exactly that character or a URI encoded version of it.</td>
211
+ </tr>
212
+ </tbody>
213
+ </table>
214
+
215
+ # FileUtils for Mustermann
216
+
217
+ This gem implements efficient file system operations for Mustermann patterns.
218
+
219
+ ## Globbing
220
+
221
+ All operations work on a list of files described by one or more pattern.
222
+
223
+ ``` ruby
224
+ require 'mustermann/file_utils'
225
+
226
+ Mustermann::FileUtils[':base.:ext'] # => ['example.txt']
227
+
228
+ Mustermann::FileUtils.glob(':base.:ext') do |file, params|
229
+ file # => "example.txt"
230
+ params # => {"base"=>"example", "ext"=>"txt"}
231
+ end
232
+ ```
233
+
234
+ To avoid having to loop over all files and see if they match, it will generate a glob pattern resembling the Mustermann pattern as closely as possible.
235
+
236
+ ``` ruby
237
+ require 'mustermann/file_utils'
238
+
239
+ Mustermann::FileUtils.glob_pattern('/:name') # => '/*'
240
+ Mustermann::FileUtils.glob_pattern('src/:path/:file.(js|rb)') # => 'src/**/*/*.{js,rb}'
241
+ Mustermann::FileUtils.glob_pattern('{a,b}/*', type: :shell) # => '{a,b}/*'
242
+
243
+ pattern = Mustermann.new('/foo/:page', '/bar/:page') # => #<Mustermann::Composite:...>
244
+ Mustermann::FileUtils.glob_pattern(pattern) # => "{/foo/*,/bar/*}"
245
+ ```
246
+
247
+ ## Mapping
248
+
249
+ It is also possible to search for files and have their paths mapped onto another path in one method call:
250
+
251
+ ``` ruby
252
+ require 'mustermann/file_utils'
253
+
254
+ Mustermann::FileUtils.glob_map(':base.:ext' => ':base.bak.:ext') # => {'example.txt' => 'example.bak.txt'}
255
+ Mustermann::FileUtils.glob_map(':base.:ext' => :base) { |file, mapped| mapped } # => ['example']
256
+ ```
257
+
258
+ This mechanism allows things like copying, renaming and linking files:
259
+
260
+ ``` ruby
261
+ require 'mustermann/file_utils'
262
+
263
+ # copies example.txt to example.bak.txt
264
+ Mustermann::FileUtils.cp(':base.:ext' => ':base.bak.:ext')
265
+
266
+ # copies Foo.app/example.txt to Foo.back.app/example.txt
267
+ Mustermann::FileUtils.cp_r(':base.:ext' => ':base.bak.:ext')
268
+
269
+ # creates a symbolic link from bin/example to lib/example.rb
270
+ Mustermann::FileUtils.ln_s('lib/:name.rb' => 'bin/:name')
271
+ ```
272
+
273
+ # Flask Syntax for Mustermann
274
+
275
+ This gem implements the `flask` pattern type for Mustermann. It is compatible with [Flask](http://flask.pocoo.org/) and [Werkzeug](http://werkzeug.pocoo.org/).
276
+
277
+ ## Overview
278
+
279
+ **Supported options:**
280
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, `converters` and `ignore_unknown_options`
281
+
282
+ **External documentation:**
283
+ [Werkzeug: URL Routing](http://werkzeug.pocoo.org/docs/0.9/routing/)
284
+
285
+ ``` ruby
286
+ require 'mustermann/flask'
287
+
288
+ Mustermann.new('/<prefix>/<path:page>', type: :flask).params('/a/b/c') # => { prefix: 'a', page: 'b/c' }
289
+
290
+ pattern = Mustermann.new('/<name>', type: :flask)
291
+
292
+ pattern.respond_to? :expand # => true
293
+ pattern.expand(name: 'foo') # => '/foo'
294
+
295
+ pattern.respond_to? :to_templates # => true
296
+ pattern.to_templates # => ['/{name}']
297
+ ```
298
+
299
+ ## Syntax
300
+
301
+ <table>
302
+ <thead>
303
+ <tr>
304
+ <th>Syntax Element</th>
305
+ <th>Description</th>
306
+ </tr>
307
+ </thead>
308
+ <tbody>
309
+ <tr>
310
+ <td><b>&lt;</b><i>name</i><b>&gt;</b></td>
311
+ <td>
312
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
313
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
314
+ </td>
315
+ </tr>
316
+ <tr>
317
+ <td><b>&lt;</b><i>converter</i><b>:</b><i>name</i><b>&gt;</b></td>
318
+ <td>
319
+ Captures depending on the converter constraint. Capture is named <i>name</i>.
320
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
321
+ See below.
322
+ </td>
323
+ </tr>
324
+ <tr>
325
+ <td><b>&lt;</b><i>converter</i><b>(</b><i>arguments</i><b>):</b><i>name</i><b>&gt;</b></td>
326
+ <td>
327
+ Captures depending on the converter constraint. Capture is named <i>name</i>.
328
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
329
+ Arguments are separated by comma. An argument can be a simple string, a string enclosed
330
+ in single or double quotes, or a key value pair (keys and values being separated by an
331
+ equal sign). See below.
332
+ </td>
333
+ </tr>
334
+ <tr>
335
+ <td><b>/</b></td>
336
+ <td>
337
+ Matches forward slash. Does not match URI encoded version of forward slash.
338
+ </td>
339
+ </tr>
340
+ <tr>
341
+ <td><i>any other character</i></td>
342
+ <td>Matches exactly that character or a URI encoded version of it.</td>
343
+ </tr>
344
+ </tbody>
345
+ </table>
346
+
347
+ ## Converters
348
+
349
+ ### Builtin Converters
350
+
351
+ #### `string`
352
+
353
+ Possible arguments: `minlength`, `maxlength`, `length`
354
+
355
+ Captures anything but a forward slash in a semi-greedy fashion.
356
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
357
+
358
+ This is also the default converter.
359
+
360
+ Examples:
361
+
362
+ ```
363
+ <name>
364
+ <string:name>
365
+ <string(minlength=3,maxlength=10):slug>
366
+ <string(length=10):slug>
367
+ ```
368
+
369
+ #### `int`
370
+
371
+ Possible arguments: `min`, `max`, `fixed_digits`
372
+
373
+ Captures digits.
374
+ Captured value will be converted to an Integer.
375
+
376
+ Examples:
377
+
378
+ ```
379
+ <int:id>
380
+ <int(min=1,max=5):page>
381
+ <int(fixed_digits=16):uuid>
382
+ ```
383
+
384
+ #### `float`
385
+
386
+ Possible arguments: `min`, `max`
387
+
388
+ Captures digits with a dot.
389
+ Captured value will be converted to an Float.
390
+
391
+ Examples:
392
+
393
+ ```
394
+ <float:precision>
395
+ <float(min=0,max=1):precision>
396
+ ```
397
+
398
+ #### `path`
399
+
400
+ Captures anything in a non-greedy fashion.
401
+
402
+ Example:
403
+
404
+ ```
405
+ <path:rest>
406
+ ```
407
+
408
+ #### `any`
409
+
410
+ Possible arguments: List of accepted strings.
411
+
412
+ Captures anything that matches one of the arguments exactly.
413
+
414
+ Example:
415
+
416
+ ```
417
+ <any(home,search,contact):page>
418
+ ```
419
+
420
+ ### Custom Converters
421
+
422
+ [Flask patterns](#-pattern-details-flask) support registering custom converters.
423
+
424
+ A converter object may implement any of the following methods:
425
+
426
+ * `convert`: Should return a block converting a string value to whatever value should end up in the `params` hash.
427
+ * `constraint`: Should return a regular expression limiting which input string will match the capture.
428
+ * `new`: Returns an object that may respond to `convert` and/or `constraint` as described above. Any arguments used for the converter inside the pattern will be passed to `new`.
429
+
430
+ ``` ruby
431
+ require 'mustermann/flask'
432
+
433
+ SimpleConverter = Struct.new(:constraint, :convert)
434
+ id_converter = SimpleConverter.new(/\d/, -> s { s.to_i })
435
+
436
+ class NumConverter
437
+ def initialize(base: 10)
438
+ @base = Integer(base)
439
+ end
440
+
441
+ def convert
442
+ -> s { s.to_i(@base) }
443
+ end
444
+
445
+ def constraint
446
+ @base > 10 ? /[\da-#{(@base-1).to_s(@base)}]/ : /[0-#{@base-1}]/
447
+ end
448
+ end
449
+
450
+ pattern = Mustermann.new('/<id:id>/<num(base=8):oct>/<num(base=16):hex>',
451
+ type: :flask, converters: { id: id_converter, num: NumConverter})
452
+
453
+ pattern.params('/10/12/f1') # => {"id"=>10, "oct"=>10, "hex"=>241}
454
+ ```
455
+
456
+ ### Global Converters
457
+
458
+ It is also possible to register a converter for all flask patterns, using `register_converter`:
459
+
460
+ ``` ruby
461
+ Mustermann::Flask.register_converter(:id, id_converter)
462
+ Mustermann::Flask.register_converter(:num, NumConverter)
463
+
464
+ pattern = Mustermann.new('/<id:id>/<num(base=8):oct>/<num(base=16):hex>', type: :flask)
465
+ pattern.params('/10/12/f1') # => {"id"=>10, "oct"=>10, "hex"=>241}
466
+ ```
467
+
468
+ There is a handy syntax for quickly creating new converter classes: When you pass a block instead of a converter object, it will yield a generic converter with setters and getters for `convert` and `constraint`, and any arguments passed to the converter.
469
+
470
+ ``` ruby
471
+ require 'mustermann/flask'
472
+
473
+ Mustermann::Flask.register_converter(:id) do |converter|
474
+ converter.constraint = /\d/
475
+ converter.convert = -> s { s.to_i }
476
+ end
477
+
478
+ Mustermann::Flask.register_converter(:num) do |converter, base: 10|
479
+ converter.constraint = base > 10 ? /[\da-#{(@base-1).to_s(base)}]/ : /[0-#{base-1}]/
480
+ converter.convert = -> s { s.to_i(base) }
481
+ end
482
+
483
+ pattern = Mustermann.new('/<id:id>/<num(base=8):oct>/<num(base=16):hex>', type: :flask)
484
+ pattern.params('/10/12/f1') # => {"id"=>10, "oct"=>10, "hex"=>241}
485
+ ```
486
+
487
+ ### Subclassing
488
+
489
+ Registering global converters will make these available for all Flask patterns. It might even override already registered converters. This global state might break unrelated code.
490
+
491
+ It is therefore recommended that, if you don't want to pass in the converters option for every pattern, you create your own subclass of `Mustermann::Flask`.
492
+
493
+ ``` ruby
494
+ require 'mustermann/flask'
495
+
496
+ MyFlask = Class.new(Mustermann::Flask)
497
+
498
+ MyFlask.register_converter(:id) do |converter|
499
+ converter.constraint = /\d/
500
+ converter.convert = -> s { s.to_i }
501
+ end
502
+
503
+ MyFlask.register_converter(:num) do |converter, base: 10|
504
+ converter.constraint = base > 10 ? /[\da-#{(@base-1).to_s(base)}]/ : /[0-#{base-1}]/
505
+ converter.convert = -> s { s.to_i(base) }
506
+ end
507
+
508
+ pattern = MyFlask.new('/<id:id>/<num(base=8):oct>/<num(base=16):hex>')
509
+ pattern.params('/10/12/f1') # => {"id"=>10, "oct"=>10, "hex"=>241}
510
+ ```
511
+
512
+ You can even register this type for usage with `Mustermann.new`:
513
+
514
+ ``` ruby
515
+ Mustermann.register(:my_flask, MyFlask)
516
+ pattern = Mustermann.new('/<id:id>/<num(base=8):oct>/<num(base=16):hex>', type: :my_flask)
517
+ pattern.params('/10/12/f1') # => {"id"=>10, "oct"=>10, "hex"=>241}
518
+ ```
519
+
520
+ # Grape Syntax for Mustermann
521
+
522
+ This gem implements the `grape` pattern type for Mustermann.
523
+
524
+ ## Overview
525
+
526
+ **Supported options:**
527
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, `converters` and `ignore_unknown_options`
528
+
529
+ ``` ruby
530
+ require 'mustermann/grape'
531
+
532
+ Mustermann.new('/:id', type: :grape).params('/foo') # => { id: 'foo' }
533
+ ```
534
+
535
+ ## Syntax
536
+
537
+ <table>
538
+ <thead>
539
+ <tr>
540
+ <th>Syntax Element</th>
541
+ <th>Description</th>
542
+ </tr>
543
+ </thead>
544
+ <tbody>
545
+ <tr>
546
+ <td><b>:</b><i>name</i> <i><b>or</b></i> <b>&#123;</b><i>name</i><b>&#125;</b></td>
547
+ <td>
548
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
549
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
550
+ </td>
551
+ </tr>
552
+ <tr>
553
+ <td><b>*</b><i>name</i> <i><b>or</b></i> <b>&#123;+</b><i>name</i><b>&#125;</b></td>
554
+ <td>
555
+ Captures anything in a non-greedy fashion. Capture is named <i>name</i>.
556
+ </td>
557
+ </tr>
558
+ <tr>
559
+ <td><b>*</b> <i><b>or</b></i> <b>&#123;+splat&#125;</b></td>
560
+ <td>
561
+ Captures anything in a non-greedy fashion. Capture is named splat.
562
+ It is always an array of captures, as you can use it more than once in a pattern.
563
+ </td>
564
+ </tr>
565
+ <tr>
566
+ <td><b>(</b><i>expression</i><b>)</b></td>
567
+ <td>
568
+ Enclosed <i>expression</i> is optional.
569
+ </td>
570
+ </tr>
571
+ <tr>
572
+ <td><i>expression</i><b>|</b><i>expression</i><b>|</b><i>...</i></td>
573
+ <td>
574
+ Will match anything matching the nested expressions. May contain any other syntax element, including captures.
575
+ </td>
576
+ </tr>
577
+ <tr>
578
+ <td><i>x</i><b>?</b></td>
579
+ <td>Makes <i>x</i> optional. For instance, <tt>(foo)?</tt> matches <tt>foo</tt> or an empty string.</td>
580
+ </tr>
581
+ <tr>
582
+ <td><b>/</b></td>
583
+ <td>
584
+ Matches forward slash. Does not match URI encoded version of forward slash.
585
+ </td>
586
+ </tr>
587
+ <tr>
588
+ <td><b>\</b><i>x</i></td>
589
+ <td>Matches <i>x</i> or URI encoded version of <i>x</i>. For instance <tt>\*</tt> matches <tt>*</tt>.</td>
590
+ </tr>
591
+ <tr>
592
+ <td><i>any other character</i></td>
593
+ <td>Matches exactly that character or a URI encoded version of it.</td>
594
+ </tr>
595
+ </tbody>
596
+ </table>
597
+
598
+
599
+ # Pyramid Syntax for Mustermann
600
+
601
+ This gem implements the `pyramid` pattern type for Mustermann. It is compatible with [Pyramid](http://www.pylonsproject.org/projects/pyramid/about) and [Pylons](http://www.pylonsproject.org/projects/pylons-framework/about).
602
+
603
+ ## Overview
604
+
605
+ **Supported options:**
606
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode` and `ignore_unknown_options`
607
+
608
+ **External Documentation:** [Pylons Framework: URL Configuration](http://docs.pylonsproject.org/projects/pylons-webframework/en/latest/configuration.html#url-config), [Pylons Book: Routes in Detail](http://pylonsbook.com/en/1.0/urls-routing-and-dispatch.html#routes-in-detail), [Pyramid: Route Pattern Syntax](http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/urldispatch.html#route-pattern-syntax)
609
+
610
+ ``` ruby
611
+ require 'mustermann/pyramid'
612
+
613
+ Mustermann.new('/{prefix}/*suffix', type: :pyramid).params('/a/b/c') # => { prefix: 'a', suffix: ['b', 'c'] }
614
+
615
+ pattern = Mustermann.new('/{name}', type: :pyramid)
616
+
617
+ pattern.respond_to? :expand # => true
618
+ pattern.expand(name: 'foo') # => '/foo'
619
+
620
+ pattern.respond_to? :to_templates # => true
621
+ pattern.to_templates # => ['/{name}']
622
+ ```
623
+
624
+ ## Syntax
625
+
626
+
627
+ <table>
628
+ <thead>
629
+ <tr>
630
+ <th>Syntax Element</th>
631
+ <th>Description</th>
632
+ </tr>
633
+ </thead>
634
+ <tbody>
635
+ <tr>
636
+ <td><b>&#123;</b><i>name</i><b>&#125;</b></td>
637
+ <td>
638
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
639
+ Capture behavior can be modified with <tt>capture</tt> and <tt>greedy</tt> option.
640
+ </td>
641
+ </tr>
642
+ <tr>
643
+ <td><b>&#123;</b><i>name</i><b>:</b><i>regexp</i><b>&#125;</b></td>
644
+ <td>
645
+ Captures anything matching the <i>regexp</i> regular expression. Capture is named <i>name</i>.
646
+ Capture behavior can be modified with <tt>capture</tt>.
647
+ </td>
648
+ </tr>
649
+ <tr>
650
+ <td><b>*</b><i>name</i></td>
651
+ <td>
652
+ Captures anything in a non-greedy fashion. Capture is named <i>name</i>.
653
+ </td>
654
+ </tr>
655
+ <tr>
656
+ <td><b>/</b></td>
657
+ <td>
658
+ Matches forward slash. Does not match URI encoded version of forward slash.
659
+ </td>
660
+ </tr>
661
+ <tr>
662
+ <td><i>any other character</i></td>
663
+ <td>Matches exactly that character or a URI encoded version of it.</td>
664
+ </tr>
665
+ </tbody>
666
+ </table>
667
+
668
+
669
+ # Rails Syntax for Mustermann
670
+
671
+ This gem implements the `rails` pattern type for Mustermann. It is compatible with [Ruby on Rails](http://rubyonrails.org/), [Journey](https://github.com/rails/journey), the [http_router gem](https://github.com/joshbuddy/http_router), [Lotus](http://lotusrb.org/) and [Scalatra](http://www.scalatra.org/) (if [configured](http://www.scalatra.org/2.3/guides/http/routes.html#toc_248))</td>
672
+
673
+ ## Overview
674
+
675
+ **Supported options:**
676
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, `version`, and `ignore_unknown_options`.
677
+
678
+ **External documentation:**
679
+ [Ruby on Rails Guides: Routing](http://guides.rubyonrails.org/routing.html).
680
+
681
+ ``` ruby
682
+ require 'mustermann'
683
+
684
+ pattern = Mustermann.new('/:example', type: :rails)
685
+ pattern === "/foo.bar" # => true
686
+ pattern === "/foo/bar" # => false
687
+ pattern.params("/foo.bar") # => { "example" => "foo.bar" }
688
+ pattern.params("/foo/bar") # => nil
689
+
690
+ pattern = Mustermann.new('/:example(/:optional)', type: :rails)
691
+ pattern === "/foo.bar" # => true
692
+ pattern === "/foo/bar" # => true
693
+ pattern.params("/foo.bar") # => { "example" => "foo.bar", "optional" => nil }
694
+ pattern.params("/foo/bar") # => { "example" => "foo", "optional" => "bar" }
695
+
696
+ pattern = Mustermann.new('/*example', type: :rails)
697
+ pattern === "/foo.bar" # => true
698
+ pattern === "/foo/bar" # => true
699
+ pattern.params("/foo.bar") # => { "example" => "foo.bar" }
700
+ pattern.params("/foo/bar") # => { "example" => "foo/bar" }
701
+ ```
702
+
703
+ ## Rails Compatibility
704
+
705
+ Rails syntax changed over time. You can target different Ruby on Rails versions by setting the `version` option to the desired Rails version.
706
+
707
+ The default is `4.2`. Versions prior to `2.3` are not supported.
708
+
709
+ ``` ruby
710
+ require 'mustermann'
711
+ Mustermann.new('/', type: :rails, version: "2.3")
712
+ Mustermann.new('/', type: :rails, version: "3.0.0")
713
+
714
+ require 'rails'
715
+ Mustermann.new('/', type: :rails, version: Rails::VERSION::STRING)
716
+ ```
717
+
718
+ ## Syntax
719
+
720
+ <table>
721
+ <thead>
722
+ <tr>
723
+ <th>Syntax Element</th>
724
+ <th>Description</th>
725
+ </tr>
726
+ </thead>
727
+ <tbody>
728
+ <tr>
729
+ <td><b>:</b><i>name</i></td>
730
+ <td>
731
+ Captures anything but a forward slash in a semi-greedy fashion. Capture is named <i>name</i>.
732
+ Capture behavior can be modified with tt>capture</tt> and <tt>greedy</tt> option.
733
+ </td>
734
+ </tr>
735
+ <tr>
736
+ <td><b>*</b><i>name</i></td>
737
+ <td>
738
+ Captures anything in a non-greedy fashion. Capture is named <i>name</i>.
739
+ </td>
740
+ </tr>
741
+ <tr>
742
+ <td><b>(</b><i>expression</i><b>)</b></td>
743
+ <td>Enclosed <i>expression</i> is optional. Not available in 2.3 compatibility mode.</td>
744
+ </tr>
745
+ <tr>
746
+ <td><b>/</b></td>
747
+ <td>
748
+ Matches forward slash. Does not match URI encoded version of forward slash.
749
+ </td>
750
+ </tr>
751
+ <tr>
752
+ <td><b>\</b><i>x</i></td>
753
+ <td>
754
+ In 3.x compatibility mode and starting with 4.2:
755
+ Matches <i>x</i> or URI encoded version of <i>x</i>. For instance <tt>\*</tt> matches <tt>*</tt>.<br>
756
+ In 4.0 or 4.1 compatibility mode:
757
+ <b>\</b> is ignored, <i>x</i> is parsed normally.<br>
758
+ </td>
759
+ </tr>
760
+ <tr>
761
+ <td><i>expression</i> <b>|</b> <i>expression</i></td>
762
+ <td>
763
+ 3.2+ mode: This will raise a `Mustermann::ParseError`. While Ruby on Rails happily parses this character, it will result in broken routes due to a buggy implementation.<br>
764
+ 5.0 mode: It will match if any of the nested expressions matches.
765
+ </td>
766
+ </tr>
767
+ <tr>
768
+ <td><i>any other character</i></td>
769
+ <td>Matches exactly that character or a URI encoded version of it.</td>
770
+ </tr>
771
+ </tbody>
772
+ </table>
773
+
774
+ # Shell Syntax for Mustermann
775
+
776
+ This gem implements the `rails` pattern type for Mustermann. It is compatible with common Unix shells (like bash or zsh).
777
+
778
+ ## Overview
779
+
780
+ **Supported options:** `uri_decode` and `ignore_unknown_options`.
781
+
782
+ **External documentation:** [Ruby's fnmatch](http://www.ruby-doc.org/core-2.1.4/File.html#method-c-fnmatch), [Wikipedia: Glob (programming)](http://en.wikipedia.org/wiki/Glob_(programming))
783
+
784
+ ``` ruby
785
+ require 'mustermann'
786
+
787
+ pattern = Mustermann.new('/*', type: :shell)
788
+ pattern === "/foo.bar" # => true
789
+ pattern === "/foo/bar" # => false
790
+
791
+ pattern = Mustermann.new('/**/*', type: :shell)
792
+ pattern === "/foo.bar" # => true
793
+ pattern === "/foo/bar" # => true
794
+
795
+ pattern = Mustermann.new('/{foo,bar}', type: :shell)
796
+ pattern === "/foo" # => true
797
+ pattern === "/bar" # => true
798
+ pattern === "/baz" # => false
799
+ ```
800
+
801
+ ## Syntax
802
+
803
+ <table>
804
+ <thead>
805
+ <tr>
806
+ <th>Syntax Element</th>
807
+ <th>Description</th>
808
+ </tr>
809
+ </thead>
810
+ <tbody>
811
+ <tr>
812
+ <td><b>*</b></td>
813
+ <td>Matches anything but a slash.</td>
814
+ </tr>
815
+ <tr>
816
+ <td><b>**</b></td>
817
+ <td>Matches anything.</td>
818
+ </tr>
819
+ <tr>
820
+ <td><b>[</b><i>set</i><b>]</b></td>
821
+ <td>Matches one character in <i>set</i>.</td>
822
+ </tr>
823
+ <tr>
824
+ <td><b>&#123;</b><i>a</i>,<i>b</i><b>&#125;</b></td>
825
+ <td>Matches <i>a</i> or <i>b</i>.</td>
826
+ </tr>
827
+ <tr>
828
+ <td><b>\</b><i>x</i></td>
829
+ <td>Matches <i>x</i> or URI encoded version of <i>x</i>. For instance <tt>\*</tt> matches <tt>*</tt>.</td>
830
+ </tr>
831
+ <tr>
832
+ <td><i>any other character</i></td>
833
+ <td>Matches exactly that character or a URI encoded version of it.</td>
834
+ </tr>
835
+ </tbody>
836
+ </table>
837
+
838
+
839
+ # Simple Syntax for Mustermann
840
+
841
+ This gem implements the `simple` pattern type for Mustermann. It is compatible with [Sinatra](http://www.sinatrarb.com/) (1.x), [Scalatra](http://www.scalatra.org/) and [Dancer](http://perldancer.org/).
842
+
843
+ ## Overview
844
+
845
+ **Supported options:**
846
+ `greedy`, `space_matches_plus`, `uri_decode` and `ignore_unknown_options`.
847
+
848
+ This is useful for porting an application that relies on this behavior to a later Sinatra version and to make sure Sinatra 2.0 patterns do not decrease performance. Simple patterns internally use the same code older Sinatra versions used for compiling the pattern. Error messages for broken patterns will therefore not be as informative as for other pattern implementations.
849
+
850
+ ``` ruby
851
+ require 'mustermann'
852
+
853
+ pattern = Mustermann.new('/:example', type: :simple)
854
+ pattern === "/foo.bar" # => true
855
+ pattern === "/foo/bar" # => false
856
+ pattern.params("/foo.bar") # => { "example" => "foo.bar" }
857
+ pattern.params("/foo/bar") # => nil
858
+
859
+ pattern = Mustermann.new('/:example/?:optional?', type: :simple)
860
+ pattern === "/foo.bar" # => true
861
+ pattern === "/foo/bar" # => true
862
+ pattern.params("/foo.bar") # => { "example" => "foo.bar", "optional" => nil }
863
+ pattern.params("/foo/bar") # => { "example" => "foo", "optional" => "bar" }
864
+
865
+ pattern = Mustermann.new('/*', type: :simple)
866
+ pattern === "/foo.bar" # => true
867
+ pattern === "/foo/bar" # => true
868
+ pattern.params("/foo.bar") # => { "splat" => ["foo.bar"] }
869
+ pattern.params("/foo/bar") # => { "splat" => ["foo/bar"] }
870
+ ```
871
+
872
+ ## Syntax
873
+
874
+ <table>
875
+ <thead>
876
+ <tr>
877
+ <th>Syntax Element</th>
878
+ <th>Description</th>
879
+ </tr>
880
+ </thead>
881
+ <tbody>
882
+ <tr>
883
+ <td><b>:</b><i>name</i></td>
884
+ <td>
885
+ Captures anything but a forward slash in a greedy fashion. Capture is named <i>name</i>.
886
+ </td>
887
+ </tr>
888
+ <tr>
889
+ <td><b>*</b></td>
890
+ <td>
891
+ Captures anything in a non-greedy fashion. Capture is named splat.
892
+ It is always an array of captures, as you can use <tt>*</tt> more than once in a pattern.
893
+ </td>
894
+ </tr>
895
+ <tr>
896
+ <td><i>x</i><b>?</b></td>
897
+ <td>Makes <i>x</i> optional. For instance <tt>foo?</tt> matches <tt>foo</tt> or <tt>fo</tt>.</td>
898
+ </tr>
899
+ <tr>
900
+ <td><b>/</b></td>
901
+ <td>
902
+ Matches forward slash. Does not match URI encoded version of forward slash.
903
+ </td>
904
+ </tr>
905
+ <tr>
906
+ <td><i>any special character</i></td>
907
+ <td>Matches exactly that character or a URI encoded version of it.</td>
908
+ </tr>
909
+ <tr>
910
+ <td><i>any other character</i></td>
911
+ <td>Matches exactly that character.</td>
912
+ </tr>
913
+ </tbody>
914
+ </table>
915
+
916
+ # String Scanner for Mustermann
917
+
918
+ This gem implements `Mustermann::StringScanner`, a tool inspired by Ruby's [`StringScanner`]() class.
919
+
920
+ ``` ruby
921
+ require 'mustermann/string_scanner'
922
+ scanner = Mustermann::StringScanner.new("here is our example string")
923
+
924
+ scanner.scan("here") # => "here"
925
+ scanner.getch # => " "
926
+
927
+ if scanner.scan(":verb our")
928
+ scanner.scan(:noun, capture: :word)
929
+ scanner[:verb] # => "is"
930
+ scanner[:nound] # => "example"
931
+ end
932
+
933
+ scanner.rest # => "string"
934
+ ```
935
+
936
+ You can pass it pattern objects directly:
937
+
938
+ ``` ruby
939
+ pattern = Mustermann.new(':name')
940
+ scanner.check(pattern)
941
+ ```
942
+
943
+ Or have `#scan` (and other methods) check these for you.
944
+
945
+ ``` ruby
946
+ scanner.check('{name}', type: :template)
947
+ ```
948
+
949
+ You can also pass in default options for ad hoc patterns when creating the scanner:
950
+
951
+ ``` ruby
952
+ scanner = Mustermann::StringScanner.new(input, type: :shell)
953
+ ```
954
+
955
+ # URI Template Syntax for Mustermann
956
+
957
+ This gem implements the `uri-template` (or `template`) pattern type for Mustermann. It is compatible with [RFC 6570](https://tools.ietf.org/html/rfc6570) (level 4), [JSON API](http://jsonapi.org/), [JSON Home Documents](http://tools.ietf.org/html/draft-nottingham-json-home-02) and [many more](https://code.google.com/p/uri-templates/wiki/Implementations)
958
+
959
+ ## Overview
960
+
961
+ **Supported options:**
962
+ `capture`, `except`, `greedy`, `space_matches_plus`, `uri_decode`, and `ignore_unknown_options`.
963
+
964
+ Please keep the following in mind:
965
+
966
+ > "Some URI Templates can be used in reverse for the purpose of variable matching: comparing the template to a fully formed URI in order to extract the variable parts from that URI and assign them to the named variables. Variable matching only works well if the template expressions are delimited by the beginning or end of the URI or by characters that cannot be part of the expansion, such as reserved characters surrounding a simple string expression. In general, regular expression languages are better suited for variable matching."
967
+ > &mdash; *RFC 6570, Sec 1.5: "Limitations"*
968
+
969
+ ``` ruby
970
+ require 'mustermann'
971
+
972
+ pattern = Mustermann.new('/{example}', type: :template)
973
+ pattern === "/foo.bar" # => true
974
+ pattern === "/foo/bar" # => false
975
+ pattern.params("/foo.bar") # => { "example" => "foo.bar" }
976
+ pattern.params("/foo/bar") # => nil
977
+
978
+ pattern = Mustermann.new("{/segments*}/{page}{.ext,cmpr:2}", type: :template)
979
+ pattern.params("/a/b/c.tar.gz") # => {"segments"=>["a","b"], "page"=>"c", "ext"=>"tar", "cmpr"=>"gz"}
980
+ ```
981
+
982
+ ## Generating URI Templates
983
+
984
+ You do not need to use URI templates (and this gem) if all you want is reusing them for hypermedia links. Most other pattern types support generating these (via `#to_pattern`):
985
+
986
+ ``` ruby
987
+ require 'mustermann'
988
+
989
+ Mustermann.new('/:name').to_templates # => ['/{name}']
990
+ ```
991
+
992
+ Moreover, Mustermann's default pattern type implements a subset of URI templates (`{capture}` and `{+capture}`) and can therefore also be used for simple templates/
993
+
994
+ ``` ruby
995
+ require 'mustermann'
996
+
997
+ Mustermann.new('/{name}').expand(name: "example") # => "/example"
998
+ ```
999
+
1000
+ ## Syntax
1001
+
1002
+ <table>
1003
+ <thead>
1004
+ <tr>
1005
+ <th>Syntax Element</th>
1006
+ <th>Description</th>
1007
+ </tr>
1008
+ </thead>
1009
+ <tbody>
1010
+ <tr>
1011
+ <td><b>&#123;</b><i>o</i> <i>var</i> <i>m</i><b>,</b> <i>var</i> <i>m</i><b>,</b> ...<b>&#125;</b></td>
1012
+ <td>
1013
+ Captures expansion.
1014
+ Operator <i>o</i>: <code>+ # . / ; ? &amp;</tt> or none.
1015
+ Modifier <i>m</i>: <code>:num *</tt> or none.
1016
+ </td>
1017
+ </tr>
1018
+ <tr>
1019
+ <td><b>/</b></td>
1020
+ <td>
1021
+ Matches forward slash. Does not match URI encoded version of forward slash.
1022
+ </td>
1023
+ </tr>
1024
+ <tr>
1025
+ <td><i>any other character</i></td>
1026
+ <td>Matches exactly that character or a URI encoded version of it.</td>
1027
+ </tr>
1028
+ </tbody>
1029
+ </table>
1030
+
1031
+ The operators `+` and `#` will always match non-greedy, whereas all other operators match semi-greedy by default.
1032
+ All modifiers and operators are supported. However, it does not parse lists as single values without the *explode* modifier (aka *star*).
1033
+ Parametric operators (`;`, `?` and `&`) currently only match parameters in given order.
1034
+
1035
+ Note that it differs from URI templates in that it takes the unescaped version of special character instead of the escaped version.
1036
+
1037
+ If you reuse the exact same templates and expose them via an external API meant for expansion,
1038
+ you should set `uri_decode` to `false` in order to conform with the specification.
1039
+
1040
+
1041
+ # Mustermann Pattern Visualizer
1042
+
1043
+ With this gem, you can visualize the internal structure of a Mustermann pattern:
1044
+
1045
+ * You can generate a **syntax highlighted** version of a pattern object. Both HTML/CSS based highlighting and ANSI color code based highlighting is supported.
1046
+ * You can turn a pattern object into a **tree** (with ANSI color codes) representing the internal AST. This of course only works for AST based patterns.
1047
+
1048
+ ## Syntax Highlighting
1049
+
1050
+ ![](highlighting.png)
1051
+
1052
+ Loading `mustermann/visualizer` will automatically add `to_html` and `to_ansi` to pattern objects.
1053
+
1054
+ ``` ruby
1055
+ require 'mustermann/visualizer'
1056
+ puts Mustermann.new('/:name').to_ansi
1057
+ puts Mustermann.new('/:name').to_html
1058
+ ```
1059
+
1060
+ Alternatively, you can also create a separate `highlight` object, which allows finer grained control and more formats:
1061
+
1062
+ ``` ruby
1063
+ require 'mustermann/visualizer'
1064
+
1065
+ pattern = Mustermann.new('/:name')
1066
+ highlight = Mustermann::Visualizer.highlight(pattern)
1067
+
1068
+ puts highlight.to_ansi
1069
+ ```
1070
+ ### `inspect` mode
1071
+
1072
+ By default, the highlighted string will be a colored version of `to_s`. It is also possible to produce a colored version of `inspect`
1073
+
1074
+ ``` ruby
1075
+ require 'mustermann/visualizer'
1076
+
1077
+ pattern = Mustermann.new('/:name')
1078
+
1079
+ # directly from the pattern
1080
+ puts pattern.to_ansi(inspect: true)
1081
+
1082
+ # via the highlighter
1083
+ highlight = Mustermann::Visualizer.highlight(pattern, inspect: true)
1084
+ puts highlight.to_ansi
1085
+ ```
1086
+
1087
+ ### Themes
1088
+
1089
+ ![](theme.png)
1090
+
1091
+ element | inherits style from | default theme | note
1092
+ -------------|---------------------|---------------|-------------------------
1093
+ default | | #839496 | ANSI `\e[10m` if not set
1094
+ special | default | #268bd2 |
1095
+ capture | special | #cb4b16 |
1096
+ name | | #b58900 | always inside `capture`
1097
+ char | default | |
1098
+ expression | capture | | only exists in URI templates
1099
+ composition | special | | meta style, does not exist directly
1100
+ composite | composition | | used for composite patterns (contains `root`s)
1101
+ group | composition | |
1102
+ union | composition | |
1103
+ optional | special | |
1104
+ root | default | | wraps the whole pattern
1105
+ separator | char | #93a1a1 |
1106
+ splat | capture | |
1107
+ named_splat | splat | |
1108
+ variable | capture | | always inside `expression`
1109
+ escaped | char | #93a1a1 |
1110
+ escaped_char | | | always inside `escaped`
1111
+ quote | special | #dc322f | always outside of `root`
1112
+ type | special | | always inside `composite`, outside of `root`
1113
+ illegal | special | #8b0000 |
1114
+
1115
+ You can set theme any of the above elements. The default theme will only be applied if no custom theming is used.
1116
+
1117
+ ``` ruby
1118
+ # custom theme with highlight object
1119
+ highlight = Mustermann::Visualizer.highlight(pattern, special: "#08f")
1120
+ puts highlight.to_ansi
1121
+ ```
1122
+
1123
+ Themes apply both to ANSI and to HTML/CSS output. The exact ANSI code used depends on the terminal and its capabilities.
1124
+
1125
+ ### HTML and CSS
1126
+
1127
+ By default, the syntax elements will be translated into `span` tags with `style` attributes.
1128
+
1129
+ ``` ruby
1130
+ Mustermann.new('/:name').to_html
1131
+ ```
1132
+
1133
+ ``` html
1134
+ <span style="color: #839496;"><span style="color: #93a1a1;">/</span><span style="color: #cb4b16;">:<span style="color: #b58900;">name</span></span></span></span>
1135
+ ```
1136
+
1137
+ You can also set the `css` option to `true` to make it include a stylesheet instead.
1138
+
1139
+ ``` ruby
1140
+ Mustermann.new('/:name').to_html(css: true)
1141
+ ```
1142
+
1143
+ ``` html
1144
+ <span class="mustermann_pattern"><style type="text/css">
1145
+ .mustermann_pattern .mustermann_name {
1146
+ color: #b58900;
1147
+ }
1148
+ /* ... etc ... */
1149
+ </style><span class="mustermann_root"><span class="mustermann_separator">/</span><span class="mustermann_capture">:<span class="mustermann_name">name</span></span></span></span>
1150
+ ```
1151
+
1152
+ Or you can set it to `false`, which will omit `style` attributes, but include `class` attributes.
1153
+
1154
+ ``` html
1155
+ <span class="mustermann_pattern"><span class="mustermann_root"><span class="mustermann_separator">/</span><span class="mustermann_capture">:<span class="mustermann_name">name</span></span></span></span>
1156
+ ```
1157
+
1158
+ It is possible to change the class prefix and the tag used.
1159
+
1160
+ ``` ruby
1161
+ Mustermann.new('/:name').to_html(css: false, class_prefix: "mm_", tag: "tt")
1162
+ ```
1163
+
1164
+ ``` html
1165
+ <tt class="mm_pattern"><tt class="mm_root"><tt class="mm_separator">/</tt><tt class="mm_capture">:<tt class="mm_name">name</tt></tt></tt></tt>
1166
+ ```
1167
+
1168
+ If you create a highlight object, you can ask it for its `stylesheet`.
1169
+
1170
+ ``` erb
1171
+ <% highlight = Mustermann::Visualizer.highlight("/:name") %>
1172
+
1173
+ <html>
1174
+ <head>
1175
+ <style type="text/css">
1176
+ <%= highlight.stylesheet %>
1177
+ </style>
1178
+ </head>
1179
+ <body>
1180
+ <%= highlight.to_html(css: false) %>
1181
+ </body>
1182
+ </html>
1183
+ ```
1184
+
1185
+
1186
+ ### Other formats
1187
+
1188
+ If you create a highlight object, you have two other formats available: Hansi template strings and s-expression like strings. These might be useful if you want to check how a theme will be applied or as intermediate format for highlighting by other means.
1189
+
1190
+ ``` ruby
1191
+ require 'mustermann/visualizer'
1192
+ highlight = Mustermann::Visualizer.highlight("/:page")
1193
+ puts highlight.to_hansi_template
1194
+ puts highlight.to_sexp
1195
+ ```
1196
+
1197
+ **Hansi template strings** wrap elements in tags that are similar to XML tags (though they are not, entity encoding and attributes are not supported, escaping works with a slash, so an escaped `>` would be `\>`, not `&gt;`).
1198
+
1199
+ ``` xml
1200
+ <pattern><root><separator>/</separator><capture>:<name>page</name></capture></root></pattern>
1201
+ ```
1202
+
1203
+ The **s-expression like syntax** looks as follows:
1204
+
1205
+ ```
1206
+ (root (separator /) (capture : (name page)))
1207
+ ```
1208
+
1209
+ * An expression is enclosed by parens and contains elements separated by spaces. The first element in the expression type (corresponding to themeable elements). These are simple strings. The other elements are either expressions, simple strings or full strings.
1210
+ * Simple strings do not contain spaces, parens, single or double quotes or any character that needs to be escaped.
1211
+ * Full strings are Ruby strings enclosed by double quotes.
1212
+ * Spaces before or after parens are optional.
1213
+
1214
+ ### IRB/Pry integration
1215
+
1216
+ When `mustermann` is being loaded from within an IRB or Pry session, it will automatically load `mustermann/visualizer` too, if possible.
1217
+ When displayed as result, it will be highlighted.
1218
+
1219
+ ![](irb.png)
1220
+
1221
+ In Pry, this will even work when nested inside other objects (like as element on an array).
1222
+
1223
+ ## Tree Rendering
1224
+
1225
+ ![](tree.png)
1226
+
1227
+ Loading `mustermann/visualizer` will automatically add `to_tree` to pattern objects.
1228
+
1229
+ ``` ruby
1230
+ require 'mustermann/visualizer'
1231
+ puts Mustermann.new("/:page(.:ext)?/*action").to_tree
1232
+ ```
1233
+
1234
+ For patterns not based on an AST (shell, simple, regexp), it will print out a single line:
1235
+
1236
+ pattern (not AST based) "/example"
1237
+
1238
+ It will display a tree for identity patterns. While these are not based on an AST internally, Mustermann supports generating an AST for these patterns.
1239
+